Async task was cancelled
Production Risk
Always re-raise CancelledError; swallowing it breaks graceful shutdown of async applications.
Raised inside a coroutine when it is cancelled via Task.cancel(). In Python 3.8+, CancelledError is a subclass of BaseException (not Exception), so most except Exception blocks do not catch it.
- 1task.cancel() was called on a running asyncio Task
- 2asyncio.wait_for() timed out and cancelled the coroutine
- 3TaskGroup cancelled sibling tasks after one failed
An asyncio task is cancelled while running.
import asyncio
async def long_task():
try:
await asyncio.sleep(100)
except asyncio.CancelledError:
print("Task cancelled — cleaning up")
raise # must re-raise CancelledError
async def main():
task = asyncio.create_task(long_task())
await asyncio.sleep(1)
task.cancel()
await task
asyncio.run(main())expected output
Task cancelled — cleaning up
Fix
Always re-raise CancelledError after cleanup
WHEN Catching CancelledError for cleanup
async def my_task():
try:
await some_operation()
except asyncio.CancelledError:
# Do cleanup (close connections, release locks, etc.)
await cleanup()
raise # MUST re-raise so the task is properly cancelledWhy this works
If CancelledError is swallowed, the task appears to complete successfully instead of being cancelled, breaking the cancel protocol.
import asyncio
async def work():
await asyncio.sleep(100)
async def main():
t = asyncio.create_task(work())
await asyncio.sleep(1)
t.cancel() # CancelledError raised in work()
await t
asyncio.run(main())async def work():
try:
await long_operation()
except asyncio.CancelledError:
await cleanup()
raise # must re-raiseimport asyncio
async def main():
result = await asyncio.shield(critical_operation())
return result✕ Swallow CancelledError with a bare except or except Exception
CancelledError is BaseException — bare except catches it but except Exception does not (in 3.8+). Never silence it; always re-raise.
Python Docs — asyncio exceptions
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev