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 8fa4dc4 commit 5e86cca
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Include/internal/pycore_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ extern PyLockStatus _PyRecursiveMutex_LockTimed(_PyRecursiveMutex *m, PyTime_t t
PyAPI_FUNC(void) _PyRecursiveMutex_Unlock(_PyRecursiveMutex *m);
extern int _PyRecursiveMutex_TryUnlock(_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
// the lock is read-locked then, new readers will be blocked. This avoids
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 5e86cca

Please sign in to comment.