8000 gh-126688: Reinit import lock after fork (#126692) · python/cpython@5610860 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5610860

Browse files
authored
gh-126688: Reinit import lock after fork (#126692)
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().
1 parent bf224bd commit 5610860

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
@@ -122,6 +122,13 @@ _PyImport_ReleaseLock(PyInterpreterState *interp)
122122
_PyRecursiveMutex_Unlock(&IMPORT_LOCK(interp));
123123
}
124124

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

126133
/***************/
127134
/* sys.modules */

0 commit comments

Comments
 (0)
0