https://github.com/python/cpython/commit/13fe8f946e60f28a7ae3b1e540973986f4eb357c
commit: 13fe8f946e60f28a7ae3b1e540973986f4eb357c
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: colesbury <[email protected]>
date: 2024-11-12T22:00:42Z
summary:

[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 5610860840aa71b186fc5639211dd268b817d65f)

Co-authored-by: Sam Gross <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2024-11-11-17-02-48.gh-issue-126688.QiOXUi.rst
M Include/internal/pycore_import.h
M Modules/posixmodule.c
M Python/import.c

diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h
index 3806e0d3cd0a95..55029abdd31b5a 100644
--- a/Include/internal/pycore_import.h
+++ b/Include/internal/pycore_import.h
@@ -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
index 00000000000000..30aa5722f0ea02
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-11-17-02-48.gh-issue-126688.QiOXUi.rst
@@ -0,0 +1,2 @@
+Fix a crash when calling :func:`os.fork` on some operating systems,
+including SerenityOS.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 5edc9dcc7ff3e5..a09305c91ecbba 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -678,6 +678,7 @@ PyOS_AfterFork_Child(void)
     _PyEval_StartTheWorldAll(&_PyRuntime);
     _PyThreadState_DeleteList(list);
 
+    _PyImport_ReInitLock(tstate->interp);
     _PyImport_ReleaseLock(tstate->interp);
 
     _PySignal_AfterFork();
diff --git a/Python/import.c b/Python/import.c
index 1541d5a55b82ec..0f08e6dfc247bd 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -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 */

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to