python runtime_error ai_generated true

asyncio.exceptions.CancelledError

ID: python/asyncio-cancel-error-on-shutdown

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.11 active — — —

Root Cause

Task cancellation during shutdown causes unhandled CancelledError in finally blocks that await cleanup operations.

generic

中文

关闭期间任务被取消,finally 块中等待清理操作时未处理 CancelledError。

Workarounds

  1. 85% success
    await asyncio.shield(cleanup()) within finally to protect cleanup from cancellation
  2. 90% success
    asyncio.run(main()) handles cancellation gracefully by cancelling all remaining tasks

Dead Ends

Common approaches that don't work:

  1. 70% fail

  2. 60% fail