gh-126688: Support Fork Parent and Child With Different Thread ID #127121
+69
−10
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.
gh-126692 took care of one situation where we were assuming the thread ID did not change across fork. However, there is at least one other, in
PyOS_AfterFork_Child()
. This PR addresses that case along with making the parent's thread ID available to the child.Here's a breakdown:
_PyRuntimeState.os_fork.mutex
to allow only one instance of forking to happen at once (in part, to protect the rest of_PyRuntimeState.os_fork
)_PyRuntimeState.os_fork.parent.tid
to make the parent's thread ID available to the child_PyRecursiveMutex_at_fork_reinit()
, which makes use of the parent thread ID_PyRecursiveMutex_at_fork_reinit()
in_PyImport_ReInitLock()
I've also moved the
_PyImport_ReInitLock()
call fromPyOS_AfterFork_Child()
to_PyRuntimeState_ReInitThreads()
, to be more consistent about where we reinitialize locks after forking. If there are any objections, I don't mind dropping that part.One motivation I have here is that I'd like to use a
_PyRecursiveMutex
elsewhere and need to be able to correctly reinitialize after forking. That requires knowing the parent's thread ID, to decide if the forking thread held the lock or not. (This wasn't a problem for the import lock, which is a_PyRecursiveMutex
, since we always acquire and hold that lock while forking.)@gpshead, I'd be particularly interested in your thoughts on this.