Skip to content

Commit 13fe8f9

Browse files
[3.13] gh-126688: Reinit import lock after fork (GH-126692) (GH-126765)
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 <[email protected]>
1 parent 3dab1ce commit 13fe8f9

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

Include/internal/pycore_import.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern int _PyImport_SetModuleString(const char *name, PyObject* module);
2121

2222
extern void _PyImport_AcquireLock(PyInterpreterState *interp);
2323
extern void _PyImport_ReleaseLock(PyInterpreterState *interp);
24+
extern void _PyImport_ReInitLock(PyInterpreterState *interp);
2425

2526
// This is used exclusively for the sys and builtins modules:
2627
extern int _PyImport_FixupBuiltin(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash when calling :func:`os.fork` on some operating systems,
2+
including SerenityOS.

Modules/posixmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ PyOS_AfterFork_Child(void)
678678
_PyEval_StartTheWorldAll(&_PyRuntime);
679679
_PyThreadState_DeleteList(list);
680680

681+
_PyImport_ReInitLock(tstate->interp);
681682
_PyImport_ReleaseLock(tstate->interp);
682683

683684
_PySignal_AfterFork();

Python/import.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ _PyImport_ReleaseLock(PyInterpreterState *interp)
120120
_PyRecursiveMutex_Unlock(&IMPORT_LOCK(interp));
121121
}
122122

123+
void
124+
_PyImport_ReInitLock(PyInterpreterState *interp)
125+
{
126+
// gh-126688: Thread id may change after fork() on some operating systems.
127+
IMPORT_LOCK(interp).thread = PyThread_get_thread_ident_ex();
128+
}
129+
123130

124131
/***************/
125132
/* sys.modules */

0 commit comments

Comments
 (0)