[Python-checkins] [3.9] GH-102126: fix deadlock at shutdown when clearing thread states (GH-102222) (#102236)

ambv webhook-mailer at python.org
Tue Mar 28 04:55:45 EDT 2023


https://github.com/python/cpython/commit/7cb3a4474731f52c74b19dd3c99ca06e227dae3b
commit: 7cb3a4474731f52c74b19dd3c99ca06e227dae3b
branch: 3.9
author: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2023-03-28T10:55:36+02:00
summary:

[3.9] GH-102126: fix deadlock at shutdown when clearing thread states (GH-102222) (#102236)

(cherry picked from commit 5f11478ce7fda826d399530af4c5ca96c592f144)

files:
A Misc/NEWS.d/next/Core and Builtins/2023-02-24-17-59-39.gh-issue-102126.HTT8Vc.rst
M Python/pystate.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-02-24-17-59-39.gh-issue-102126.HTT8Vc.rst b/Misc/NEWS.d/next/Core and Builtins/2023-02-24-17-59-39.gh-issue-102126.HTT8Vc.rst
new file mode 100644
index 000000000000..68c43688c3df
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-02-24-17-59-39.gh-issue-102126.HTT8Vc.rst	
@@ -0,0 +1 @@
+Fix deadlock at shutdown when clearing thread states if any finalizer tries to acquire the runtime head lock. Patch by Kumar Aditya.
diff --git a/Python/pystate.c b/Python/pystate.c
index c3520c336a79..1cf9be1eabb7 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -288,11 +288,19 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
         _PyErr_Clear(tstate);
     }
 
+    // Clear the current/main thread state last.
     HEAD_LOCK(runtime);
-    for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
+    PyThreadState *p = interp->tstate_head;
+    HEAD_UNLOCK(runtime);
+    while (p != NULL) {
+        // See https://github.com/python/cpython/issues/102126
+        // Must be called without HEAD_LOCK held as it can deadlock
+        // if any finalizer tries to acquire that lock.
         PyThreadState_Clear(p);
+        HEAD_LOCK(runtime);
+        p = p->next;
+        HEAD_UNLOCK(runtime);
     }
-    HEAD_UNLOCK(runtime);
 
     Py_CLEAR(interp->audit_hooks);
 



More information about the Python-checkins mailing list