Skip to content

Commit

Permalink
gh-116480: Fix _PySemaphore_Wait call during thread deletion
Browse files Browse the repository at this point in the history
In general, when `_PyThreadState_GET()` is non-NULL then the current
thread is "attached", but there is a small window during
`PyThreadState_DeleteCurrent()` where that's not true:
tstate_delete_common is called when the thread is detached, but before
current_fast_clear().

This updates _PySemaphore_Wait() to handle that case.
  • Loading branch information
colesbury committed Mar 7, 2024
1 parent 5d0cdfe commit e4aa866
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Python/parking_lot.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,17 @@ _PySemaphore_Wait(_PySemaphore *sema, PyTime_t timeout, int detach)
PyThreadState *tstate = NULL;
if (detach) {
tstate = _PyThreadState_GET();
if (tstate) {
PyEval_ReleaseThread(tstate);
// Only detach if we are attached
if (tstate && tstate->state != _Py_THREAD_ATTACHED) {
tstate = NULL;
}
}

if (tstate) {
PyEval_ReleaseThread(tstate);
}
int res = _PySemaphore_PlatformWait(sema, timeout);

if (detach && tstate) {
if (tstate) {
PyEval_AcquireThread(tstate);
}
return res;
Expand Down

0 comments on commit e4aa866

Please sign in to comment.