Skip to content

Commit 7160f9f

Browse files
miss-islingtonAbdoulrasheedcolesbury
authored
[3.13] pythongh-137017: Ensure Thread.is_alive() only returns False after the underlying OS thread exits (pythongh-137315) (pythongh-138917)
(cherry picked from commit aa9ceb1) Co-authored-by: Abdul <[email protected]> Co-authored-by: Sam Gross <[email protected]>
1 parent 2fd8f03 commit 7160f9f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :obj:`threading.Thread.is_alive` to remain ``True`` until the underlying OS
2+
thread is fully cleaned up. This avoids false negatives in edge cases
3+
involving thread monitoring or premature :obj:`threading.Thread.is_alive` calls.

Modules/_threadmodule.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,9 @@ ThreadHandle_start(ThreadHandle *self, PyObject *func, PyObject *args,
453453
}
454454

455455
static int
456-
join_thread(ThreadHandle *handle)
456+
join_thread(void *arg)
457457
{
458+
ThreadHandle *handle = (ThreadHandle*)arg;
458459
assert(get_thread_handle_state(handle) == THREAD_HANDLE_RUNNING);
459460
PyThread_handle_t os_handle;
460461
if (ThreadHandle_get_os_handle(handle, &os_handle)) {
@@ -528,8 +529,7 @@ ThreadHandle_join(ThreadHandle *self, PyTime_t timeout_ns)
528529
}
529530
}
530531

531-
if (_PyOnceFlag_CallOnce(&self->once, (_Py_once_fn_t *)join_thread,
532-
self) == -1) {
532+
if (_PyOnceFlag_CallOnce(&self->once, join_thread, self) == -1) {
533533
return -1;
534534
}
535535
assert(get_thread_handle_state(self) == THREAD_HANDLE_DONE);
@@ -657,6 +657,9 @@ PyThreadHandleObject_is_done(PyThreadHandleObject *self,
657657
PyObject *Py_UNUSED(ignored))
658658
{
659659
if (_PyEvent_IsSet(&self->handle->thread_is_exiting)) {
660+
if (_PyOnceFlag_CallOnce(&self->handle->once, join_thread, self->handle) == -1) {
661+
return NULL;
662+
}
660663
Py_RETURN_TRUE;
661664
}
662665
else {

0 commit comments

Comments
 (0)