Skip to content

Commit

Permalink
pythongh-126688: Reinit import lock after fork
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
colesbury committed Nov 11, 2024
1 parent 20043d5 commit fc4bdfb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Include/internal/pycore_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ PyAPI_FUNC(int) _PyRecursiveMutex_IsLockedByCurrentThread(_PyRecursiveMutex *m);
PyAPI_FUNC(void) _PyRecursiveMutex_Lock(_PyRecursiveMutex *m);
PyAPI_FUNC(void) _PyRecursiveMutex_Unlock(_PyRecursiveMutex *m);

static inline void
_PyRecursiveMutex_at_fork_reinit(_PyRecursiveMutex *m)
{
memset(m, 0, sizeof(*m));
}


// A readers-writer (RW) lock. The lock supports multiple concurrent readers or
// a single writer. The lock is write-preferring: if a writer is waiting while
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when calling :func:`os.fork` on some operating systems,
including SerenityOS.
3 changes: 2 additions & 1 deletion Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,8 @@ PyOS_AfterFork_Child(void)
_PyEval_StartTheWorldAll(&_PyRuntime);
_PyThreadState_DeleteList(list);

_PyImport_ReleaseLock(tstate->interp);
// gh-126688: Reinit lock because thread id may differ in child process.
_PyRecursiveMutex_at_fork_reinit(&tstate->interp->imports.lock);

_PySignal_AfterFork();

Expand Down

0 comments on commit fc4bdfb

Please sign in to comment.