diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-04-25 21:40:32 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-04-25 21:40:32 (GMT) |
commit | a624040d72b7a83bab4737fdfe14b747b530f858 (patch) | |
tree | 3cb9b0e4f38ea8217ba1dea4822e5953f8de644a /Lib | |
parent | 1ca8c19b65637d292e3d10408ee6243447ec550a (diff) | |
download | cpython-a624040d72b7a83bab4737fdfe14b747b530f858.zip cpython-a624040d72b7a83bab4737fdfe14b747b530f858.tar.gz cpython-a624040d72b7a83bab4737fdfe14b747b530f858.tar.bz2 |
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.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/SocketServer.py | 26 | ||||
-rw-r--r-- | Lib/test/test_socketserver.py | 25 |
2 files changed, 39 insertions, 12 deletions
diff --git a/Lib/SocketServer.py b/Lib/SocketServer.py index ff56d98..f0db511 100644 --- a/Lib/SocketServer.py +++ b/Lib/SocketServer.py @@ -198,7 +198,7 @@ class BaseServer: self.server_address = server_address self.RequestHandlerClass = RequestHandlerClass self.__is_shut_down = threading.Event() - self.__serving = False + self.__shutdown_request = False def server_activate(self): """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 another thread. """ - self.__serving = True self.__is_shut_down.clear() - while self.__serving: - # XXX: Consider using another file descriptor or - # connecting to the socket to wake this up instead of - # polling. Polling reduces our responsiveness to a - # shutdown request and wastes cpu at all other times. - r, w, e = select.select([self], [], [], poll_interval) - if r: - self._handle_request_noblock() - self.__is_shut_down.set() + try: + while not self.__shutdown_request: + # XXX: Consider using another file descriptor or + # connecting to the socket to wake this up instead of + # polling. Polling reduces our responsiveness to a + # shutdown request and wastes cpu at all other times. + r, w, e = select.select([self], [], [], poll_interval) + if self in r: + self._handle_request_noblock() + finally: + self.__shutdown_request = False + self.__is_shut_down.set() def shutdown(self): """Stops the serve_forever loop. @@ -234,7 +236,7 @@ class BaseServer: serve_forever() is running in another thread, or it will deadlock. """ - self.__serving = False + self.__shutdown_request = True self.__is_shut_down.wait() # The distinction between handling, getting, processing and diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py index a938831..4f0dd3e 100644 --- a/Lib/test/test_socketserver.py +++ b/Lib/test/test_socketserver.py @@ -241,6 +241,31 @@ class SocketServerTest(unittest.TestCase): # SocketServer.DatagramRequestHandler, # 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(): if imp.lock_held(): |