]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-126688: Reinit import lock after fork (#126692)
authorSam Gross <colesbury@gmail.com>
Tue, 12 Nov 2024 20:53:58 +0000 (20:53 +0000)
committerGitHub <noreply@github.com>
Tue, 12 Nov 2024 20:53:58 +0000 (15:53 -0500)
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().

Include/internal/pycore_import.h
Misc/NEWS.d/next/Core_and_Builtins/2024-11-11-17-02-48.gh-issue-126688.QiOXUi.rst [new file with mode: 0644]
Modules/posixmodule.c
Python/import.c

index 290ba95e1a0ad7c0274021ad7071a81082e397ac..318c712bdfa17494fbacab5bbf599a998238ee69 100644 (file)
@@ -21,6 +21,7 @@ extern int _PyImport_SetModuleString(const char *name, PyObject* module);
 
 extern void _PyImport_AcquireLock(PyInterpreterState *interp);
 extern void _PyImport_ReleaseLock(PyInterpreterState *interp);
+extern void _PyImport_ReInitLock(PyInterpreterState *interp);
 
 // This is used exclusively for the sys and builtins modules:
 extern int _PyImport_FixupBuiltin(
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-11-17-02-48.gh-issue-126688.QiOXUi.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-11-17-02-48.gh-issue-126688.QiOXUi.rst
new file mode 100644 (file)
index 0000000..30aa572
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a crash when calling :func:`os.fork` on some operating systems,
+including SerenityOS.
index 1ce2baecb8a964918d0947fc12b725bddf005c53..da7399de86f21377e1d9c06d88857597206428ee 100644 (file)
@@ -678,6 +678,7 @@ PyOS_AfterFork_Child(void)
     _PyEval_StartTheWorldAll(&_PyRuntime);
     _PyThreadState_DeleteList(list);
 
+    _PyImport_ReInitLock(tstate->interp);
     _PyImport_ReleaseLock(tstate->interp);
 
     _PySignal_AfterFork();
index 29bd8bf68ff5e1e73d76370391333c28c0bbda88..09fe95fa1fb64783fa55640a468412e8b8da55dd 100644 (file)
@@ -122,6 +122,13 @@ _PyImport_ReleaseLock(PyInterpreterState *interp)
     _PyRecursiveMutex_Unlock(&IMPORT_LOCK(interp));
 }
 
+void
+_PyImport_ReInitLock(PyInterpreterState *interp)
+{
+    // gh-126688: Thread id may change after fork() on some operating systems.
+    IMPORT_LOCK(interp).thread = PyThread_get_thread_ident_ex();
+}
+
 
 /***************/
 /* sys.modules */