[Python-checkins] bpo-45021: Fix a hang in forked children (GH-28007)

gpshead webhook-mailer at python.org
Mon Sep 20 14:30:27 EDT 2021


https://github.com/python/cpython/commit/0bfa1106acfcddc03590e1f5d6789dbad3affe70
commit: 0bfa1106acfcddc03590e1f5d6789dbad3affe70
branch: main
author: nullptr <3621629+0x0L at users.noreply.github.com>
committer: gpshead <greg at krypto.org>
date: 2021-09-20T11:30:19-07:00
summary:

bpo-45021: Fix a hang in forked children (GH-28007)

_global_shutdown_lock should be reinitialized in forked children

files:
A Misc/NEWS.d/next/Library/2021-08-28-13-00-12.bpo-45021.rReeaj.rst
M Lib/concurrent/futures/thread.py
M Lib/test/test_concurrent_futures.py

diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py
index b7a2cac7f5701..51c942f51abd3 100644
--- a/Lib/concurrent/futures/thread.py
+++ b/Lib/concurrent/futures/thread.py
@@ -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):
diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py
index b0df12c66c901..84209ca2520b8 100644
--- a/Lib/test/test_concurrent_futures.py
+++ b/Lib/test/test_concurrent_futures.py
@@ -911,6 +911,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):
 
diff --git a/Misc/NEWS.d/next/Library/2021-08-28-13-00-12.bpo-45021.rReeaj.rst b/Misc/NEWS.d/next/Library/2021-08-28-13-00-12.bpo-45021.rReeaj.rst
new file mode 100644
index 0000000000000..54fd9109a9ae5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-08-28-13-00-12.bpo-45021.rReeaj.rst
@@ -0,0 +1 @@
+Fix a potential deadlock at shutdown of forked children when using :mod:`concurrent.futures` module
\ No newline at end of file



More information about the Python-checkins mailing list