]> granicus.if.org Git - python/commitdiff
Merged revisions 80484 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 25 Apr 2010 21:55:45 +0000 (21:55 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 25 Apr 2010 21:55:45 +0000 (21:55 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r80484 | antoine.pitrou | 2010-04-25 23:40:32 +0200 (dim., 25 avril 2010) | 6 lines

  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.
........

Lib/SocketServer.py
Lib/test/test_socketserver.py
Misc/NEWS

index f01cb5f2ccbf58c51357b6da846a4be68c6a6bb1..7040738e761fcac589b5c0b7d2a1a0afc264a15a 100644 (file)
@@ -197,7 +197,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.
@@ -214,17 +214,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.
@@ -233,7 +235,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
index b8f664fd67f0399ce2555c89c01bdd81d1dc654b..27e1d3fa36a30cbe427aa9b7ae46404ab046a08b 100644 (file)
@@ -243,6 +243,30 @@ class SocketServerTest(unittest.TestCase):
     #                             SocketServer.DatagramRequestHandler,
     #                             self.dgram_examine)
 
+    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():
index e50af64fa850dabb16c9b0cc066c2191248bf6ac..fdec90d75ce8839560fcd63be7b532bef1082b1f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -33,6 +33,11 @@ Core and Builtins
 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
   indefinitely if the other end didn't respond.