8000 gh-116480: Fix _PySemaphore_Wait call during thread deletion · python/cpython@e4aa866 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4aa866

Browse files
committed
gh-116480: Fix _PySemaphore_Wait call during thread deletion
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.
1 parent 5d0cdfe commit e4aa866

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Python/parking_lot.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,17 @@ _PySemaphore_Wait(_PySemaphore *sema, PyTime_t timeout, int detach)
194194
PyThreadState *tstate = NULL;
195195
if (detach) {
196196
tstate = _PyThreadState_GET();
197-
if (tstate) {
198-
PyEval_ReleaseThread(tstate);
197+
// Only detach if we are attached
198+
if (tstate && tstate->state != _Py_THREAD_ATTACHED) {
199+
tstate = NULL;
199200
}
200201
}
201202

203+
if (tstate) {
204+
PyEval_ReleaseThread(tstate);
205+
}
202206
int res = _PySemaphore_PlatformWait(sema, timeout);
203-
204-
if (detach && tstate) {
207+
if (tstate) {
205208
PyEval_AcquireThread(tstate);
206209
}
207210
return res;

0 commit comments

Comments
 (0)
0