python runtime_error ai_generated true

并发未来异常:未来被取消

concurrent.futures.CancelledError: Future was cancelled

ID: python/asyncio-cancel-future

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 90% 成功率
    if not fut.cancelled(): await fut
  2. 95% 成功率
    try: await fut except asyncio.CancelledError: raise

无效尝试

常见但无效的做法:

  1. 70% 失败

    Swallowing cancellation can lead to inconsistent state and resource leaks.

  2. 80% 失败

    result() raises the same error; it's not a proper fix.