gh-148318: Fix multiprocessing exitcode=None after join() in multithreaded programs#150360
Open
jlaportebot wants to merge 1 commit into
Open
gh-148318: Fix multiprocessing exitcode=None after join() in multithreaded programs#150360jlaportebot wants to merge 1 commit into
jlaportebot wants to merge 1 commit into
Conversation
…pythongh-148318) When multiple threads join() fork-based child processes concurrently, os.waitpid() can be called by one thread for a PID that another thread is also waiting on. Since waitpid() reaps the child process-wide, the second thread's call raises OSError(ECHILD), leaving Process.exitcode stuck at None. Add a class-level threading.Lock around the os.waitpid() call in Popen.poll() so that only one thread performs waitpid at a time, with a double-check of self.returncode inside the lock to avoid missing a result set by another thread. Fixes pythongh-148318.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When multiple threads concurrently
join()fork-based child processes,os.waitpid()can be called by one thread for a PID that another thread is also waiting on. Sincewaitpid()reaps the child process-wide, the second thread's call raisesOSError(ECHILD)(errno=10), leavingProcess.exitcodestuck atNone.Fixes #148318.
Root Cause
os.waitpid()is a process-level operation — it reaps any matching child, regardless of which thread called it. When two threads simultaneously calljoin()on different processes, the following race can occur:poll()→os.waitpid(pid_A)→ succeeds, reaps child Apoll()→os.waitpid(pid_B)→ but the kernel may return the status for child A (already reaped by Thread A) → raisesOSError(ECHILD)poll()returnsNone(from theexcept OSErrorbranch), soself.returncodestaysNonejoin()returns, butexitcoderemainsNoneFix
Add a class-level
threading.Lockaround theos.waitpid()call inPopen.poll(). Under the lock, we double-checkself.returncodein case another thread already set it while we waited for the lock.The lock is placed only around the
waitpidcall and the immediatereturncodecheck, not around thewait()method's sentinel-wait, so it does not add significant contention.Reproducer
Impact
Only affects
popen_fork.Popen(the "fork" start method on Unix). The "spawn" methods use different wait mechanisms and are not affected.The lock is a class attribute, so all
Popeninstances share it — this ensures cross-instance synchronization. The lock is cheap (uncontended fast path is ~20ns on Linux) and only held for the duration of a singlewaitpidsyscall.