Skip to content

Add opt-in status-code retries to Client and AsyncClient - #3857

Open
masterramankhatri-glitch wants to merge 10 commits into
encode:masterfrom
masterramankhatri-glitch:feature/status-code-retries
Open

masterramankhatri-glitch wants to merge 10 commits into
encode:masterfrom
masterramankhatri-glitch:feature/status-code-retries

Conversation

@masterramankhatri-glitch

Copy link
Copy Markdown

Summary

This PR adds opt-in status-code retries to httpx. Until now, httpx did not
retry a request when the server responded with a transient error status
(for example 429 or 503), so every user had to write their own retry loop
around the client. This change adds that capability inside the client
itself, in a fully opt-in way.

What changed

  • Client and AsyncClient can now be configured to automatically retry
    a request when the response has one of the configured retryable status
    codes.
  • The retry behaviour is off by default. Existing code that does not
    opt in behaves exactly as before, so this is fully backward compatible.
  • Retries apply per request and stop as soon as a non-retryable response
    is received or the configured attempts are exhausted.
  • Updated the client implementation in httpx/_client.py.
  • Added documentation for the new option in docs/advanced/clients.md.
  • Added a changelog entry under the 1.0.0 (Unreleased) section.

Tests

  • Added a new test module tests/client/test_retries.py covering the
    retry behaviour for both the sync and async clients — 21 tests, all
    passing.
  • Full test suite run locally: 1435 passed (the only failures are
    pre-existing and unrelated to this change).

Notes

This is intentionally a minimal, focused implementation: only status-code
based retries, no changes to transport-level or connection-error handling,
so it is easy to review and safe to extend later.

@YashrajMiyatra

Copy link
Copy Markdown

The opt-in retry on status codes is a clean addition. Good call having test_status_retries verify exponential backoff jitter and ensuring streaming responses don't rewind mid-stream. Diff looks clean.

@ege-arhan ege-arhan 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.

Thanks, the opt-in shape and replayability guard look right.

One issue with the Retry-After handling in _get_retry_delay (httpx/_client.py:253-274): the header value is returned unbounded, so a server can park the client for an arbitrary amount of time. Retry-After: 86400 makes time.sleep()/anyio.sleep() block for 24h, with no way to bound it short of disabling retries entirely.

Repro (with time.sleep patched to record the delay, max_retries=1, transport returns 503 with Retry-After: 86400):

status 200 sleep_delays [86400.0]

The HTTP-date branch (line 261-274) is unbounded the same way.

Suggestion: clamp both branches to a configurable ceiling, e.g. a retry_after_max (default something like 60s) or reuse a backoff cap:

if delay >= 0:
    return min(float(delay), self.retry_after_max)

and a test asserting Retry-After: 86400 produces the capped delay would keep this from regressing. Worth noting too that the docs currently just say the header is honored, without mentioning that it can be huge.

@masterramankhatri-glitch

Copy link
Copy Markdown
Author

Thanks for the careful review! You're right - the Retry-After value was unbounded. I've added a retry_after_max client option (default 60s) and clamped both the delta-seconds and HTTP-date branches to it, with regression tests for the Retry-After: 86400 case (sync, async, and HTTP-date forms) and a docs note. Pushed in the latest commits - please take another look when you get a chance.

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

Development

Successfully merging this pull request may close these issues.

3 participants