8000 GH-102126: fix deadlock at shutdown when clearing thread states (#102… · python/cpython@5f11478 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f11478

Browse files
GH-102126: fix deadlock at shutdown when clearing thread states (#102222)
1 parent 56e93c8 commit 5f11478

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,12 +754,19 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
754754
_PyErr_Clear(tstate);
755755
}
756756

757+
// Clear the current/main thread state last.
757758
HEAD_LOCK(runtime);
758-
// XXX Clear the current/main thread state last.
759-
for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
759+
PyThreadState *p = interp->threads.head;
760+
HEAD_UNLOCK(runtime);
761+
8AB4 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.
760765
PyThreadState_Clear(p);
766+
HEAD_LOCK(runtime);
767+
p = p->next;
768+
HEAD_UNLOCK(runtime);
761769
}
762-
HEAD_UNLOCK(runtime);
763770

764771
/* It is possible that any of the objects below have a finalizer
765772
that runs Python code or otherwise relies on a thread state

0 commit comments

Comments
 (0)
0