From e79b6b0a9621a3099835fbbbb5088382695eba07 Mon Sep 17 00:00:00 2001 From: ChuBoning <102216855+ChuBoning@users.noreply.github.com> Date: Thu, 23 Nov 2023 20:48:04 +0800 Subject: [PATCH 1/4] Fix HEAD_LOCK deadlock in child process after fork HEAD_LOCK is called from _PyEval_ReInitThreads->_PyThreadState_DeleteExcept before _PyRuntimeState_ReInitThreads reinit runtime->interpreters.mutex which might be locked before fork. --- Modules/posixmodule.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 7c367b9d2e9818..d634f7bc5f1c55 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -597,6 +597,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); @@ -616,11 +621,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; From f09883cb168fa3b536df712222cd0bbdd1e59121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 4 Sep 2024 18:20:15 +0200 Subject: [PATCH 2/4] Add Blurb --- .../2024-09-04-18-20-11.gh-issue-112275.W_iMiB.rst | 2 ++ 1 file changed, 2 insertions(+) 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..32e25dcbe63a34 --- /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,2 @@ +A deadlock in ``pystate.c`` at fork is now fixed. Patch by ChuBoning based +on previous Python 3.12 fix by Victor Stinner from GH-20596. From 593ea9056eb1b28170e9cc5e726a0415abf57fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 4 Sep 2024 18:21:27 +0200 Subject: [PATCH 3/4] Reword Blurb --- .../2024-09-04-18-20-11.gh-issue-112275.W_iMiB.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 index 32e25dcbe63a34..596c3dbe4ace1a 100644 --- 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 @@ -1,2 +1,3 @@ -A deadlock in ``pystate.c`` at fork is now fixed. Patch by ChuBoning based -on previous Python 3.12 fix by Victor Stinner from GH-20596. +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 from GH-20596. From 7b805ae65887f9ca598a57d1b40580247b9d36bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 4 Sep 2024 18:33:01 +0200 Subject: [PATCH 4/4] Further reword Blurb --- .../2024-09-04-18-20-11.gh-issue-112275.W_iMiB.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 596c3dbe4ace1a..d663be1867ed3d 100644 --- 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 @@ -1,3 +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 from GH-20596. +fix by Victor Stinner.