]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-126688: Reinit import lock after fork (GH-126692) (GH-126765)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 12 Nov 2024 22:00:42 +0000 (23:00 +0100)
committerGitHub <noreply@github.com>
Tue, 12 Nov 2024 22:00:42 +0000 (22:00 +0000)
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 5610860840aa71b186fc5639211dd268b817d65f)

Co-authored-by: Sam Gross <colesbury@gmail.com>
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 3806e0d3cd0a95bd9ddc6557212ded290772cc32..55029abdd31b5a479f16a048d402c44c2d7affca 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 5edc9dcc7ff3e596367d92038a41a5cd17546ed6..a09305c91ecbba139d0ef5314d77971277bc633b 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 1541d5a55b82ec06be23e2478266a50cf7f4740f..0f08e6dfc247bdef2b5afc8ace3fd50d7cbcc67d 100644 (file)
@@ -120,6 +120,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 */