diff options
author | Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> | 2023-02-25 21:00:05 +0530 |
---|---|---|
committer | Michał Górny <mgorny@gentoo.org> | 2023-03-15 08:12:55 +0100 |
commit | 66506413c0e0d836036e58473f54bb972727e3ef (patch) | |
tree | 70de30cdca7dcef025ffc697b23bfe43404e76de | |
parent | gh-101857: Allow xattr detection on musl libc (#101858) (diff) | |
download | cpython-66506413c0e0d836036e58473f54bb972727e3ef.tar.gz cpython-66506413c0e0d836036e58473f54bb972727e3ef.tar.bz2 cpython-66506413c0e0d836036e58473f54bb972727e3ef.zip |
[3.11] GH-102126: fix deadlock at shutdown when clearing thread state… (#102234)gentoo-3.11.2_p2
[3.11] GH-102126: fix deadlock at shutdown when clearing thread states (GH-102222)
(cherry picked from commit 5f11478ce7fda826d399530af4c5ca96c592f144)
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2023-02-24-17-59-39.gh-issue-102126.HTT8Vc.rst | 1 | ||||
-rw-r--r-- | Python/pystate.c | 12 |
2 files changed, 11 insertions, 2 deletions
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-02-24-17-59-39.gh-issue-102126.HTT8Vc.rst b/Misc/NEWS.d/next/Core and Builtins/2023-02-24-17-59-39.gh-issue-102126.HTT8Vc.rst new file mode 100644 index 00000000000..68c43688c3d --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-02-24-17-59-39.gh-issue-102126.HTT8Vc.rst @@ -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. diff --git a/Python/pystate.c b/Python/pystate.c index c0d161f894c..dfca3f5fd71 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -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 *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); } - HEAD_UNLOCK(runtime); Py_CLEAR(interp->audit_hooks); |