There was an error while loading. Please reload this page.
1 parent 5775863 commit 026faf2Copy full SHA for 026faf2
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
@@ -396,11 +396,19 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
396
_PyErr_Clear(tstate);
397
}
398
399
+ // Clear the current/main thread state last.
400
HEAD_LOCK(runtime);
- for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
401
+ PyThreadState *p = interp->threads.head;
402
+ HEAD_UNLOCK(runtime);
403
+ while (p != NULL) {
404
+ // See https://github.com/python/cpython/issues/102126
405
+ // Must be called without HEAD_LOCK held as it can deadlock
406
+ // if any finalizer tries to acquire that lock.
407
PyThreadState_Clear(p);
408
+ HEAD_LOCK(runtime);
409
+ p = p->next;
410
411
- HEAD_UNLOCK(runtime);
412
413
Py_CLEAR(interp->audit_hooks);
414
0 commit comments