I have a suspicion that the socket is still open in the stack frames. When Python throws an exception, it saves the stack frames, so debuggers and other tools can view the stack and evaluate values.
For historical reasons, and now for backward compatibility, information about the stack is stored (downstream) in sys (see sys.exc_info (), sys.exc_type and others). This is one of the things that were removed in Python 3.0.
For you, this means that the stack is still alive and referenced. There, the stack contains local data for some function that has an open socket. This is why the socket is not yet closed. It is only when the stack trace is deleted that everything will be gc'ed.
To check this, insert something like
try: 1/0 except ZeroDivisionError: pass
in the except clause. This is a quick way to replace the current exception with something else.
source share