Skip to content

Commit 0bfa110

Browse files
authored
bpo-45021: Fix a hang in forked children (GH-28007)
_global_shutdown_lock should be reinitialized in forked children
1 parent 9510e6f commit 0bfa110

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Lib/concurrent/futures/thread.py

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def _python_exit():
3636
# See bpo-39812 for context.
3737
threading._register_atexit(_python_exit)
3838

39+
# At fork, reinitialize the `_global_shutdown_lock` lock in the child process
40+
if hasattr(os, 'register_at_fork'):
41+
os.register_at_fork(before=_global_shutdown_lock.acquire,
42+
after_in_child=_global_shutdown_lock._at_fork_reinit,
43+
after_in_parent=_global_shutdown_lock.release)
44+
3945

4046
class _WorkItem(object):
4147
def __init__(self, future, fn, args, kwargs):

Lib/test/test_concurrent_futures.py

+14
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,20 @@ def test_idle_thread_reuse(self):
911911
self.assertEqual(len(executor._threads), 1)
912912
executor.shutdown(wait=True)
913913

914+
@unittest.skipUnless(hasattr(os, 'register_at_fork'), 'need os.register_at_fork')
915+
def test_hang_global_shutdown_lock(self):
916+
# bpo-45021: _global_shutdown_lock should be reinitialized in the child
917+
# process, otherwise it will never exit
918+
def submit(pool):
919+
pool.submit(submit, pool)
920+
921+
with futures.ThreadPoolExecutor(1) as pool:
922+
pool.submit(submit, pool)
923+
924+
for _ in range(50):
925+
with futures.ProcessPoolExecutor(1, mp_context=get_context('fork')) as workers:
926+
workers.submit(tuple)
927+
914928

915929
class ProcessPoolExecutorTest(ExecutorTest):
916930

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a potential deadlock at shutdown of forked children when using :mod:`concurrent.futures` module

0 commit comments

Comments
 (0)