mirror of
https://github.com/python/cpython.git
synced 2025-10-02 21:25:24 +00:00
Issue #2302: Fix a race condition in SocketServer.BaseServer.shutdown,
where the method could block indefinitely if called just before the event loop started running. This also fixes the occasional freezes witnessed in test_httpservers.
This commit is contained in:
parent
1ca8c19b65
commit
a624040d72
3 changed files with 44 additions and 12 deletions
|
@ -198,7 +198,7 @@ class BaseServer:
|
||||||
self.server_address = server_address
|
self.server_address = server_address
|
||||||
self.RequestHandlerClass = RequestHandlerClass
|
self.RequestHandlerClass = RequestHandlerClass
|
||||||
self.__is_shut_down = threading.Event()
|
self.__is_shut_down = threading.Event()
|
||||||
self.__serving = False
|
self.__shutdown_request = False
|
||||||
|
|
||||||
def server_activate(self):
|
def server_activate(self):
|
||||||
"""Called by constructor to activate the server.
|
"""Called by constructor to activate the server.
|
||||||
|
@ -215,17 +215,19 @@ class BaseServer:
|
||||||
self.timeout. If you need to do periodic tasks, do them in
|
self.timeout. If you need to do periodic tasks, do them in
|
||||||
another thread.
|
another thread.
|
||||||
"""
|
"""
|
||||||
self.__serving = True
|
|
||||||
self.__is_shut_down.clear()
|
self.__is_shut_down.clear()
|
||||||
while self.__serving:
|
try:
|
||||||
# XXX: Consider using another file descriptor or
|
while not self.__shutdown_request:
|
||||||
# connecting to the socket to wake this up instead of
|
# XXX: Consider using another file descriptor or
|
||||||
# polling. Polling reduces our responsiveness to a
|
# connecting to the socket to wake this up instead of
|
||||||
# shutdown request and wastes cpu at all other times.
|
# polling. Polling reduces our responsiveness to a
|
||||||
r, w, e = select.select([self], [], [], poll_interval)
|
# shutdown request and wastes cpu at all other times.
|
||||||
if r:
|
r, w, e = select.select([self], [], [], poll_interval)
|
||||||
self._handle_request_noblock()
|
if self in r:
|
||||||
self.__is_shut_down.set()
|
self._handle_request_noblock()
|
||||||
|
finally:
|
||||||
|
self.__shutdown_request = False
|
||||||
|
self.__is_shut_down.set()
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
"""Stops the serve_forever loop.
|
"""Stops the serve_forever loop.
|
||||||
|
@ -234,7 +236,7 @@ class BaseServer:
|
||||||
serve_forever() is running in another thread, or it will
|
serve_forever() is running in another thread, or it will
|
||||||
deadlock.
|
deadlock.
|
||||||
"""
|
"""
|
||||||
self.__serving = False
|
self.__shutdown_request = True
|
||||||
self.__is_shut_down.wait()
|
self.__is_shut_down.wait()
|
||||||
|
|
||||||
# The distinction between handling, getting, processing and
|
# The distinction between handling, getting, processing and
|
||||||
|
|
|
@ -241,6 +241,31 @@ class SocketServerTest(unittest.TestCase):
|
||||||
# SocketServer.DatagramRequestHandler,
|
# SocketServer.DatagramRequestHandler,
|
||||||
# self.dgram_examine)
|
# self.dgram_examine)
|
||||||
|
|
||||||
|
@reap_threads
|
||||||
|
def test_shutdown(self):
|
||||||
|
# Issue #2302: shutdown() should always succeed in making an
|
||||||
|
# other thread leave serve_forever().
|
||||||
|
class MyServer(SocketServer.TCPServer):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class MyHandler(SocketServer.StreamRequestHandler):
|
||||||
|
pass
|
||||||
|
|
||||||
|
threads = []
|
||||||
|
for i in range(20):
|
||||||
|
s = MyServer((HOST, 0), MyHandler)
|
||||||
|
t = threading.Thread(
|
||||||
|
name='MyServer serving',
|
||||||
|
target=s.serve_forever,
|
||||||
|
kwargs={'poll_interval':0.01})
|
||||||
|
t.daemon = True # In case this function raises.
|
||||||
|
threads.append((t, s))
|
||||||
|
for t, s in threads:
|
||||||
|
t.start()
|
||||||
|
s.shutdown()
|
||||||
|
for t, s in threads:
|
||||||
|
t.join()
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
if imp.lock_held():
|
if imp.lock_held():
|
||||||
|
|
|
@ -25,6 +25,11 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #2302: Fix a race condition in SocketServer.BaseServer.shutdown,
|
||||||
|
where the method could block indefinitely if called just before the
|
||||||
|
event loop started running. This also fixes the occasional freezes
|
||||||
|
witnessed in test_httpservers.
|
||||||
|
|
||||||
- Issue #5103: SSL handshake would ignore the socket timeout and block
|
- Issue #5103: SSL handshake would ignore the socket timeout and block
|
||||||
indefinitely if the other end didn't respond.
|
indefinitely if the other end didn't respond.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue