You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using retry_if_no_exception_type to avoid retries for a specific exception, this causes asyncio.CancelledError to be caught and retried. This is contra-typical behaviour, as CancelledError does not typically cause a retry to occur in other scenarios. This is because task cancellation is not typically a retry case, and allow these errors to bubble up enables cancellation utilities like asyncio.wait_for to work as excpected.
Steps to reproduce
Observe the behaviour as expected with this:
from tenacity import retry, wait_fixed, stop_after_attempt
import asyncio
@retry(
wait=wait_fixed(10),
stop=stop_after_attempt(2),
reraise=True,
)
async def test():
print("Sleeping")
await asyncio.sleep(3)
print("Done")
await asyncio.wait_for(test(), 1) # Expected to cancel after 1s
suppressing cancellation completely is not common and is actively discouraged
The docs do mention the possibility of using uncancel(), although I'm not sure this is the right thing. The alternative might be to reserve one or more types of error that avoid retry unless, for whatever reason, someone explicitly needs them to be retried. For example, they could still use retry=retry_if_exception_type(asyncio.CancelledError).
Activity
jackwildman commentedon Jun 24, 2025
This old PR is potentially relevant from before CancelledError was handled correctly. Particularly, it refers to this excerpt from cancel() docs:
The docs do mention the possibility of using uncancel(), although I'm not sure this is the right thing. The alternative might be to reserve one or more types of error that avoid retry unless, for whatever reason, someone explicitly needs them to be retried. For example, they could still use
retry=retry_if_exception_type(asyncio.CancelledError).fix: correct None checks in retry_if_exception_message and CancelledE…