Repository navigation
Conversation
A sync callable returning an awaitable is a first-class async retry strategy: the async RetryBaseT alias explicitly admits Callable[[RetryCallState], Awaitable[bool]]. AsyncRetrying._run_retry invoked it through wrap_to_async_func, which returns the awaitable the sync callable produced without awaiting it, so the retry decision was made on the truthiness of a coroutine object -- always true. A predicate that (after awaiting) said 'do not retry' therefore retried forever, and the coroutine was leaked with a 'never awaited' warning. Await the result when it is awaitable, mirroring the lifecycle-callback (jd#728) and terminal-callback (jd#747) directions on their own parameters. Assisted-by: Claude (Anthropic) via automated audit
|
Tick the box to add this pull request to the merge queue (same as
|
Ekko82
left a comment
There was a problem hiding this comment.
I tested synchronous retry predicates that return a pending asyncio.Future or Task resolving to False. With AsyncRetrying, stop_after_attempt(3), and reraise=True, the base calls the target three times in both cases. On f0f7558, both stop after one call and re-raise the original ValueError instance.
The synchronous and async predicates that return False directly stop after one call on both revisions. These were four focused cases per revision on CPython 3.13.9, Windows x64. The probe awaited any outstanding objects it created during cleanup.
The async
RetryBaseToverloads promise thatretry=/stop=/wait=predicates may beCallable[[RetryCallState], Awaitable[bool]]. ButAsyncRetrying._run_retrycalls the predicate throughwrap_to_async_funcand never awaits the result when the predicate is a plaindefreturning an awaitable: the coroutine object is coerced by truthiness (always truthy), so a predicate that decides "do not retry" is silently swallowed and the call retries forever. Reproduced with a probe: aretrypredicate returningFalsestill retried 3 times toRetryError, plus "coroutine was never awaited" warnings.Fix: in
_run_retry,isawaitable()the predicate result andawaitit before using the verdict (+4 lines). Deliberately scoped to the retry predicate only -- it is the only parameter whose type promises an awaitable;stop/wait(WaitBaseT) are sync-only by their stubs.A regression test drives
AsyncRetryingwith an async-wrapping retry predicate that returnsFalseand asserts a single attempt with noRetryError(red: 3 attempts + RetryError on the unfixed code).Disclosure: diagnosed and implemented with the help of an AI coding agent; I verified the regression test red-then-green locally (tests/ full suite 189 passed) and the repo's ruff check/format pass.
Assisted-by: AI (ZCode agent)