[Python-checkins] cpython (2.7): Issue #8428: Fix a race condition in multiprocessing.Pool when terminating

antoine.pitrou python-checkins at python.org
Mon Apr 11 00:28:24 CEST 2011


http://hg.python.org/cpython/rev/dfc61dc14f59
changeset:   69239:dfc61dc14f59
branch:      2.7
parent:      69228:3630bc3d5a88
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Mon Apr 11 00:26:42 2011 +0200
summary:
  Issue #8428: Fix a race condition in multiprocessing.Pool when terminating
worker processes: new processes would be spawned while the pool is being
shut down.  Patch by Charles-François Natali.

files:
  Lib/multiprocessing/pool.py |  9 +++++++--
  Misc/NEWS                   |  4 ++++
  2 files changed, 11 insertions(+), 2 deletions(-)


diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py
--- a/Lib/multiprocessing/pool.py
+++ b/Lib/multiprocessing/pool.py
@@ -295,6 +295,8 @@
         while pool._worker_handler._state == RUN and pool._state == RUN:
             pool._maintain_pool()
             time.sleep(0.1)
+        # send sentinel to stop workers
+        pool._taskqueue.put(None)
         debug('worker handler exiting')
 
     @staticmethod
@@ -413,7 +415,6 @@
         if self._state == RUN:
             self._state = CLOSE
             self._worker_handler._state = CLOSE
-            self._taskqueue.put(None)
 
     def terminate(self):
         debug('terminating pool')
@@ -447,7 +448,6 @@
 
         worker_handler._state = TERMINATE
         task_handler._state = TERMINATE
-        taskqueue.put(None)                 # sentinel
 
         debug('helping task handler/workers to finish')
         cls._help_stuff_finish(inqueue, task_handler, len(pool))
@@ -457,6 +457,11 @@
         result_handler._state = TERMINATE
         outqueue.put(None)                  # sentinel
 
+        # We must wait for the worker handler to exit before terminating
+        # workers because we don't want workers to be restarted behind our back.
+        debug('joining worker handler')
+        worker_handler.join()
+
         # Terminate workers which haven't already finished.
         if pool and hasattr(pool[0], 'terminate'):
             debug('terminating workers')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -51,6 +51,10 @@
 Library
 -------
 
+- Issue #8428: Fix a race condition in multiprocessing.Pool when terminating
+  worker processes: new processes would be spawned while the pool is being
+  shut down.  Patch by Charles-François Natali.
+
 - Issue #7311: fix HTMLParser to accept non-ASCII attribute values.
 
 - Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list