diff --git a/Misc/NEWS.d/next/Library/2026-11-11-06-00-00.gh-issue-161712.BrIkxH.rst b/Misc/NEWS.d/next/Library/2026-11-11-06-00-00.gh-issue-161712.BrIkxH.rst new file mode 100644 index 00000000000000..c7f9264951d88c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-11-06-00-00.gh-issue-161712.BrIkxH.rst @@ -0,0 +1,3 @@ +Fix :meth:`threading.Thread.join` with a timeout blocking forever when the +deadline passed between the two reads of the clock used to compute the time +left. diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index 2b227a4eec3721..282df9dddddac8 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -570,10 +570,13 @@ ThreadHandle_join(ThreadHandle *self, PyTime_t timeout_ns) int detach = 1; while (!PyEvent_WaitTimed(&self->thread_is_exiting, timeout_ns, detach)) { if (deadline) { - // _PyDeadline_Get will return a negative value if the deadline has - // been exceeded. + // Read the clock once. _PyDeadline_Get() returns a negative value + // once the deadline has passed, and a negative timeout would mean + // no timeout at all (gh-161712). timeout_ns = _PyDeadline_Get(deadline); - timeout_ns = Py_MAX(timeout_ns, 0); + if (timeout_ns < 0) { + timeout_ns = 0; + } } if (timeout_ns) {