Fix retry_if_exception_cause_type infinite loop on cyclic exception cause chains#659
Open
earfman wants to merge 2 commits into
Open
Fix retry_if_exception_cause_type infinite loop on cyclic exception cause chains#659earfman wants to merge 2 commits into
earfman wants to merge 2 commits into
Conversation
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.
Fixes #658.
Problem
retry_if_exception_cause_type.__call__walks the__cause__chain until it reachesNone. A cyclic chain — the simplest being ordinaryraise e from e— never reachesNone, so the predicate spins forever at 100% CPU. Because the spin is inside the retry predicate,stop_after_attempt/stop_after_delaynever get a chance to end the retry: stop conditions are only consulted after the predicate returns. Two-node cycles (a.__cause__ = b; b.__cause__ = a) hang the same way. The stdlibtracebackmodule guards against exactly these cause/context cycles when walking exception chains.Fix
Track the ids of exceptions already visited and stop the walk when the chain repeats:
Behavior on acyclic chains is unchanged; a cyclic chain now finishes scanning each distinct exception once and returns the correct result.
Tests
Adds
test_retry_if_exception_cause_type_with_cause_cyclecovering both cycle shapes (raise e from eand a two-node cycle). The new test hangs indefinitely on currentmainwithout the fix (only bounded by an external timeout) and passes in milliseconds with it. Full suite: 172 passed, 1 skipped, 12 subtests.Notes
As flagged in #658, the proposed
retry_unless_exception_cause_typefeature (#625 / #626) walks the cause chain the same way — if this fix lands, that feature can reuse the cycle-safe walk.Prepared with AI assistance and independently verified before submission (fresh clone, hang reproduced on HEAD c650fb4, fix reviewed, full suite green, regression test confirmed non-vacuous).