subprocess: Popen.wait(timeout) raises TimeoutExpired early when the kqueue/pidfd fast path falls back to polling
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- macos, python
- Domain
- operating-systems
Research direction
Start in Lib/subprocess.py at Popen._wait, where endtime is computed from started and the endtime -= elapsed adjustment runs after the _wait_kqueue()/_wait_pidfd() fallback to polling. Run the reproducer from the issue, which forces the first two _try_wait calls to return (0, 0), and confirm it raises TimeoutExpired at about 2 s. Done means rc 7 is printed after about 2 s, with a regression test added to Lib/test/test_subprocess.py.
Written by the indexing model from the issue text.
Description
Bug description:
On macOS/BSD, Popen.wait(timeout=T) can raise TimeoutExpired well before T has elapsed. The child has in fact already exited, and its exit status is lost to the caller.
In Popen._wait (Lib/subprocess.py), endtime is an absolute deadline:
started = _time()
endtime = started + timeout
If _wait_kqueue() (or _wait_pidfd()) reports the exit but the first waitpid(WNOHANG) still returns 0, the code falls back to busy polling and does:
elapsed = _time() - started
endtime -= elapsed
Since endtime is already absolute, this moves the deadline earlier by the time already spent. The busy loop then raises TimeoutExpired as soon as now > started + T - elapsed. That is immediately whenever more than half of the timeout had elapsed when the child exited, provided the process is still not reapable on the loop's first poll. The endtime -= elapsed line looks like a leftover from a relative-timeout version of this code, and can simply be removed.
Reproducer. It forces the rare race the fallback exists for by making the first two WNOHANG reaps return (0, 0):
import subprocess, sys, time
p = subprocess.Popen([sys.executable, "-c", "import time, sys; time.sleep(2); sys.exit(7)"])
real, calls = p._try_wait, [0]
def racy(flags):
if calls[0] < 2:
calls[0] += 1
return (0, 0) # NOTE_EXIT seen, but the child is not reapable yet
return real(flags)
p._try_wait = racy
t0 = time.monotonic()
try:
print("rc", p.wait(timeout=3))
except subprocess.TimeoutExpired:
print(f"TimeoutExpired after {time.monotonic() - t0:.2f}s of a 3 s timeout; child rc {p.wait()}")
Output on 3.15.2:
TimeoutExpired after 2.02s of a 3 s timeout; child rc 7
The expected output is rc 7, after about 2 s.
Without the patch, we hit this in practice on a heavily loaded machine (5-minute load average of about 60 on 16 cores). Long-running child processes with timeouts of about 1,500-1,560 s exited normally at 804-879 s, past half their timeout but well before it, and wait() raised TimeoutExpired at that moment instead of returning their status.
CPython versions tested on:
3.15
Operating systems tested on:
macOS
- Dominant language
- Python
- Stars
- 78.4k
- Forks
- 36.6k
- Avg merge
- 23h 34m
- Merged PRs (30d)
- 610
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/cpython
-
Item assignment and deletion raise `IndexError` instead of `TypeError` on types without them, for a huge indexPossibly taken A pull request linked to this issue is open or already merged. Openinterpreter-core type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
corner case: base64.b32decode ignorechars=b'=' miscounts paddingPossibly taken @ElDShell claimed this 1 day ago. Open3.15 easy type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
python/cpython#162353 · 1 comment ·
Maintainers usually reply within 1 day
-
TracebackException.lineno changes from str to int after format() when a keyword typo suggestion is foundPossibly taken @Sudhakar7J claimed this 2 days ago. Openstdlib type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
python/cpython#162296 · 1 comment ·
Maintainers usually reply within 1 day
-
[HACL] The `HACL_CAN_COMPILE_VEC128` macro is not defined when building Python with `-Wundef`Possibly taken @tamaki0575-cloud claimed this 1 day ago. Openbuild extension-modules topic-C-API type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
python/cpython#162219 · 2 comments ·
Maintainers usually reply within 1 day
-
zipfile: ZipInfo.comment exceeding 65535 bytes causes struct.error in ZipFile.close() without proper validationPossibly taken @1cbyc claimed this 6 days ago. Openstdlib type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
Similar issues
-
bot-triaged module: correctness (silent) module: fft triage review
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
conda-forge/zarpaint-feedstock#8 · 1 comment · 1 reaction ·
-
plot_prediction colorbar shows a 0-1 range instead of the data rangePossibly taken @nikhil3495 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
mllam/neural-lam#902 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
didi/helixharness#28 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 1/5 Under an hour Newbie friendliness 62/100
DaoCloud/DaoCloud-docs#7839 ·
Maintainers usually reply within 1 day