-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
gh-126688: Reinit import lock after fork #126692
Conversation
The PyMutex implementation supports unlocking after fork because we clear the list of watiers in parking_lot.c. This doesn't work as well for _PyRecursiveMutex because on some systems, such as SerenityOS, the thread id is not preserved across fork().
Thanks @oskar-skog. My first attempt had a bug and I also had to merge "main" back into the PR branch to fix some other test issues. Sorry if that complicated applying the diff to 3.13. You may already know this, but you can get the PR as a diff with a URL like https://github.com/python/cpython/pull/126692.patch. That's sometimes easier to apply than diffing between two commits involving merges. |
No, it actually made it simpler to patch, and I didn't know you could just download a PR as a patch. |
Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
The PyMutex implementation supports unlocking after fork because we clear the list of waiters in parking_lot.c. This doesn't work as well for _PyRecursiveMutex because on some systems, such as SerenityOS, the thread id is not preserved across fork(). (cherry picked from commit 5610860) Co-authored-by: Sam Gross <colesbury@gmail.com>
GH-126765 is a backport of this pull request to the 3.13 branch. |
The PyMutex implementation supports unlocking after fork because we clear the list of waiters in parking_lot.c. This doesn't work as well for _PyRecursiveMutex because on some systems, such as SerenityOS, the thread id is not preserved across fork(). (cherry picked from commit 5610860) Co-authored-by: Sam Gross <colesbury@gmail.com>
// gh-126688: Thread id may change after fork() on some operating systems. | ||
IMPORT_LOCK(interp).thread = PyThread_get_thread_ident_ex(); |
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.
Wouldn't this lead to deadlocks if some other thread happened to be holding the import lock when we forked? It seems like the child process needs to know the thread ID from which it forked.
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.
We acquire the import lock before forking so that no other thread holds it:
Lines 618 to 624 in 09c240f
void | |
PyOS_BeforeFork(void) | |
{ | |
PyInterpreterState *interp = _PyInterpreterState_GET(); | |
run_at_forkers(interp->before_forkers, 1); | |
_PyImport_AcquireLock(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.
Hmm, looks like we always acquire the import lock in PyOS_BeforeFork()
. It should be fine then.
The
PyMutex
implementation supports unlocking after fork because we clear the list of watiers in parking_lot.c. This doesn't work as well for_PyRecursiveMutex
because on some systems, such as SerenityOS, the thread id is not preserved acrossfork()
.