[3.14] gh-137017: Ensure Thread.is_alive() only returns False after the underlying OS thread exits (gh-137315) (gh-138916)

(cherry picked from commit aa9ceb1721)

Co-authored-by: Abdul <abdulrasheedibrahim47@gmail.com>
Co-authored-by: Sam Gross <colesbury@gmail.com>
This commit is contained in:
Miss Islington (bot) 2025-10-07 19:37:31 +02:00 committed by GitHub
parent 85dabb9c15
commit 9e13cc2896
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 0 deletions

View file

@ -0,0 +1,3 @@
Fix :obj:`threading.Thread.is_alive` to remain ``True`` until the underlying OS
thread is fully cleaned up. This avoids false negatives in edge cases
involving thread monitoring or premature :obj:`threading.Thread.is_alive` calls.

View file

@ -690,6 +690,9 @@ PyThreadHandleObject_is_done(PyObject *op, PyObject *Py_UNUSED(dummy))
{
PyThreadHandleObject *self = PyThreadHandleObject_CAST(op);
if (_PyEvent_IsSet(&self->handle->thread_is_exiting)) {
if (_PyOnceFlag_CallOnce(&self->handle->once, join_thread, self->handle) == -1) {
return NULL;
}
Py_RETURN_TRUE;
}
else {