Skip to content

Commit

Permalink
bpo-39360: Ensure all workers exit when finalizing a multiprocessing.…
Browse files Browse the repository at this point in the history
…Pool
  • Loading branch information
isidentical committed Mar 15, 2020
1 parent 6672c16 commit 548d5fc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Lib/multiprocessing/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,6 @@ def close(self):
def terminate(self):
util.debug('terminating pool')
self._state = TERMINATE
self._worker_handler._state = TERMINATE
self._change_notifier.put(None)
self._terminate()

def join(self):
Expand Down Expand Up @@ -683,6 +681,8 @@ def _terminate_pool(cls, taskqueue, inqueue, outqueue, pool, change_notifier,
util.debug('finalizing pool')

worker_handler._state = TERMINATE
change_notifier.put(None)

task_handler._state = TERMINATE

util.debug('helping task handler/workers to finish')
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,21 @@ def test_pool_worker_lifetime_early_close(self):
for (j, res) in enumerate(results):
self.assertEqual(res.get(), sqr(j))

def test_pool_hang(self):
# tests cases against bpo-38744 and bpo-39360
cmd = '''if 1:
from multiprocessing import Pool
class A:
def init(self):
self.pool = Pool(processes=1)
def do_something(x):
return x1
problem = A()
problem.pool.map(do_something, [1,2,3])
'''
rc, out, err = test.support.script_helper.assert_python_ok('-c', cmd)
self.assertEqual(rc, 0)

#
# Test of creating a customized manager class
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure all workers exit when finalizing a :class:`multiprocessing.Pool`.
Patch by Batuhan Taskaya.

0 comments on commit 548d5fc

Please sign in to comment.