8000 [3.11] GH-102126: fix deadlock at shutdown when clearing thread state… · python/cpython@026faf2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 026faf2

Browse files
[3.11] GH-102126: fix deadlock at shutdown when clearing thread state… (#102234)
[3.11] GH-102126: fix deadlock at shutdown when clearing thread states (GH-102222) (cherry picked from commit 5f11478)
1 parent 5775863 commit 026faf2

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,19 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
396396
_PyErr_Clear(tstate);
397397
}
398398

399+
// Clear the current/main thread state last.
399400
HEAD_LOCK(runtime);
400-
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.
401407
PyThreadState_Clear(p);
408+
HEAD_LOCK(runtime);
409+
p = p->next;
410+
HEAD_UNLOCK(runtime);
402411
}
403-
HEAD_UNLOCK(runtime);
404412

405413
Py_CLEAR(interp->audit_hooks);
406414

0 commit comments

Comments
 (0)
0