Fix closing of socket not sending TCP FIN on windows

This commit is contained in:
Kovid Goyal 2015-05-22 18:50:11 +05:30
parent f8974e3a4c
commit 4cbf4540ae

View File

@ -328,7 +328,6 @@ class Connection(object): # {{{
remote_addr = None remote_addr = None
remote_port = None remote_port = None
linger = False
def __init__(self, server_loop, socket): def __init__(self, server_loop, socket):
self.server_loop = server_loop self.server_loop = server_loop
@ -346,25 +345,8 @@ class Connection(object): # {{{
def close(self): def close(self):
"""Close the socket underlying this connection.""" """Close the socket underlying this connection."""
self.socket_file.close() self.socket_file.close()
self.socket.shutdown(socket.SHUT_WR)
if not self.linger:
# Python's socket module does NOT call close on the kernel
# socket when you call socket.close(). We do so manually here
# because we want this server to send a FIN TCP segment
# immediately. Note this must be called *before* calling
# socket.close(), because the latter drops its reference to
# the kernel socket.
if hasattr(self.socket, '_sock'):
self.socket._sock.close()
self.socket.close() self.socket.close()
else:
# On the other hand, sometimes we want to hang around for a bit
# to make sure the client has a chance to read our entire
# response. Skipping the close() calls here delays the FIN
# packet until the socket object is garbage-collected later.
# Someday, perhaps, we'll do the full lingering_close that
# Apache does, but not today.
pass
def __enter__(self): def __enter__(self):
return self return self
@ -496,6 +478,7 @@ class ThreadPool(object): # {{{
c = worker.conn c = worker.conn
if c and not c.socket_file.closed: if c and not c.socket_file.closed:
c.socket.shutdown(socket.SHUT_RDWR) c.socket.shutdown(socket.SHUT_RDWR)
c.socket.close()
worker.join() worker.join()
except (AssertionError, except (AssertionError,
# Ignore repeated Ctrl-C. # Ignore repeated Ctrl-C.