]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11]GH-112275: Fix HEAD_LOCK deadlock in child process after fork (#112336)
authorChuBoning <102216855+ChuBoning@users.noreply.github.com>
Wed, 4 Sep 2024 17:14:59 +0000 (01:14 +0800)
committerGitHub <noreply@github.com>
Wed, 4 Sep 2024 17:14:59 +0000 (19:14 +0200)
HEAD_LOCK is called from _PyEval_ReInitThreads->_PyThreadState_DeleteExcept before _PyRuntimeState_ReInitThreads reinit runtime->interpreters.mutex which might be locked before fork.

Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Misc/NEWS.d/next/Core_and_Builtins/2024-09-04-18-20-11.gh-issue-112275.W_iMiB.rst [new file with mode: 0644]
Modules/posixmodule.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-09-04-18-20-11.gh-issue-112275.W_iMiB.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-09-04-18-20-11.gh-issue-112275.W_iMiB.rst
new file mode 100644 (file)
index 0000000..d663be1
--- /dev/null
@@ -0,0 +1,3 @@
+A deadlock involving ``pystate.c``'s ``HEAD_LOCK`` in ``posixmodule.c``
+at fork is now fixed. Patch by ChuBoning based on previous Python 3.12
+fix by Victor Stinner.
index 46859dab35c50089cbb2c6b24bac25e70525663c..6fb2fc8ce250c1c9db44f900676634b7fcbb8889 100644 (file)
@@ -603,6 +603,11 @@ PyOS_AfterFork_Child(void)
         goto fatal_error;
     }
 
+    status = _PyRuntimeState_ReInitThreads(runtime);
+    if (_PyStatus_EXCEPTION(status)) {
+        goto fatal_error;
+    }
+
     PyThreadState *tstate = _PyThreadState_GET();
     _Py_EnsureTstateNotNULL(tstate);
 
@@ -622,11 +627,6 @@ PyOS_AfterFork_Child(void)
 
     _PySignal_AfterFork();
 
-    status = _PyRuntimeState_ReInitThreads(runtime);
-    if (_PyStatus_EXCEPTION(status)) {
-        goto fatal_error;
-    }
-
     status = _PyInterpreterState_DeleteExceptMain(runtime);
     if (_PyStatus_EXCEPTION(status)) {
         goto fatal_error;