Skip to content

Commit 9f17cba

Browse files
committed
some changes
1 parent 207d611 commit 9f17cba

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

Lib/multiprocessing/pool.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,8 @@ def close(self):
651651
def terminate(self):
652652
util.debug('terminating pool')
653653
self._state = TERMINATE
654+
self._worker_handler._state = TERMINATE
655+
self._change_notifier.put(None)
654656
self._terminate()
655657

656658
def join(self):
@@ -680,8 +682,14 @@ def _terminate_pool(cls, taskqueue, inqueue, outqueue, pool, change_notifier,
680682
# this is guaranteed to only be called once
681683
util.debug('finalizing pool')
682684

683-
worker_handler._state = TERMINATE
684-
change_notifier.put(None)
685+
# Explicitly do the cleanup here if it didn't come from terminate()
686+
# (required for if the queue will block, if it is already closed)
687+
if worker_handler._state != TERMINATE:
688+
# Notify that the worker_handler state has been changed so the
689+
# _handle_workers loop can be unblocked (and exited) in order to
690+
# send the finalization sentinel all the workers.
691+
worker_handler._state = TERMINATE
692+
change_notifier.put(None)
685693

686694
task_handler._state = TERMINATE
687695

Lib/test/_test_multiprocessing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2780,7 +2780,7 @@ def test_pool_worker_lifetime_early_close(self):
27802780
for (j, res) in enumerate(results):
27812781
self.assertEqual(res.get(), sqr(j))
27822782

2783-
def test_pool_hang(self):
2783+
def test_worker_finalization_via_atexit_handler_of_multiprocessing(self):
27842784
# tests cases against bpo-38744 and bpo-39360
27852785
cmd = '''if 1:
27862786
from multiprocessing import Pool
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Ensure all workers exit when finalizing a :class:`multiprocessing.Pool` implicitly via the module finalization
22
handlers of multiprocessing. This fixes a deadlock situation that can be experienced when the Pool is not
3-
properly finalized via the context manager or a call to ``multiprocessing.Pool.terminate``. Patch by Batuhan Taskaya.
3+
properly finalized via the context manager or a call to ``multiprocessing.Pool.terminate``. Patch by Batuhan Taskaya
4+
and Pablo Galindo.

0 commit comments

Comments
 (0)