Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

gh-162438: Fix premature TimeoutExpired in subprocess.Popen.wait() fallback - #162472

Open
vikash1311994 wants to merge 1 commit into
python:mainfrom
vikash1311994:fix/subprocess-wait-absolute-deadline-gh-162438
Open

vikash1311994 wants to merge 1 commit into
python:mainfrom
vikash1311994:fix/subprocess-wait-absolute-deadline-gh-162438

Conversation

@vikash1311994

Copy link
Copy Markdown

Issue

gh-162438 — subprocess.Popen.wait(timeout) can raise TimeoutExpired early when the pidfd/kqueue fast path falls back to busy polling.

Root cause

Popen._wait() builds an absolute deadline: endtime = started + timeout. In the rare race where the fast wait (_wait_pidfd() / _wait_kqueue()) reports the child gone but waitpid(WNOHANG) still returns (0, 0), the fallback did:

elapsed = _time() - started
endtime -= elapsed

Subtracting already-elapsed time from an absolute deadline moves the deadline earlier. Once more than half of timeout had elapsed before the fallback, the busy loop immediately saw a negative remaining time and raised TimeoutExpired, discarding the child's real exit status. The line was introduced in gh-83069 and is the only such arithmetic in the file.

Fix

Remove the subtraction; keep endtime absolute through the fallback so the caller's original deadline is honoured. No platform-specific code, no API change, and no change to the normal (non-race) wait path. There are no other occurrences of this bug shape in subprocess.py.

Tests

  • Added FastWaitTestCase.test_wait_timeout_not_shortened_in_reap_race: the fast wait reports an exit after 0.25 s of a 0.4 s timeout while the first two WNOHANG reaps return (0, 0).
  • Pre-fix (unmodified main): TimeoutExpired — test fails.
  • Sabotage check: restored endtime -= elapsed → test fails with TimeoutExpired; restored the fix → test passes.
  • ./python -m test test_subprocess: SUCCESS, 374 tests run, 49 skipped, 0 failures.
  • Issue reproducer on a locally built main: before the fix, TimeoutExpired after 2.01 s of a 3 s timeout with child rc 7; after the fix, rc 7 is returned.
  • Added a Misc/NEWS.d/next/Library entry.

Risk

Low: one line removed from a rarely taken fallback branch. started is still used to compute endtime. No behavioural change outside the race, no new dependencies.

Explicit exclusions

…t() fallback

Keep the deadline absolute when the pidfd/kqueue fast path falls back to busy polling.
@python-cla-bot

python-cla-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@BHUVANSH855 BHUVANSH855 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sign CLA, so that maintainers can start reviewing.

@vikash1311994

vikash1311994 commented Nov 23, 2026 via email

Copy link
Copy Markdown
Author

Comment thread Lib/subprocess.py
# 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.

@picnixz picnixz added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 28, 2026
@bedevere-app

bedevere-app Bot commented Nov 28, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness.

Please read our guidelines for using AI tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants