subprocess: Popen.wait(timeout) raises TimeoutExpired early when the kqueue/pidfd fast path falls back to polling

未关闭 适合新手
#162,438 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
macos, python

调研方向

从 Lib/subprocess.py 中的 Popen._wait 开始,这里 endtime 由 started 计算得出,并且在回退到 _wait_kqueue()/_wait_pidfd() 轮询之后执行 endtime -= elapsed 调整。运行 issue 中的复现脚本,该脚本强制前两次 _try_wait 调用返回 (0, 0),并确认其在约 2 秒时抛出 TimeoutExpired。完成的标志是约 2 秒后打印出 rc 7,并在 Lib/test/test_subprocess.py 中添加了回归测试。

由索引模型根据 Issue 内容生成。

描述

stdlib topic-subprocess type-bug
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 分钟
30 天内合并 PR
610

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。