There was an error while loading. Please reload this page.
1 parent 56e93c8 commit 5f11478Copy full SHA for 5f11478
2 files changed
Misc/NEWS.d/next/Core and Builtins/2023-02-24-17-59-39.gh-issue-102126.HTT8Vc.rst
@@ -0,0 +1 @@
1
+Fix deadlock at shutdown when clearing thread states if any finalizer tries to acquire the runtime head lock. Patch by Kumar Aditya.
Python/pystate.c
@@ -754,12 +754,19 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
754
_PyErr_Clear(tstate);
755
}
756
757
+ // Clear the current/main thread state last.
758
HEAD_LOCK(runtime);
- // XXX Clear the current/main thread state last.
759
- for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
+ PyThreadState *p = interp->threads.head;
760
+ HEAD_UNLOCK(runtime);
761
+ while (p != NULL) {
762
+ // See https://github.com/python/cpython/issues/102126
763
+ // Must be called without HEAD_LOCK held as it can deadlock
764
+ // if any finalizer tries to acquire that lock.
765
PyThreadState_Clear(p);
766
+ HEAD_LOCK(runtime);
767
+ p = p->next;
768
769
- HEAD_UNLOCK(runtime);
770
771
/* It is possible that any of the objects below have a finalizer
772
that runs Python code or otherwise relies on a thread state
0 commit comments