python runtime_error ai_generated true

concurrent.futures.CancelledError: Future was cancelled

ID: python/asyncio-cancel-future

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-11-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.7 active
3.8 active

Root Cause

Awaiting a future that has been cancelled, often due to a parent task being cancelled.

generic

中文

等待一个已被取消的future,通常是因为父任务被取消。

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Swallowing cancellation can lead to inconsistent state and resource leaks.

  2. 80% fail

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