There was an error while loading. Please reload this page.
1 parent c25b484 commit ca82bceCopy full SHA for ca82bce
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
@@ -288,11 +288,19 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
288
_PyErr_Clear(tstate);
289
}
290
291
+ // Clear the current/main thread state last.
292
HEAD_LOCK(runtime);
- for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
293
+ PyThreadState *p = interp->tstate_head;
294
+ HEAD_UNLOCK(runtime);
295
+ while (p != NULL) {
296
+ // See https://github.com/python/cpython/issues/102126
297
+ // Must be called without HEAD_LOCK held as it can deadlock
298
+ // if any finalizer tries to acquire that lock.
299
PyThreadState_Clear(p);
300
+ HEAD_LOCK(runtime);
301
+ p = p->next;
302
303
- HEAD_UNLOCK(runtime);
304
305
Py_CLEAR(interp->audit_hooks);
306
0 commit comments