-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-126688: Support Fork Parent and Child With Different Thread ID #127121
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
base: main
Are you sure you want to change the base?
gh-126688: Support Fork Parent and Child With Different Thread ID #127121
Conversation
@@ -694,6 +713,9 @@ PyOS_AfterFork_Child(void) | |||
goto fatal_error; | |||
} | |||
|
|||
_PyRuntime.os_fork.parent = (struct _os_fork_parent){0}; | |||
PyMutex_Unlock(&_PyRuntime.os_fork.mutex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might need to be done before _PyThreadState_DeleteList above to handle the very silly case of someone forking from a destructor and deadlocking? ("why is that even reasonable to support?" is valid...)
@@ -678,7 +698,6 @@ PyOS_AfterFork_Child(void) | |||
_PyEval_StartTheWorldAll(&_PyRuntime); | |||
_PyThreadState_DeleteList(list); | |||
|
|||
_PyImport_ReInitLock(tstate->interp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a comment mentioning which call above is now responsible for doing this.
@@ -515,6 +516,7 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime) | |||
for (PyInterpreterState *interp = runtime->interpreters.head; | |||
interp != NULL; interp = interp->next) | |||
{ | |||
_PyImport_ReInitLock(interp, parent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this only being done when the GIL is disabled instead of always as it was in the past?
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.