Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems to be at the wrong place here, it looks like a continuation of the comment above which isn't the case.

# 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.
Expand Down
34 changes: 34 additions & 0 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading