mirror of https://github.com/python/cpython
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 <ericsnowcurrently@gmail.com>
This commit is contained in:
parent
601f3a7b33
commit
3cdfdc07a9
|
@ -194,14 +194,16 @@ _PySemaphore_Wait(_PySemaphore *sema, PyTime_t timeout, int detach)
|
||||||
PyThreadState *tstate = NULL;
|
PyThreadState *tstate = NULL;
|
||||||
if (detach) {
|
if (detach) {
|
||||||
tstate = _PyThreadState_GET();
|
tstate = _PyThreadState_GET();
|
||||||
if (tstate) {
|
if (tstate && tstate->state == _Py_THREAD_ATTACHED) {
|
||||||
|
// Only detach if we are attached
|
||||||
PyEval_ReleaseThread(tstate);
|
PyEval_ReleaseThread(tstate);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
tstate = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int res = _PySemaphore_PlatformWait(sema, timeout);
|
int res = _PySemaphore_PlatformWait(sema, timeout);
|
||||||
|
if (tstate) {
|
||||||
if (detach && tstate) {
|
|
||||||
PyEval_AcquireThread(tstate);
|
PyEval_AcquireThread(tstate);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
|
|
Loading…
Reference in New Issue