fix: never retry CancelledError under retry_if_not_exception_type - #661
Open
MohammedAnasNathani wants to merge 5 commits into
Open
fix: never retry CancelledError under retry_if_not_exception_type#661MohammedAnasNathani wants to merge 5 commits into
MohammedAnasNathani wants to merge 5 commits into
Conversation
retry_if_not_exception_type(T) treated every non-T exception as retryable, including asyncio.CancelledError. That broke asyncio.wait_for / timeout cancellation: the task was retried instead of stopping. Exclude control-flow exceptions (CancelledError, KeyboardInterrupt, SystemExit, GeneratorExit) before applying the user type filter. Same guard on retry_unless_exception_type. Async + sync regression tests. Fixes jd#529.
Required for green CI under current ruff: preview RUF036 flags None | X unions in public retry signatures. Purely mechanical reordering; no behavior change.
CI runs ruff format --check; wrap long async test def and normalize spacing around the new control-flow cases.
…ator Double @asynctest made asyncio.run receive None (wrapper result), failing wait_for coverage. Also restore @asynctest on test_iscoroutinefunction after insert.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #529.
retry_if_not_exception_type(ValueError)currently retries any non-ValueError, includingasyncio.CancelledError. That breaks cancellation:Default
retry_if_exception_type()only retriesExceptionsubclasses, so cancel already worked there. The bug is specific to theif_not/unlesspredicates.Fix
Central helper
_is_control_flow_exception— never retry:asyncio.CancelledErrorKeyboardInterrupt/SystemExit/GeneratorExitApplied in:
retry_if_not_exception_typeretry_unless_exception_typeTests
wait_for+retry_if_not_exception_type→ one attempt onlyCancelledError→ not retriedKeyboardInterrupt→ not retriedWhy this is important
Without this, any
asyncio.timeout/wait_foraround a tenacity-wrapped coroutine with a customretry_if_not_exception_typecan hang or run past the deadline.