Skip to content

Commit 3cdfdc0

Browse files
gh-108724: Fix _PySemaphore_Wait call during thread deletion (#116483)
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(). Co-authored-by: Eric Snow <[email protected]>
1 parent 601f3a7 commit 3cdfdc0

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Python/parking_lot.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,16 @@ _PySemaphore_Wait(_PySemaphore *sema, PyTime_t timeout, int detach)
194194
PyThreadState *tstate = NULL;
195195
if (detach) {
196196
tstate = _PyThreadState_GET();
197-
if (tstate) {
197+
if (tstate && tstate->state == _Py_THREAD_ATTACHED) {
198+
// Only detach if we are attached
198199
PyEval_ReleaseThread(tstate);
199200
}
201+
else {
202+
tstate = NULL;
203+
}
200204
}
201-
202205
int res = _PySemaphore_PlatformWait(sema, timeout);
203-
204-
if (detach && tstate) {
206+
if (tstate) {
205207
PyEval_AcquireThread(tstate);
206208
}
207209
return res;

0 commit comments

Comments
 (0)