diff --git a/Misc/NEWS.d/next/Library/2025-08-01-23-11-25.gh-issue-137017.0yGcNc.rst b/Misc/NEWS.d/next/Library/2025-08-01-23-11-25.gh-issue-137017.0yGcNc.rst new file mode 100644 index 00000000000000..726cae3157a289 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-08-01-23-11-25.gh-issue-137017.0yGcNc.rst @@ -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. \ No newline at end of file diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index f82ad6870f850f..548c29b1de848f 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -704,6 +704,10 @@ 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) { + PyErr_SetString(PyExc_RuntimeError, "failed to join thread"); + return NULL; + } Py_RETURN_TRUE; } else {