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.

classification
Title: Block on close TCP socket in SocketServer.py
Type: enhancement Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jarvisliang, neologix, pitrou
Priority: normal Keywords:

Created on 2012-08-01 10:07 by jarvisliang, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg167110 - (view) Author: Jarvis (jarvisliang) Date: 2012-08-01 10:07
In the Python 2.4, it closes the socket only by calling request.close() method. There is a risk by using this method to close socket. If the socket handle count does not reach zero because another process still has a handle to the socket then the connection is not closed and the socket is not deallocated. So in Python 2.7 it updates it by calling request.shutdown() first, which can close the underlying connection and send a FIN/EOF to the peer regardless of how many processes have handles to the socket. After that, it calls request.close() to deallocate the socket. You can see this updates below that is from the file of C:\Python27\Lib\SocketServer.py.

    def shutdown_request(self, request):
        """Called to shutdown and close an individual request."""
        try:
            #explicitly shutdown.  socket.close() merely releases
            #the socket and waits for GC to perform the actual close.
            request.shutdown(socket.SHUT_WR)
        except socket.error:
            pass #some platforms may raise ENOTCONN here
        self.close_request(request)

However,it will block at self.close_request(request) after request.shutdown(socket.SHUT_WR) if there are remaining data on reading side of socket.

Here, it calls request.shutdown() with SHUT_WR flag, which means it only shuts down the writing side of the socket. The reading side of the socket isn't shut down at same time. So when calling close_request to deallocate the socket, it will always be waiting to read response until response data is available. It seems like an issue in SokcetServer.py library.

In terms of that, I replaced the SHUT_WR flag with SHUT_RDWR (shut down both reading and writing sides of the socket) for request.shutdown method. Then this issue was resolved, the SSL connection was closed immediately.
msg169646 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2012-09-01 16:53
> So when calling close_request to deallocate the socket, it will 
> always be waiting to read response until response data is available. 
> It seems like an issue in SokcetServer.py library.

Hum, I don't see what you mean.
Even if there is still data in the receive socket buffer, socket.clos() shouldn't block. Actually, it should result in a RST being sent.
Could you describe your problem more precisely (with a code snippet it would be better)?
History
Date User Action Args
2022-04-11 14:57:33adminsetgithub: 59728
2013-05-20 21:37:40neologixsetstatus: pending -> closed
resolution: not a bug
stage: resolved
2012-12-24 09:33:58neologixsetstatus: open -> pending
2012-09-01 16:54:00neologixsetnosy: + neologix
messages: + msg169646
2012-08-06 08:41:54tshepangsetnosy: + pitrou
2012-08-01 10:07:47jarvisliangcreate