Skip to content

fix: await awaitable results from async retry predicates - #784

Open
Lushiyao5 wants to merge 1 commit into
jd:mainfrom
Lushiyao5:fix/async-retry-predicate-await-20261119
Open

Lushiyao5 wants to merge 1 commit into
jd:mainfrom
Lushiyao5:fix/async-retry-predicate-await-20261119

Conversation

@Lushiyao5

Copy link
Copy Markdown

The async RetryBaseT overloads promise that retry=/stop=/wait= predicates may be Callable[[RetryCallState], Awaitable[bool]]. But AsyncRetrying._run_retry calls the predicate through wrap_to_async_func and never awaits the result when the predicate is a plain def returning 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: a retry predicate returning False still retried 3 times to RetryError, plus "coroutine was never awaited" warnings.

Fix: in _run_retry, isawaitable() the predicate result and await it 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 AsyncRetrying with an async-wrapping retry predicate that returns False and asserts a single attempt with no RetryError (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)

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
@mergify

mergify Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

@Ekko82 Ekko82 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants