python
runtime_error
ai_generated
true
并发未来异常:未来被取消
concurrent.futures.CancelledError: Future was cancelled
ID: python/asyncio-cancel-future
80%修复率
83%置信度
0证据数
2024-11-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.7 | active | — | — | — |
| 3.8 | active | — | — | — |
根因分析
等待一个已被取消的future,通常是因为父任务被取消。
English
Awaiting a future that has been cancelled, often due to a parent task being cancelled.
解决方案
-
90% 成功率
if not fut.cancelled(): await fut
-
95% 成功率
try: await fut except asyncio.CancelledError: raise
无效尝试
常见但无效的做法:
-
70% 失败
Swallowing cancellation can lead to inconsistent state and resource leaks.
-
80% 失败
result() raises the same error; it's not a proper fix.