Repository navigation
socket close() hangs client if used without prior shutdown() #43559
Description
Activity
In Python 2.5b1, when closing a client socket using
socket.close(), the connecting client hangs. I.e. the
socket is not properly taken down. If you add an
explicit call to socket.shutdown(), prior to the
close(), it works again.But I think this shutdown() should not be mandatory? At
least, it wasn't in older Python versions.Sample code attached. Run the script and connect to
socket 9000. If you remove the call to shutdown, your
client connection will hang (use telnet or netcat).- addedextension-modulesC modules in the Modules dirC modules in the Modules dir
on Jun 27, 2006 - addedextension-modulesC modules in the Modules dirC modules in the Modules dir
on Jun 27, 2006 Logged In: YES
user_id=129426Oops forgot to mention: Tested with Python2.5b1 (official
binary releases) on Windows XP and on Mac OS tiger.Logged In: YES
user_id=33168Shutdown should not be mandatory.
I can reproduce the same behaviour under linux. This is new
breakage in 2.5 and needs to be fixed.Logged In: YES
user_id=21627The problem is that _socketobject.close fails to set
recv_into and recvfrom_into to _dummy.The real problem seems to me that close relies on
refcounting to close the underlying socket object. I think
it should first call self._sock.close() before releasing
it.Also, a test case should be added that the socket object
really does go away after close, e.g. in the formnative_socket = s._sock s.close() assert sys.getrefcount(native_socket) == 2
Logged In: YES
user_id=33168The attached patch fixes the problem for me. It includes a
test. If no one gets to it in the next few days, I'll apply it.It could be augmented with Martin's suggestion to check the
refcount.Logged In: YES
user_id=1126061The patch fixes the problem for me too, on NetBSD 3.0,
revision 47189.
Thanks, Matt
Logged In: YES
user_id=21627I committed this as r47190. Adding my test is pointless in
the current state: the test already passes only if the
socket gets released by .close.The refcount test would have been useful if there was an
explicit self._sock.close() call in close. I tried adding
one, but that would not work because you then can't call
close multiple times anymore.Logged In: YES
user_id=129426I updated to latest svn and my code works again now.
Thanks for fixing this in such short notice.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: