This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2017-09-19.16:30:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1505838609.92.0.689981315301.issue31520@psf.upfronthosting.co.za>
In-reply-to
Content
_socket.socket object destructor emits a ResourceWarning if the socket is not closed. 

The problem is this warning:

build/Lib/contextlib.py:60: ResourceWarning: unclosed <socket.socket [closed] fd=3, family=AddressFamily.AF_INET, type=2049, proto=6>
  self.gen = func(*args, **kwds)

The message says "unclosed" and "closed" in the same sentence. It's confusing.

In fact, the Python module "socket" has a socket.socket class based on the _socket.socket of the C module "_socket".

The Python module has a private _closed attribute set to True as soon as close() was closed. *But* the underlying _socket.socket.close() is only called once the "io refs" counter reachs zero.

The Python module allows to open to "fork" the socket using the makefile() method: the "io refs" counter is increased in that case. makefile() creates a raw socket.SocketIO() object which will call the close() method of the original socket.

Ok, let's come back at the C level. The _socket.socket destructor checks if _socket.socket.close() was called to decide if the ResourceWarning should be emitted or not.

Maybe SocketIO should raise the ResourceWarning?

IMHO the minimum patch is to modify socket.socket.__repr__() to include the "io refs" counter. So a developer knowing the implementation can understand the surprising warning.
History
Date User Action Args
2017-09-19 16:30:09vstinnersetrecipients: + vstinner
2017-09-19 16:30:09vstinnersetmessageid: <1505838609.92.0.689981315301.issue31520@psf.upfronthosting.co.za>
2017-09-19 16:30:09vstinnerlinkissue31520 messages
2017-09-19 16:30:09vstinnercreate