Skip to content

Commit a63bfb7

Browse files
committed
test state of Threadhandle in test of is_running
1 parent 1519687 commit a63bfb7

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

Lib/test/test_threading.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,25 +1458,23 @@ def run_in_bg():
14581458
self.assertEqual(err, b"")
14591459
self.assertEqual(out.strip(), b"Exiting...")
14601460

1461+
def _nothing(self):
1462+
pass
1463+
1464+
def _set_ident_error(self):
1465+
1/0
1466+
14611467
def test_error_bootstrap(self):
14621468
# gh-140746: Test that Thread.start() doesn't hang indefinitely if
14631469
# the new thread fails (MemoryError in the referenced issue)
14641470
# during its initialization
14651471

1466-
def nothing():
1467-
pass
1468-
1469-
def _set_ident_error():
1470-
1/0
14711472

1473+
thread = threading.Thread(target=self._nothing)
14721474
with support.catch_unraisable_exception(), self.assertRaises(RuntimeError):
1473-
thread = threading.Thread(target=nothing)
1474-
thread._set_ident = _set_ident_error
1475+
thread._set_ident = self._set_ident_error
14751476
thread.start()
14761477

1477-
# Give the thread a moment to clean up after itself
1478-
# specially in free-threading builds.
1479-
time.sleep(0.1)
14801478
self.assertFalse(thread.is_alive())
14811479
self.assertFalse(thread in threading._limbo)
14821480
self.assertFalse(thread in threading._active)

Modules/_threadmodule.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class _thread.RLock "rlockobject *" "clinic_state()->rlock_type"
9898
// NOT_STARTED -> STARTING -> RUNNING -> DONE
9999
// | ^
100100
// | |
101-
// +----- FAILED --------+
101+
// +----- FAILED -------+
102102
typedef enum {
103103
THREAD_HANDLE_NOT_STARTED = 1,
104104
THREAD_HANDLE_STARTING = 2,
@@ -398,7 +398,7 @@ thread_run(void *boot_raw)
398398
PyErr_FormatUnraisable(
399399
"Exception ignored in thread started by %R", boot->func);
400400
}
401-
// Notify that the bootstrap is done and failed (e.g. Memory error).
401+
// Notify that bootstrap is done and failed (e.g. Memory error).
402402
set_thread_handle_state(handle, THREAD_HANDLE_FAILED);
403403
_PyEvent_Notify(&handle->thread_is_running);
404404
}
@@ -722,7 +722,8 @@ static PyObject *
722722
PyThreadHandleObject_is_running(PyObject *op, PyObject *Py_UNUSED(dummy))
723723
{
724724
PyThreadHandleObject *self = PyThreadHandleObject_CAST(op);
725-
if (_PyEvent_IsSet(&self->handle->thread_is_running)) {
725+
if (get_thread_handle_state(self->handle) == THREAD_HANDLE_RUNNING
726+
&& _PyEvent_IsSet(&self->handle->thread_is_running)) {
726727
Py_RETURN_TRUE;
727728
}
728729
else {
@@ -2944,4 +2945,4 @@ PyMODINIT_FUNC
29442945
PyInit__thread(void)
29452946
{
29462947
return PyModuleDef_Init(&thread_module);
2947-
}
2948+
}

0 commit comments

Comments
 (0)