summaryrefslogtreecommitdiffstats
path: root/Python/qsbr.c
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-05-31 18:19:38 (GMT)
committerGitHub <noreply@github.com>2024-05-31 18:19:38 (GMT)
commita7e81fdfc175bd9cf82dfd8f1e1853cb942bf0f0 (patch)
treefa6ecd452e4f0f4ffc9aea8b0a18e62db618d094 /Python/qsbr.c
parent5e8396e6841e3aae8cc10117583e596ff294d7e0 (diff)
downloadcpython-a7e81fdfc175bd9cf82dfd8f1e1853cb942bf0f0.zip
cpython-a7e81fdfc175bd9cf82dfd8f1e1853cb942bf0f0.tar.gz
cpython-a7e81fdfc175bd9cf82dfd8f1e1853cb942bf0f0.tar.bz2
[3.13] gh-119369: Fix deadlock during thread exit in free-threaded build (GH-119528) (#119868)
Release the GIL before calling `_Py_qsbr_unregister`. The deadlock could occur when the GIL was enabled at runtime. The `_Py_qsbr_unregister` call might block while holding the GIL because the thread state was not active, but the GIL was still held. (cherry picked from commit 078b8c8cf2bf68f7484cc4d2e3dd74b6fab55664) Co-authored-by: Sam Gross <colesbury@gmail.com>
Diffstat (limited to 'Python/qsbr.c')
-rw-r--r--Python/qsbr.c5
1 files changed, 5 insertions, 0 deletions
diff --git a/Python/qsbr.c b/Python/qsbr.c
index 1e02ff9..9cbce90 100644
--- a/Python/qsbr.c
+++ b/Python/qsbr.c
@@ -236,6 +236,11 @@ _Py_qsbr_unregister(PyThreadState *tstate)
struct _qsbr_shared *shared = &tstate->interp->qsbr;
struct _PyThreadStateImpl *tstate_imp = (_PyThreadStateImpl*) tstate;
+ // gh-119369: GIL must be released (if held) to prevent deadlocks, because
+ // we might not have an active tstate, which means taht blocking on PyMutex
+ // locks will not implicitly release the GIL.
+ assert(!tstate->_status.holds_gil);
+
PyMutex_Lock(&shared->mutex);
// NOTE: we must load (or reload) the thread state's qbsr inside the mutex
// because the array may have been resized (changing tstate->qsbr) while