[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 5f11478ce7)
This commit is contained in:
Kumar Aditya 2023-02-25 21:00:05 +05:30 committed by GitHub
parent 5775863e9d
commit 026faf20cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View file

@ -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_Clear(p);
}
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);
}
Py_CLEAR(interp->audit_hooks);