gh-119369: Fix deadlock during thread exit in free-threaded build by colesbury · Pull Request #119528 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ decrement_stoptheworld_countdown(struct _stoptheworld_state *stw);

/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
static void
tstate_delete_common(PyThreadState *tstate)
tstate_delete_common(PyThreadState *tstate, int release_gil)
{
assert(tstate->_status.cleared && !tstate->_status.finalized);
tstate_verify_not_active(tstate);
Expand Down Expand Up @@ -1793,10 +1793,6 @@ tstate_delete_common(PyThreadState *tstate)

HEAD_UNLOCK(runtime);

#ifdef Py_GIL_DISABLED
_Py_qsbr_unregister(tstate);
#endif

// XXX Unbind in PyThreadState_Clear(), or earlier
// (and assert not-equal here)?
if (tstate->_status.bound_gilstate) {
Expand All @@ -1807,6 +1803,14 @@ tstate_delete_common(PyThreadState *tstate)
// XXX Move to PyThreadState_Clear()?
clear_datastack(tstate);

if (release_gil) {
_PyEval_ReleaseLock(tstate->interp, tstate, 1);
}

#ifdef Py_GIL_DISABLED
_Py_qsbr_unregister(tstate);
#endif

tstate->_status.finalized = 1;
}

Expand All @@ -1818,7 +1822,7 @@ zapthreads(PyInterpreterState *interp)
when the threads are all really dead (XXX famous last words). */
while ((tstate = interp->threads.head) != NULL) {
tstate_verify_not_active(tstate);
tstate_delete_common(tstate);
tstate_delete_common(tstate, 0);
free_threadstate((_PyThreadStateImpl *)tstate);
}
}
Expand All @@ -1829,7 +1833,7 @@ PyThreadState_Delete(PyThreadState *tstate)
{
_Py_EnsureTstateNotNULL(tstate);
tstate_verify_not_active(tstate);
tstate_delete_common(tstate);
tstate_delete_common(tstate, 0);
free_threadstate((_PyThreadStateImpl *)tstate);
}

Expand All @@ -1842,8 +1846,7 @@ _PyThreadState_DeleteCurrent(PyThreadState *tstate)
_Py_qsbr_detach(((_PyThreadStateImpl *)tstate)->qsbr);
#endif
current_fast_clear(tstate->interp->runtime);
tstate_delete_common(tstate);
_PyEval_ReleaseLock(tstate->interp, tstate, 1);
tstate_delete_common(tstate, 1); // release GIL as part of call
free_threadstate((_PyThreadStateImpl *)tstate);
}

Expand Down
5 changes: 5 additions & 0 deletions Python/qsbr.c