From 30ff6747f4cfeecdcabb79bc54e253600485982b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmed=20Semih=20=C3=96zmekik?= Date: Thu, 12 Nov 2026 03:45:42 +0300 Subject: [PATCH] [3.15] gh-161712: Read the clock once in `ThreadHandle_join()` (GH-161771) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. (cherry picked from commit 88d137a891a82e04437973cdda7ef6a96a1a2b01) Co-authored-by: Ahmed Semih Ă–zmekik --- .../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 d0847af17cc625..9a1a45e399e629 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -541,7 +541,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) {