mirror of
https://github.com/python/cpython.git
synced 2025-08-19 16:20:59 +00:00
[3.9] bpo-45021: Fix a hang in forked children (GH-28007) (GH-28481)
_global_shutdown_lock should be reinitialized in forked children
(cherry picked from commit 0bfa1106ac
)
Co-authored-by: nullptr <3621629+0x0L@users.noreply.github.com>
Automerge-Triggered-By: GH:gpshead
This commit is contained in:
parent
04dea46dde
commit
b06c3b3647
3 changed files with 21 additions and 0 deletions
|
@ -36,6 +36,12 @@ def _python_exit():
|
||||||
# See bpo-39812 for context.
|
# See bpo-39812 for context.
|
||||||
threading._register_atexit(_python_exit)
|
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):
|
class _WorkItem(object):
|
||||||
def __init__(self, future, fn, args, kwargs):
|
def __init__(self, future, fn, args, kwargs):
|
||||||
|
|
|
@ -896,6 +896,20 @@ class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest, BaseTestCase):
|
||||||
self.assertEqual(len(executor._threads), 1)
|
self.assertEqual(len(executor._threads), 1)
|
||||||
executor.shutdown(wait=True)
|
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):
|
class ProcessPoolExecutorTest(ExecutorTest):
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix a potential deadlock at shutdown of forked children when using :mod:`concurrent.futures` module
|
Loading…
Add table
Add a link
Reference in a new issue