Skip to content

Commit

Permalink
[3.11] GH-102126: fix deadlock at shutdown when clearing thread state… (
Browse files Browse the repository at this point in the history
#102234)

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

(cherry picked from commit 5f11478)
  • Loading branch information
kumaraditya303 authored Feb 25, 2023
1 parent 5775863 commit 026faf2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 10 additions & 2 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,19 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
_PyErr_Clear(tstate);
}

// Clear the current/main thread state last.
HEAD_LOCK(runtime);
for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
PyThreadState *p = interp->threads.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);

Expand Down

0 comments on commit 026faf2

Please sign in to comment.