python runtime_error ai_generated true

asyncio.exceptions.CancelledError: sleep interrupted

ID: python/asyncio-sleep-interrupt

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-05-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.7 active
3.8 active

Root Cause

A task is cancelled during asyncio.sleep(), which is a common cancellation point.

generic

中文

在asyncio.sleep()期间任务被取消,这是一个常见的取消点。

Workarounds

  1. 95% success
    try: await asyncio.sleep(10) except asyncio.CancelledError: raise
  2. 80% success
    await asyncio.shield(asyncio.sleep(10))

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Blocks the event loop and defeats the purpose of asyncio.

  2. 70% fail

    May lead to resource leaks and inconsistent state.