summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>2023-03-28 08:55:36 (GMT)
committerGitHub <noreply@github.com>2023-03-28 08:55:36 (GMT)
commit7cb3a4474731f52c74b19dd3c99ca06e227dae3b (patch)
tree791e6c9e71c26f0740dc694b6754ce3a209723c2 /Python
parentb5a94301a281854d3c746bc0a0167a7d6aaf91f5 (diff)
downloadcpython-7cb3a4474731f52c74b19dd3c99ca06e227dae3b.zip
cpython-7cb3a4474731f52c74b19dd3c99ca06e227dae3b.tar.gz
cpython-7cb3a4474731f52c74b19dd3c99ca06e227dae3b.tar.bz2
[3.9] GH-102126: fix deadlock at shutdown when clearing thread states (GH-102222) (#102236)
(cherry picked from commit 5f11478ce7fda826d399530af4c5ca96c592f144)
Diffstat (limited to 'Python')
-rw-r--r--Python/pystate.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index c3520c3..1cf9be1 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);