Skip to content

[3.9] bpo-45021: Fix a hang in forked children (GH-28007) #28481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/concurrent/futures/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def _python_exit():
# See bpo-39812 for context.
threading._register_atexit(_python_exit)

# At fork, reinitialize the `_global_shutdown_lock` lock in the child process
if hasattr(os, 'register_at_fork'):
os.register_at_fork(before=_global_shutdown_lock.acquire,
after_in_child=_global_shutdown_lock._at_fork_reinit,
after_in_parent=_global_shutdown_lock.release)


class _WorkItem(object):
def __init__(self, future, fn, args, kwargs):
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_concurrent_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,20 @@ def test_idle_thread_reuse(self):
self.assertEqual(len(executor._threads), 1)
executor.shutdown(wait=True)

@unittest.skipUnless(hasattr(os, 'register_at_fork'), 'need os.register_at_fork')
def test_hang_global_shutdown_lock(self):
# bpo-45021: _global_shutdown_lock should be reinitialized in the child
# process, otherwise it will never exit
def submit(pool):
pool.submit(submit, pool)

with futures.ThreadPoolExecutor(1) as pool:
pool.submit(submit, pool)

for _ in range(50):
with futures.ProcessPoolExecutor(1, mp_context=get_context('fork')) as workers:
workers.submit(tuple)


class ProcessPoolExecutorTest(ExecutorTest):

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