mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
[3.13] gh-137017: Ensure Thread.is_alive() only returns False after the underlying OS thread exits (gh-137315) (gh-138917)
(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:
parent
2fd8f036c0
commit
7160f9f5d8
2 changed files with 9 additions and 3 deletions
|
|
@ -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.
|
||||
|
|
@ -453,8 +453,9 @@ ThreadHandle_start(ThreadHandle *self, PyObject *func, PyObject *args,
|
|||
}
|
||||
|
||||
static int
|
||||
join_thread(ThreadHandle *handle)
|
||||
join_thread(void *arg)
|
||||
{
|
||||
ThreadHandle *handle = (ThreadHandle*)arg;
|
||||
assert(get_thread_handle_state(handle) == THREAD_HANDLE_RUNNING);
|
||||
PyThread_handle_t os_handle;
|
||||
if (ThreadHandle_get_os_handle(handle, &os_handle)) {
|
||||
|
|
@ -528,8 +529,7 @@ ThreadHandle_join(ThreadHandle *self, PyTime_t timeout_ns)
|
|||
}
|
||||
}
|
||||
|
||||
if (_PyOnceFlag_CallOnce(&self->once, (_Py_once_fn_t *)join_thread,
|
||||
self) == -1) {
|
||||
if (_PyOnceFlag_CallOnce(&self->once, join_thread, self) == -1) {
|
||||
return -1;
|
||||
}
|
||||
assert(get_thread_handle_state(self) == THREAD_HANDLE_DONE);
|
||||
|
|
@ -657,6 +657,9 @@ PyThreadHandleObject_is_done(PyThreadHandleObject *self,
|
|||
PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue