Skip to content

Commit 04bbdc8

Browse files
lesnik512claude
andcommitted
fix(retry): hoist deposit() to per-request, not per-attempt
`AsyncRetry.__call__` and `Retry.__call__` deposited a token inside the per-attempt for-loop. A request that retried twice contributed three deposits and two withdrawals — inflating the budget's deposits-to- withdrawals ratio by `(attempts-1)/attempts` and letting through more retries than `percent_can_retry` should permit. Move `self.budget.deposit()` immediately above the loop in both worlds so the Finagle contract holds: deposits count original requests; withdrawals count retries. Behavioral impact: tighter retry pacing under load — users with active retry traffic see the budget refuse retries earlier than before. This is the documented contract; the previous behavior was the bug. Closes audit Low finding (retry.py:105 + :236) from planning/audit/2026-06-07-deep-audit.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 79daf0c commit 04bbdc8

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎src/httpware/middleware/resilience/retry.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ async def __call__(self, request: httpx2.Request, next: AsyncNext) -> httpx2.Res
100100
last_exc: BaseException | None = None
101101
last_response: httpx2.Response | None = None
102102

103+
self.budget.deposit()
103104
for attempt in range(self.max_attempts):
104105
is_last = attempt + 1 >= self.max_attempts
105-
self.budget.deposit()
106106
try:
107107
return await next(request)
108108
except StatusError as exc:
@@ -231,9 +231,9 @@ def __call__(self, request: httpx2.Request, next: Next) -> httpx2.Response: # n
231231
last_exc: BaseException | None = None
232232
last_response: httpx2.Response | None = None
233233

234+
self.budget.deposit()
234235
for attempt in range(self.max_attempts):
235236
is_last = attempt + 1 >= self.max_attempts
236-
self.budget.deposit()
237237
try:
238238
return next(request)
239239
except StatusError as exc:

‎tests/test_retry.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,3 +611,30 @@ async def streamed_body() -> typing.AsyncIterator[bytes]:
611611
record = streaming_records[0]
612612
assert record.method == "PUT" # ty: ignore[unresolved-attribute]
613613
assert record.last_exception_type == "ServiceUnavailableError" # ty: ignore[unresolved-attribute]
614+
615+
616+
class _CountingBudget(RetryBudget):
617+
"""RetryBudget that counts deposit() calls."""
618+
619+
def __init__(self) -> None:
620+
super().__init__()
621+
self.deposit_calls = 0
622+
623+
def deposit(self) -> None:
624+
self.deposit_calls += 1
625+
super().deposit()
626+
627+
628+
async def test_deposit_fires_once_per_call_not_per_attempt() -> None:
629+
"""deposit() must be called exactly once per AsyncRetry.__call__, regardless of attempts."""
630+
sleeper = _SleepRecorder()
631+
handler = _ResponseSequence([HTTPStatus.SERVICE_UNAVAILABLE, HTTPStatus.SERVICE_UNAVAILABLE, HTTPStatus.OK])
632+
budget = _CountingBudget()
633+
client = _client(
634+
handler,
635+
retry=AsyncRetry(_sleep=sleeper, base_delay=0.001, max_delay=0.002, max_attempts=3, budget=budget),
636+
)
637+
response = await client.get("https://example.test/x")
638+
assert response.status_code == HTTPStatus.OK
639+
assert handler.calls == 3 # noqa: PLR2004 — "3" is intentional literal in test (max_attempts=3)
640+
assert budget.deposit_calls == 1, f"expected 1 deposit per request, got {budget.deposit_calls}"

‎tests/test_retry_sync.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,30 @@ def test_is_streaming_body_sync_predicates() -> None:
477477
assert _is_streaming_body_sync((1, 2)) is False
478478
assert _is_streaming_body_sync(iter([1, 2])) is True
479479
assert _is_streaming_body_sync(x for x in range(3)) is True # generator
480+
481+
482+
class _CountingBudget(RetryBudget):
483+
"""RetryBudget that counts deposit() calls."""
484+
485+
def __init__(self) -> None:
486+
super().__init__()
487+
self.deposit_calls = 0
488+
489+
def deposit(self) -> None:
490+
self.deposit_calls += 1
491+
super().deposit()
492+
493+
494+
def test_deposit_fires_once_per_call_not_per_attempt() -> None:
495+
"""deposit() must be called exactly once per Retry.__call__, regardless of attempts."""
496+
sleeper = _SleepRecorder()
497+
handler = _ResponseSequence([HTTPStatus.SERVICE_UNAVAILABLE, HTTPStatus.SERVICE_UNAVAILABLE, HTTPStatus.OK])
498+
budget = _CountingBudget()
499+
client = _client(
500+
handler,
501+
retry=Retry(_sleep=sleeper, base_delay=0.001, max_delay=0.002, max_attempts=3, budget=budget),
502+
)
503+
response = client.get("https://example.test/x")
504+
assert response.status_code == HTTPStatus.OK
505+
assert handler.calls == 3 # noqa: PLR2004 — "3" is intentional literal in test (max_attempts=3)
506+
assert budget.deposit_calls == 1, f"expected 1 deposit per request, got {budget.deposit_calls}"

0 commit comments

Comments
 (0)