From 1d326234dcbc53b101f02757ff95935e4d1862bb Mon Sep 17 00:00:00 2001 From: Vikash Date: Mon, 23 Nov 2026 03:41:51 +0000 Subject: [PATCH] gh-162438: Fix premature TimeoutExpired in subprocess.Popen.wait() fallback Keep the deadline absolute when the pidfd/kqueue fast path falls back to busy polling. --- Lib/subprocess.py | 5 +-- Lib/test/test_subprocess.py | 34 +++++++++++++++++++ ...-11-23-09-00-00.gh-issue-162438.Rk9wZq.rst | 4 +++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-23-09-00-00.gh-issue-162438.Rk9wZq.rst diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 3e9f58fb1f4f763..84fb1536ea58778 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -2380,8 +2380,9 @@ def _wait(self, timeout): # even though poll() / kqueue() said so. Very # rare and mostly theoretical. Fallback to busy # polling. - elapsed = _time() - started - endtime -= elapsed + # endtime is an absolute deadline; do not + # subtract the time already spent here, or the + # wait can expire early (gh-162438). # Enter a busy loop if we have a timeout. This busy loop was # cribbed from Lib/threading.py in Thread.wait() at r71065. diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 23359d3147ade3a..577b127f0894f5e 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -4450,6 +4450,40 @@ def test_fast_path_avoid_busy_loop(self): self.assertEqual(p.wait(timeout=support.LONG_TIMEOUT), 0) self.assertFalse(m.called) + @unittest.skipIf(mswindows, "requires the POSIX wait implementation") + def test_wait_timeout_not_shortened_in_reap_race(self): + # gh-162438: when the fast wait reports an exit but the first + # waitpid(WNOHANG) cannot reap the child yet, _wait() falls back + # to busy polling. It used to subtract the elapsed time from an + # already-absolute deadline, which made wait(timeout=T) expire + # as soon as more than half of T had elapsed before the fallback, + # losing the child's exit status. + p = subprocess.Popen(ZERO_RETURN_CMD) + real_try_wait = p._try_wait + ncalls = 0 + + def racy_try_wait(flags): + nonlocal ncalls + ncalls += 1 + if ncalls <= 2: + # poll()/kqueue() reported the exit, but the child is + # still not reapable on the first two attempts. + return (0, 0) + return real_try_wait(flags) + + def exit_reported_late(self, timeout): + # Simulate the fast wait noticing the exit after most of + # the caller's timeout has already elapsed. + time.sleep(0.25) + return True + + with mock.patch.object(subprocess.Popen, "_wait_pidfd", + exit_reported_late), \ + mock.patch.object(subprocess.Popen, "_wait_kqueue", + exit_reported_late), \ + mock.patch.object(p, "_try_wait", racy_try_wait): + self.assertEqual(p.wait(timeout=0.4), 0) + @unittest.skipIf(mswindows, "requires the POSIX wait implementation") def test_wait_huge_timeout(self): # gh-154836: very large timeout values used to overflow the C diff --git a/Misc/NEWS.d/next/Library/2026-11-23-09-00-00.gh-issue-162438.Rk9wZq.rst b/Misc/NEWS.d/next/Library/2026-11-23-09-00-00.gh-issue-162438.Rk9wZq.rst new file mode 100644 index 000000000000000..5d705bf5f1b7e54 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-23-09-00-00.gh-issue-162438.Rk9wZq.rst @@ -0,0 +1,4 @@ +Fix :meth:`subprocess.Popen.wait` raising :exc:`subprocess.TimeoutExpired` +early, and losing the child's exit status, when the pidfd or kqueue fast +path reports an exit but the first non-blocking ``waitpid()`` cannot reap +the child yet.