multiprocessing.Process.is_alive() incorrect if waitpid() was executed concurrently to it
未关闭
还没有人认领这个 Issue。
topic-multiprocessing
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
As per title. In my case I triggered the os.waitpid() using psutil library, but easily reproducible using os.waitpid, too.
Reproduction:
Python 3.9.5 (default, Nov 18 2021, 16:00:48)
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from multiprocessing import Process
>>> import os
>>> import time
>>> p = Process(target=time.sleep, args=(10,))
>>> p.start(); os.waitpid(p.pid, 0)
(17587, 0)
>>> p.is_alive()
True
>>> p.join() # Returns immediately
>>>
I understand it's something of a corner case; however, join() behaves correctly here, so we definitely have enough info about whether the process is alive.
By the looks of it, if the process has been already waited on, poll() Popen method in multiprocessing returns None, which is interpreted as the process being still alive.
Your environment
- CPython versions tested on: 3.9.5, but main version seems affected
- Operating system and architecture: x64 Ubuntu Linux
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先跟踪 multiprocessing.Process.is_alive()、Popen.poll() 路径和 Process.join(),同时在 Linux 上复现并发 os.waitpid() 示例。比较每条路径如何确定子进程已退出,然后添加回归测试覆盖,证明在子进程已经被等待之后,is_alive() 会报告 false,并且 join() 仍会立即返回。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- operating-systems
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100