Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3: ResourceWarning Unclosed

I have a Reddit Bot coded in Python where sometimes I get the following error:

sys:1: ResourceWarning: unclosed ssl.SSLSocket fd=4, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('192.168.1.113', 55513), raddr=('198.41.209.140', 443)>

192.168.1.113 is my local IP and 198.41.209.140 is apparently CloudFlare's IP

I also sometimes get the following error which makes me think the problem is related to SSL?

Error EOF occurred in violation of protocol (_ssl.c:598)

How do I fix this issue?

Edit: How do I see what part of my code is causing this issue? I have an exception in my try/except to print all exceptions as:

except Exception as e:
    print("Login/API Error", e)

but all the errors i get do not have this which means it is not an exception?

like image 346
Bijan Avatar asked Oct 15 '25 17:10

Bijan


1 Answers

I have not used sockets in my own code, so the following is based on my general Python knowledge.

The socket docs do not say that a socket.socket instance is a context manager, but the class has the requisite __enter__ and __exit__ methods. Both are standard, minimal, implementations. It is possible you might want to subclass socket or SSLSocket to do something more in __exit__, such as printing your substitute for a traceback. But to start, you need to see the full tracebacks. Subclass SSLSocket inherits both methods. Since an SSLSocket "wraps" a socket, there are apparently two objects to be concerned with. So I would start with the following (untested, obviously) for the top level structure of your code:

<preliminary code>
with socket.socket(...) as sock  # or other socket-returning function
  <more preparation>
  with ssl.wrap_socket(sock, ...) as SSLsock # or the context function
    <use SSLsock to communicate>

The error message you got should have been prefixed with SSLEOFError. Was it? The doc says "A subclass of SSLError raised when the SSL connection has been terminated abruptly. Generally, you shouldn’t try to reuse the underlying transport when this error is encountered." This is definitely an Exception.

>>> issubclass(ssl.SSLEOFError, Exception)
True

What might provoke Cloudfare into terminating the connection 'abruptly'? A bug in its protocol handling; a bug in ssl.py? Does your bot somehow violate Terms of Service?

If you can't get all answers you need here, I would try python-list, also accessible as newsgroup gmane.comp.python.general at news.gmane.org. It has participants with network experience.

like image 133
Terry Jan Reedy Avatar answered Oct 17 '25 11:10

Terry Jan Reedy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!