Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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);

Expand Down