python runtime_error ai_generated true

asyncio.exceptions.CancelledError: Task exception was never retrieved

ID: python/asyncio-task-exception-silent

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.7 active
3.8 active
3.9 active

Root Cause

A task raises an exception but the exception is never awaited or retrieved, leading to silent failures.

generic

中文

任务引发异常,但异常从未被等待或检索,导致静默失败。

Workarounds

  1. 90% success
    task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None)
  2. 95% success
    try: await task except Exception as e: print(e)

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The exception is lost, making debugging difficult.

  2. 60% fail

    If the task is not done, it may block or raise.