3 ms·
Python uses reference counting. The exception was preventing the code from reaching the point where it sets the reference to the socket to None. This isn't clea
by js2 5y ago
Python uses reference counting. The exception was preventing the code from reaching the point where it sets the reference to the socket to None. This isn't clear in the minimized example in the post, but if you follow the link to the PR[1], the original code looked like this:
def close(self):
if self.sock is not None:
self._shutdown_transport()
# Call shutdown first to make sure that pending messages
# reach the AMQP broker if the program exits after
# calling this method.
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
self.sock = None
self.connected = False
If either shutdown (or close) raises an exception, self.sock isn't set to None. Thus Python retains the reference. Aside, but setting self.sock to None will eventually close the socket once Python GC's the reference[2]. It's still best to explicitly call close though. From [2]:
> Python takes the automatic shutdown a step further, and says that when a socket is garbage collected, it will automatically do a close if it’s needed. But relying on this is a very bad habit. If your socket just disappears without doing a close, the socket at the other end may hang indefinitely, thinking you’re just being slow. Please close your sockets when you’re done.
Another way you can have leaks with a reference counting GC is through circular references. Python provides WeakRef for such situations, but it's up to the programmer to use it correctly.
1. https://github.com/celery/celery/issues/4843#issuecomment-991394349 https://github.com/celery/celery/issues/4843#issuecomment-99...
2. https://docs.python.org/3/howto/sockets.html#disconnecting https://docs.python.org/3/howto/sockets.html#disconnecting
- tmarice 5y agoPython uses a generational reference counting GC, it should be able to handle circular references.