python
runtime_error
ai_generated
true
asyncio.exceptions.CancelledError
ID: python/asyncio-cancel-error-on-shutdown
80%Fix Rate
85%Confidence
0Evidence
2024-03-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.11 | active | — | — | — |
Root Cause
Task cancellation during shutdown causes unhandled CancelledError in finally blocks that await cleanup operations.
generic中文
关闭期间任务被取消,finally 块中等待清理操作时未处理 CancelledError。
Workarounds
-
85% success
await asyncio.shield(cleanup()) within finally to protect cleanup from cancellation
-
90% success
asyncio.run(main()) handles cancellation gracefully by cancelling all remaining tasks
Dead Ends
Common approaches that don't work:
- 70% fail
- 60% fail