New submission from STINNER Victor:

_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.

----------
components: Library (Lib)
messages: 302541
nosy: haypo
priority: normal
severity: normal
status: open
title: ResourceWarning: unclosed <socket.socket [closed] fd=3, ...> warning
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue31520>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to