Skip to content

gh-137017: Fix Thread.is_alive() to only return False after the underlying OS thread exits #137315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading