Message302541
_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. |
|
Date |
User |
Action |
Args |
2017-09-19 16:30:09 | vstinner | set | recipients:
+ vstinner |
2017-09-19 16:30:09 | vstinner | set | messageid: <1505838609.92.0.689981315301.issue31520@psf.upfronthosting.co.za> |
2017-09-19 16:30:09 | vstinner | link | issue31520 messages |
2017-09-19 16:30:09 | vstinner | create | |
|