concurrent.futures.wait() waits forever if given a cancelled future
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
concurrent.futures.wait() hangs if given a future that is already cancelled.
Steps to reproduce:
- Get a future that is cancellable (create lots of jobs such that the later ones cannot start yet and are therefore cancellable):
>>> import concurrent.futures
>>> pool = concurrent.futures.ThreadPoolExecutor(max_workers=1)
>>> import time
>>> for _ in range(1000): future = pool.submit(time.sleep, 1000)
- Cancel this future and note that it returns
Truemeaning it has been cancelled.
>>> future.cancel()
True
- Now waiting on this future hangs:
>>> concurrent.futures.wait([future])
The expected behaviour, regardless of the value of return_when is that wait() treats cancelled futures as already completed. This is essentially how return_when=FIRST_COMPLETED and return_when=ALL_COMPLETED are documented to behave:
The function will return when any future finishes or is cancelled.
and
The function will return when all futures finish or are cancelled.
Treating a future that is cancelled prior to wait() as different to one that changes state to cancelled once waiting risks a race condition: it could change state after any possible check but before blocking. i.e. it needs to be level-triggered not edge-triggered. And indeed, it works this way for futures that are done, just not ones that are cancelled.
Your environment
- CPython versions tested on: 3.9.7, 3.10.2
- Operating system and architecture: Linux
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,使用 concurrent.futures.wait() 和一个已取消的 future 重现提供的示例。检查 wait() 入口点及其对已取消 future 的处理,然后为报告的行为添加覆盖测试;完成的标准是,对于文档所述的 return_when 值,wait() 会返回而不是挂起。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 52/100