subprocess: Popen.wait(timeout) raises TimeoutExpired early when the kqueue/pidfd fast path falls back to polling
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 72/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- macos, python
調査の方向性
Lib/subprocess.py の Popen._wait から始めてください。ここでは started から endtime が計算され、_wait_kqueue()/_wait_pidfd() によるポーリングへのフォールバックの後に endtime -= elapsed の調整が実行されます。issue の再現コードを実行してください。これは最初の 2 回の _try_wait 呼び出しに (0, 0) を返させるものです。約 2 秒で TimeoutExpired が送出されることを確認してください。約 2 秒後に rc 7 が出力され、Lib/test/test_subprocess.py にリグレッションテストが追加されていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Python
- スター
- 78.4k
- フォーク
- 36.6k
- 平均マージ
- 23時間 34分
- マージ済み PR(30日)
- 610
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
interpreter-core type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
corner case: base64.b32decode ignorechars=b'=' miscounts padding対応中かも @ElDShell が今日担当しました。 オープン3.15 easy type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
python/cpython#162353 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
TracebackException.lineno changes from str to int after format() when a keyword typo suggestion is found対応中かも @Sudhakar7J が 2 日前に担当しました。 オープンstdlib type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
python/cpython#162296 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[HACL] The `HACL_CAN_COMPILE_VEC128` macro is not defined when building Python with `-Wundef`対応中かも @tamaki0575-cloud が 1 日前に担当しました。 オープンbuild extension-modules topic-C-API type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
python/cpython#162219 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
zipfile: ZipInfo.comment exceeding 65535 bytes causes struct.error in ZipFile.close() without proper validation対応中かも @1cbyc が 5 日前に担当しました。 オープンstdlib type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
vllm-project/vllm#69304 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
pydanty:is-working
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
pydantic/pydantic-ai#14187 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
test
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
prebid/salesagent#2676 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 62/100
sunososobro-hub/cloudball#339 ·
メンテナーはふだん 1 日以内に返信