Repository navigation
fix: honor Retry-After on 5xx responses, not just 429 - #619
Merged
Merged
Conversation
A 5xx carrying Retry-After was ignored: the client retried on its default backoff instead of waiting the requested time, unlike 429. Give ServiceError a retry_after_seconds slot (mirroring TooManyRequests) and pass the header from the 5xx branch of interpret_b2_error; b2http already honors a non-None value.
sophiecarreras
approved these changes
Oct 1, 2026
sophiecarreras
left a comment
Contributor
There was a problem hiding this comment.
Looks right, and it reuses the existing sleep path in _translate_and_retry. Non-blocking: (1) int(retry_after) will raise ValueError for the HTTP-date form of Retry-After, which proxies in front of B2 can emit; please fall back to None (default backoff) instead of failing inside interpret_b2_error; (2) consider whether very large values should be capped, as with 429; (3) please add a string-valued header case and a 503 test through _translate_and_retry so the actual sleep is covered. CI: unit sessions are green; the red jobs are the known test_encryption integration failures (#614).
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.
Context
When the server returns a
5xx(e.g.503 service_unavailable) carrying aRetry-Afterheader, the client ignores it and retries on its own default exponential backoff (~1s first
step) instead of waiting the requested interval. A
429carrying the sameRetry-Afteris honored correctly. The raw responses are byte-for-byte identical apart from status/code, so
this is a client handling difference, not a server difference — and it makes the client retry
more aggressively than B2 explicitly asked for, which is exactly what
Retry-Afterexists toprevent. Backblaze's Integration Checklist gives the same
Retry-Afterguidance for 5xx as for429.
Root cause
In
b2sdk/_internal/exception.pythere is exactly one write site and one read site forretry_after_seconds. The429branch buildsTooManyRequests(retry_after_seconds=...), andTooManyRequests.__init__is the only constructor that stored the value. The500 <= status < 600branch buildsServiceErrorfrom the message string only and never readsresponse_headers, andServiceErrorhad noretry_after_secondsslot — it inheritedB2Error.__init__, which setsretry_after_seconds = Noneunconditionally.b2http's_translate_and_retryalready honors a non-Noneretry_after_secondson read, so only thewrite side for 5xx was missing.
Fix
ServiceErroran__init__(self, *args, retry_after_seconds=None)that stores the valuewhen present (mirroring
TooManyRequests), keeping the positional message argument.retry_after_seconds=response_headers.get('retry-after')from the 5xx branch.No change to
b2http's read logic — it already honors the value; only the 5xx write side wasmissing.
Verification
test/unit/test_exception.py: aServiceErrorbuilt from a503with{'retry-after': 200}hasretry_after_seconds == 200, andNonewithout the header. Redbefore the change, green after.
nox -s unit).api.retry_after_503scenario goesFAIL → PASS (the client now waits the server-requested interval before retrying).
Fixes #606