Skip to content

Commit 078b8c8

Browse files
authored
gh-119369: Fix deadlock during thread exit in free-threaded build (#119528)
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.
1 parent 64ff1e2 commit 078b8c8

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix deadlock during thread deletion in free-threaded build, which could
2+
occur when the GIL was enabled at runtime.

Python/pystate.c

+12-9
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ decrement_stoptheworld_countdown(struct _stoptheworld_state *stw);
17511751

17521752
/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
17531753
static void
1754-
tstate_delete_common(PyThreadState *tstate)
1754+
tstate_delete_common(PyThreadState *tstate, int release_gil)
17551755
{
17561756
assert(tstate->_status.cleared && !tstate->_status.finalized);
17571757
tstate_verify_not_active(tstate);
@@ -1793,10 +1793,6 @@ tstate_delete_common(PyThreadState *tstate)
17931793

17941794
HEAD_UNLOCK(runtime);
17951795

1796-
#ifdef Py_GIL_DISABLED
1797-
_Py_qsbr_unregister(tstate);
1798-
#endif
1799-
18001796
// XXX Unbind in PyThreadState_Clear(), or earlier
18011797
// (and assert not-equal here)?
18021798
if (tstate->_status.bound_gilstate) {
@@ -1807,6 +1803,14 @@ tstate_delete_common(PyThreadState *tstate)
18071803
// XXX Move to PyThreadState_Clear()?
18081804
clear_datastack(tstate);
18091805

1806+
if (release_gil) {
1807+
_PyEval_ReleaseLock(tstate->interp, tstate, 1);
1808+
}
1809+
1810+
#ifdef Py_GIL_DISABLED
1811+
_Py_qsbr_unregister(tstate);
1812+
#endif
1813+
18101814
tstate->_status.finalized = 1;
18111815
}
18121816

@@ -1818,7 +1822,7 @@ zapthreads(PyInterpreterState *interp)
18181822
when the threads are all really dead (XXX famous last words). */
18191823
while ((tstate = interp->threads.head) != NULL) {
18201824
tstate_verify_not_active(tstate);
1821-
tstate_delete_common(tstate);
1825+
tstate_delete_common(tstate, 0);
18221826
free_threadstate((_PyThreadStateImpl *)tstate);
18231827
}
18241828
}
@@ -1829,7 +1833,7 @@ PyThreadState_Delete(PyThreadState *tstate)
18291833
{
18301834
_Py_EnsureTstateNotNULL(tstate);
18311835
tstate_verify_not_active(tstate);
1832-
tstate_delete_common(tstate);
1836+
tstate_delete_common(tstate, 0);
18331837
free_threadstate((_PyThreadStateImpl *)tstate);
18341838
}
18351839

@@ -1842,8 +1846,7 @@ _PyThreadState_DeleteCurrent(PyThreadState *tstate)
18421846
_Py_qsbr_detach(((_PyThreadStateImpl *)tstate)->qsbr);
18431847
#endif
18441848
current_fast_clear(tstate->interp->runtime);
1845-
tstate_delete_common(tstate);
1846-
_PyEval_ReleaseLock(tstate->interp, tstate, 1);
1849+
tstate_delete_common(tstate, 1); // release GIL as part of call
18471850
free_threadstate((_PyThreadStateImpl *)tstate);
18481851
}
18491852

Python/qsbr.c

+5
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ _Py_qsbr_unregister(PyThreadState *tstate)
236236
struct _qsbr_shared *shared = &tstate->interp->qsbr;
237237
struct _PyThreadStateImpl *tstate_imp = (_PyThreadStateImpl*) tstate;
238238

239+
// gh-119369: GIL must be released (if held) to prevent deadlocks, because
240+
// we might not have an active tstate, which means taht blocking on PyMutex
241+
// locks will not implicitly release the GIL.
242+
assert(!tstate->_status.holds_gil);
243+
239244
PyMutex_Lock(&shared->mutex);
240245
// NOTE: we must load (or reload) the thread state's qbsr inside the mutex
241246
// because the array may have been resized (changing tstate->qsbr) while

0 commit comments

Comments
 (0)