Solved

ResourceWarning: unclosed SSL socket


I get the following warning every time I create a CogniteClient:

 

ResourceWarning: unclosed <ssl.SSLSocket fd=5, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('10.xx.xx.xx', 49572), raddr=('40.xx.xx.xx', 443)>

 

Is this caused by the connection handling in Cognite SDK? I’m using the following Python modules:

cognite-sdk-4.11.0

cognite-sdk-core-2.56.1

icon

Best answer by Håkon V. Treider 2 November 2022, 07:19

View original

10 replies

Userlevel 2

Hi

I’m not sure if it solves your problem, but you should remove the “cognite-sdk-core” package. With SDK >v4 it was removed so it might cause issues to have both packages installed.

Are you running the code behind a firewall/proxy?

I removed cognite-sdk-core, but the warning remains.

I’m running the code on my local machine at work. I assume I’m behind a company firewall, but I’m getting the same warning when I’m on my home network as well.

Userlevel 2

Can you share more of the stacktrace? Does it still work?

There isn’t any more stacktrace than what I posted. It still works, yes.

Userlevel 4
Badge

Could you run your script like the following (converts warnings to exceptions):

>>> python -Werror my_script.py

That way we might get some more insight into exactly where the warning originates from. 

I tried running it with  -Werror, but the only difference in output was the aforementioned ResourceWarning duplicated as an Exception.

Userlevel 4
Badge

Great, can you post the stack trace please?

$ python -Werror test_cdf_endpoint.py 
Exception ignored in: <ssl.SSLSocket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('10.x.x.x', 40596), raddr=('20.x.x.x', 443)>
Traceback (most recent call last):
File "/home/user/projects/my-project/env/lib/python3.9/site-packages/yaml/scanner.py", line 286, in stale_possible_simple_keys
for level in list(self.possible_simple_keys):
ResourceWarning: unclosed <ssl.SSLSocket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('10.x.x.x', 40596), raddr=('20.x.x.x', 443)>

 

Userlevel 4
Badge

A bit of a weird stack trace, very short? Only file mentioned is from yaml lib, which surely can’t be the cause? From googling around, it seems completely safe to just silence this particular warning, see e.g. https://github.com/boto/boto3/issues/454#issuecomment-324782994

warnings.filterwarnings("ignore", category=ResourceWarning, message="unclosed.*<ssl.SSLSocket.*>") 

Yes, the stack trace didn’t make much sense to me neither. I’ll just ignore the warning like you suggested. Thanks for your help.

Reply