From e742f995efc5c2d9e3206b0646b51a0def2f846c Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Thu, 5 Sep 2024 11:14:03 +0200 Subject: [PATCH] [3.8] gh-112275: Fix HEAD_LOCK deadlock in child process after fork (GH-112336) (GH-123688) (#123713) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit HEAD_LOCK is called from _PyEval_ReInitThreads->_PyThreadState_DeleteExcept before _PyRuntimeState_ReInitThreads reinit runtime->interpreters.mutex which might be locked before fork. (cherry picked from commit 522799a05e3e820339718151ac055af6d864d463) (cherry picked from commit 0152431f179e85f582a4c393aa3b99d3918a1109) Co-authored-by: Ɓukasz Langa Co-authored-by: ChuBoning <102216855+ChuBoning@users.noreply.github.com> --- .../2024-09-04-18-20-11.gh-issue-112275.W_iMiB.rst | 3 +++ Modules/posixmodule.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-09-04-18-20-11.gh-issue-112275.W_iMiB.rst 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 index 00000000000000..d663be1867ed3d --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-09-04-18-20-11.gh-issue-112275.W_iMiB.rst @@ -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. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 2ec8458cb51bfb..0059be2e9a0b3a 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -472,10 +472,10 @@ PyOS_AfterFork_Child(void) { _PyRuntimeState *runtime = &_PyRuntime; _PyGILState_Reinit(runtime); + _PyRuntimeState_ReInitThreads(runtime); _PyEval_ReInitThreads(runtime); _PyImport_ReInitLock(); _PySignal_AfterFork(); - _PyRuntimeState_ReInitThreads(runtime); _PyInterpreterState_DeleteExceptMain(runtime); run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);