From 4eb4ae697f6aafd3996c229dcde3af0f4b0ef5c9 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 19 Aug 2022 06:42:25 +0000 Subject: [PATCH 1/2] fix deadlock --- Python/pystate.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Python/pystate.c b/Python/pystate.c index 642d680ba20b1f..4ada6054109fec 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -829,11 +829,16 @@ new_threadstate(PyInterpreterState *interp) // Every valid interpreter must have at least one thread. assert(id > 1); assert(old_head->prev == NULL); - + // Unlock before allocating memory. + HEAD_UNLOCK(runtime); tstate = alloc_threadstate(); + HEAD_LOCK(runtime); if (tstate == NULL) { goto error; } + // Read interp->threads.head again as another thread could + // have created a thread state while we were allocating memory. + old_head = interp->threads.head; // Set to _PyThreadState_INIT. memcpy(tstate, &initial._main_interpreter._initial_thread, From e0b93d6e3025c3b7ed83e4f0dfcf4d65dc5300ec Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 19 Aug 2022 06:51:19 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-08-19-06-51-17.gh-issue-96071.mVgPAo.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-08-19-06-51-17.gh-issue-96071.mVgPAo.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-08-19-06-51-17.gh-issue-96071.mVgPAo.rst b/Misc/NEWS.d/next/Core and Builtins/2022-08-19-06-51-17.gh-issue-96071.mVgPAo.rst new file mode 100644 index 00000000000000..37653ffac12418 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-08-19-06-51-17.gh-issue-96071.mVgPAo.rst @@ -0,0 +1 @@ +Fix a deadlock in :c:func:`PyGILState_Ensure` when allocating new thread state. Patch by Kumar Aditya.