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

Skip to content

Commit 6c2e052

Browse files
[3.10] GH-102126: fix deadlock at shutdown when clearing thread state… (#102235)
[3.10] GH-102126: fix deadlock at shutdown when clearing thread states (GH-102222). (cherry picked from commit 5f11478)
1 parent 3d88b4e commit 6c2e052

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
@@ -293,11 +293,19 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
293293
_PyErr_Clear(tstate);
294294
}
295295

296+
// Clear the current/main thread state last.
296297
HEAD_LOCK(runtime);
297-
for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
298+
PyThreadState *p = interp->tstate_head;
299+
HEAD_UNLOCK(runtime);
300+
while (p != NULL) {
301+
// See https://github.com/python/cpython/issues/102126
302+
// Must be called without HEAD_LOCK held as it can deadlock
303+
// if any finalizer tries to acquire that lock.
298304
PyThreadState_Clear(p);
305+
HEAD_LOCK(runtime);
306+
p = p->next;
307+
HEAD_UNLOCK(runtime);
299308
}
300-
HEAD_UNLOCK(runtime);
301309

302310
Py_CLEAR(interp->audit_hooks);
303311

0 commit comments

Comments
 (0)
0