subprocess: Popen.wait(timeout) raises TimeoutExpired early when the kqueue/pidfd fast path falls back to polling
维护者通常 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 内容生成。
描述
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
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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 天内回复