From fd9c0baa5e7ff0119f3751d7118bd893fb8d972f Mon Sep 17 00:00:00 2001 From: Ahmed Semih Ozmekik Date: Tue, 10 Nov 2026 17:49:27 +0300 Subject: [PATCH] [3.15] gh-161712: Read the clock once in ThreadHandle_join() Py_MAX() evaluated _PyDeadline_Get() twice. If the deadline passed between the two calls, the negative second value was used as the timeout, which means no timeout, and Thread.join(timeout) could block forever. main already reads it once since gh-157495. --- .../Library/2026-11-10-19-00-00.gh-issue-161712.HyM9PA.rst | 3 +++ Modules/_threadmodule.c | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-10-19-00-00.gh-issue-161712.HyM9PA.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-10-19-00-00.gh-issue-161712.HyM9PA.rst b/Misc/NEWS.d/next/Library/2026-11-10-19-00-00.gh-issue-161712.HyM9PA.rst new file mode 100644 index 00000000000000..c7f9264951d88c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-10-19-00-00.gh-issue-161712.HyM9PA.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 81ff9af1bd97c6..d5203247272ac2 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -571,7 +571,8 @@ ThreadHandle_join(ThreadHandle *self, PyTime_t timeout_ns) if (deadline) { // _PyDeadline_Get will return a negative value if the deadline has // been exceeded. - timeout_ns = Py_MAX(_PyDeadline_Get(deadline), 0); + timeout_ns = _PyDeadline_Get(deadline); + timeout_ns = Py_MAX(timeout_ns, 0); } if (timeout_ns) {