Repository navigation
Stop BackOffHTTPClient retrying past the maximum backoff - #1599
Conversation
The retry guard tested wait <= self._MAX_BACKOFF, but wait is min(2 ** self._NR_BACKOFF, self._MAX_BACKOFF), so the comparison was always true and the 'did not reach the max retry limit' check was dead. A persistently failing retryable request therefore kept retrying (recursing) until Python's recursion limit rather than stopping once the backoff was exhausted. Compare the un-clamped 2 ** self._NR_BACKOFF against self._MAX_BACKOFF in both the APIError and RefreshError paths, so retries stop after the backoff reaches the maximum. The sleep duration still uses the clamped value.
|
Hey @uttam12331, thanks for this I reproduced it and the diagnosis is correct. On master a permanently 429 request retries 954 times before dying on the recursion limit, and your new test fails there (954 != 8) and passes on your branch. Full suite is green. One thing before this can go in though:
The early return True needs the same guard, plus a test case covering it. Thanks ! |
The 403 usageLimits branch of _should_retry returned True unconditionally, so it bypassed the maximum-backoff check that limits the other retryable errors. A permanently failing usageLimits request therefore kept retrying until the recursion limit. Compute the within-max-backoff check once and return it from the usageLimits branch as well, so every retryable path shares the same bound. Adds a regression test that a permanently failing 403 usageLimits request stops after the maximum backoff.
|
Good catch, thanks - you're right, the 403 usageLimits branch returned I've pulled the |
BackOffHTTPClientnever stops retrying a persistently failing request.The retry guard checks
wait <= self._MAX_BACKOFF, butwaitis computed asmin(2 ** self._NR_BACKOFF, self._MAX_BACKOFF), so it is clamped toself._MAX_BACKOFF(128) and the comparison is always true. The comment above it says the intent is to retry only "AND we did not reach the max retry limit" — but that limit check is dead. The same dead guard appears in both theAPIErrorandRefreshErrorpaths.So a request that keeps returning 429 / 5xx keeps retrying (each retry is a recursive call) until Python's recursion limit (~1000), sleeping up to 128 s between attempts, instead of giving up after the backoff is exhausted.
Reproduced with a session that always returns 429: the request recurses ~994 times before failing, rather than stopping after ~7 retries.
The fix compares the un-clamped
2 ** self._NR_BACKOFFagainstself._MAX_BACKOFF, so retries stop once the backoff reaches the maximum (after_NR_BACKOFFreaches 8, i.e. the initial call plus 7 retries). The sleep duration still uses the clampedwait. With the fix, the same permanently-failing request stops after 8 attempts and raisesAPIError.The now-unused
waitparameter of the nested_should_retryhelper is removed.Added
test_gives_up_after_max_backoff(a session that always returns 429 must raiseAPIErrorafter a bounded number of calls); it fails on the current code and passes with the change. Existingtests/http_client_test.pypasses;flake8is clean.