Skip to content

Keep waiting for a review through rate limits and unavailability - #96

Open
apoorvarohilla wants to merge 2 commits into
prooflinedev:mainfrom
apoorvarohilla:wait-through-retryable-problems
Open

apoorvarohilla wants to merge 2 commits into
prooflinedev:mainfrom
apoorvarohilla:wait-through-retryable-problems

Conversation

@apoorvarohilla

@apoorvarohilla apoorvarohilla commented Nov 23, 2026 •

Copy link
Copy Markdown

Problem

wait_for_review raised the 429 or 503 that ended one read's retries, even when most of its timeout was left. A CI job waiting on a review failed on a brief rate limit or service restart instead of waiting.

Loopback reproduction (real sockets and real time, ProoflineClient; the tests below cover both clients):

Server behavior Wait timeout Before After
503 five times (Retry-After: 0), then the review is COMPLETED 300 s raised HTTP 503 after 4 requests, 0.0 s COMPLETED after 6 requests, 5.0 s
503 with Retry-After: 90 3 s raised HTTP 503 after 1 request, 0.0 s TIMED_OUT after 1 request, 3.0 s

The outcome also depended on the length of the header rather than on the wait's deadline. test_retry_after_is_bounded_by_wait_deadline pins that a 503 with Retry-After: 20 against a 10-second wait returns TIMED_OUT, and #12 describes TIMED_OUT as the only consistent outcome for a retryable problem in a wait. But Retry-After: 61 against a 300-second wait raised 503 immediately, because the transport refuses a single call's retry above 60 seconds and the wait re-raised that refusal.

Approach

A single call keeps its bounds: at most four attempts, never retried earlier than its Retry-After, and a Retry-After over 60 seconds is returned to the caller. Those are the right limits for one request. The wait is the part that has a longer budget, so the change is in the wait:

  • When a read ends with a retryable 429 or 503, wait_for_review treats it like an unfinished review. It holds the next read until the larger of its own poll interval and the problem's Retry-After has passed, sleeping toward it in steps of at most a minute and never past the deadline, and reads again. When the deadline passes it returns TIMED_OUT with the last review it read, as it already does for a review that is still running.
  • Every other problem (400, 401, 403, 404, 500, 502, ...) still ends the wait and raises, as before. So does an encoded or unreadable 429 or 503, which the transport refuses before its retry policy reads it.
  • The transport marks the 429 and 503 problems that pass through its retry policy as retryable and records their parsed Retry-After (retry_after, inf for more seconds than a float holds, None when absent or invalid) on the ProoflineError it raises. retry_delay is split into retry_after (parse) and the existing 60-second retry bound, with unchanged results. Callers of non-retried requests such as request_review can read the same value.

The README and the wait_for_review docstring describe the new behavior.

Verification

New tests in tests/test_http.py, all on both clients with the suite's fake Clock:

  • test_wait_keeps_polling_after_one_request_exhausts_its_retries (429, 503): five 503s then a completed review returns COMPLETED; the requests and sleeps are exactly four transport attempts, one five-second wait poll, and the final read.
  • test_wait_honors_a_retry_after_longer_than_one_request_retries: with Retry-After: 90 the second request arrives exactly 90 seconds after the first (completion is only offered then), slept as 60 + 30.
  • test_wait_holds_a_retry_after_too_long_for_a_float_until_its_deadline: a 400-digit Retry-After holds the wait to its deadline (TIMED_OUT, one request) instead of polling early.
  • test_wait_retries_a_rate_limit_without_a_problem_document: four HTML 503 pages, then a completed review, returns COMPLETED; the sleeps are exactly the transport's 1, 2, 4 and the wait's 5.
  • test_wait_sleeps_a_very_long_retry_after_in_bounded_steps: Retry-After: 100000 in a wait with timeout=1e12 sleeps exactly 100000 seconds in steps of at most 60, then completes.
  • test_wait_times_out_when_retry_after_outlasts_its_deadline: Retry-After: 90 in a 60-second wait sleeps to the deadline and returns TIMED_OUT.
  • test_wait_keeps_the_last_review_read_before_the_service_was_unavailable: a RUNNING read followed by 503s returns TIMED_OUT that still carries the RUNNING review, after exactly three reads and sleeps of 5, 60 + 30 (the Retry-After) and 5 (to the deadline).
  • test_wait_still_raises_problems_that_are_not_retryable (400, 401, 403, 404, 500, 502): one request, no sleep, the same status raised.
  • test_wait_raises_an_encoded_rate_limit_it_cannot_read (429, 503): a gzip-encoded problem still raises invalid-response after one request, and is not retryable.
  • test_a_rate_limit_problem_carries_its_retry_after (429, 503): retryable and retry_after on a retried GET and a non-retried POST; a 403 has neither.

Against main, 28 of these 40 cases fail: the 20 cases of the seven waiting tests each raise 429 or 503, and the 8 cases of the last two tests fail only because main has no retryable or retry_after (an encoded problem raises there too). The 12 non-retryable cases pass on both. The existing single-call tests (test_retry_count_is_bounded_for_rate_limit_and_provider_unavailable, test_long_retry_after_is_not_retried_early, the malformed Retry-After cases) pass unchanged.

python -m pytest -q               259 passed, 8 subtests passed
python -m ruff check .            All checks passed
python -m ruff format --check .   237 files already formatted
python -m mypy proofline scripts  Success: no issues found

scripts/check.py does not run on Windows (#87), so I ran its steps directly. On Windows, test_streaming_and_resolution_keep_deadline_outcomes_explicit[sync] fails intermittently on main as well (2 of 12 runs before this change); that is the clock-resolution issue #85 addresses.

Review feedback

I ran Proofline on this change in my fork (apoorvarohilla/sdk-python#2, four runs; this PR squashes the result). On the first commit it reported four warnings, which came down to two issues, and I confirmed both:

  • An encoded or unreadable 429 or 503 is refused by the transport before its retry policy reads it, but the wait retried it by status alone. The wait now continues only for problems marked retryable; test_wait_raises_an_encoded_rate_limit_it_cannot_read did not raise on the first commit and does now.
  • With a very large timeout, the whole Retry-After went to time.sleep, which raises OverflowError near 1e10 seconds. The wait now sleeps toward the Retry-After in steps of at most a minute.

A second run on that commit raised five more warnings:

  • A Retry-After with more digits than a float holds parsed to inf and was discarded, so the wait polled early. Confirmed: it is now kept as inf and the wait holds until its deadline. A single call still refuses it, as before.
  • The docs called unreadable responses not retryable, while a fully received 503 without a problem document (an HTML page from a proxy) is retryable. Confirmed as wording: the behavior was intended and is now tested; the README and docstring say that only a response refused before its body is read, such as an encoded one, is not retryable. (Reported twice.)
  • The 90-second test did not prove when the second request arrived. Confirmed: it now offers completion only after 90 seconds and checks each request's time.
  • That the wait can sample the clock after its deadline check and call get_review with no time left. I disagree that this needs a change: limit_timeout refuses the request before anything is sent when no budget remains, and the wait returns TIMED_OUT, the same as the loop on main.

A third run raised three more, all confirmed:

  • A 429 or 503 body nested too deeply for the JSON decoder raises RecursionError out of problem parsing before it can be marked retryable (reported three times). Confirmed, but it applies to every error status on main, and Don't report caller errors as invalid API responses #75 already catches RecursionError there, so I left it to that PR; with both merged, such a 429 or 503 is retryable.
  • The docs did not mention that retry_after can be inf. They do now.
  • The HTML 503 test checked only the outcome. It now checks the exact sleeps, so it proves the wait held before the final read.

A fourth run raised three more. I fixed one and disagree with two:

  • The unavailable-then-TIMED_OUT test checked only the outcome, which an early exit would also return. Confirmed: it now checks every read and sleep.
  • That the async wait's new hold uses asyncio.sleep, which does not run under Trio. The wait's existing poll sleep and the transport's retry sleep on main already use asyncio.sleep, so a Trio wait already fails there for a RUNNING review; the hold follows that. Moving the client to anyio.sleep would be a separate change.
  • That the README promises retryable but the deadline check can raise first. When the wait's deadline passes during a retry, the transport raises its timeout error rather than the 429 or 503, so there is no rate-limit problem to mark; wait_for_review returns TIMED_OUT, as the same README sentence says.

Related

#75 handles problem bodies nested too deeply to decode (see above).

#85 changes the condition just above this one (which timeout ends a wait as TIMED_OUT); it still re-raises 429 and 503, so the two changes are independent and compose.


Prepared with AI assistance (Claude Code). I reviewed, ran and verified every change and test myself.

apoorvarohilla and others added 2 commits November 23, 2026 18:51
wait_for_review raised the 429 or 503 that ended one read's retries, even
with most of its timeout left, so a CI wait failed on a brief rate limit
or restart. A Retry-After over 60 seconds raised at once, while a shorter
one that outlasted the deadline returned TIMED_OUT.

The transport marks a 429 or 503 whose body it read as retryable and
records its Retry-After. wait_for_review treats a retryable problem like
an unfinished review: it holds the next read until the larger of its poll
interval and the Retry-After has passed, sleeping toward it in steps of at
most a minute within the deadline, then returns TIMED_OUT with the last
review it read. Every other problem, and a 429 or 503 refused before its
body is read, still raises.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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.

1 participant