Repository navigation
Keep waiting for a review through rate limits and unavailability - #96
Open
apoorvarohilla wants to merge 2 commits into
Open
apoorvarohilla wants to merge 2 commits into
apoorvarohilla wants to merge 2 commits into
Conversation
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
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.
Problem
wait_for_reviewraised 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):Retry-After: 0), then the review isCOMPLETEDCOMPLETEDafter 6 requests, 5.0 sRetry-After: 90TIMED_OUTafter 1 request, 3.0 sThe outcome also depended on the length of the header rather than on the wait's deadline.
test_retry_after_is_bounded_by_wait_deadlinepins that a 503 withRetry-After: 20against a 10-second wait returnsTIMED_OUT, and #12 describesTIMED_OUTas the only consistent outcome for a retryable problem in a wait. ButRetry-After: 61against 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 aRetry-Afterover 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:wait_for_reviewtreats it like an unfinished review. It holds the next read until the larger of its own poll interval and the problem'sRetry-Afterhas passed, sleeping toward it in steps of at most a minute and never past the deadline, and reads again. When the deadline passes it returnsTIMED_OUTwith the last review it read, as it already does for a review that is still running.retryableand records their parsedRetry-After(retry_after,inffor more seconds than a float holds,Nonewhen absent or invalid) on theProoflineErrorit raises.retry_delayis split intoretry_after(parse) and the existing 60-second retry bound, with unchanged results. Callers of non-retried requests such asrequest_reviewcan read the same value.The README and the
wait_for_reviewdocstring describe the new behavior.Verification
New tests in
tests/test_http.py, all on both clients with the suite's fakeClock:test_wait_keeps_polling_after_one_request_exhausts_its_retries(429, 503): five 503s then a completed review returnsCOMPLETED; 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: withRetry-After: 90the 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-digitRetry-Afterholds 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, returnsCOMPLETED; 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: 100000in a wait withtimeout=1e12sleeps exactly 100000 seconds in steps of at most 60, then completes.test_wait_times_out_when_retry_after_outlasts_its_deadline:Retry-After: 90in a 60-second wait sleeps to the deadline and returnsTIMED_OUT.test_wait_keeps_the_last_review_read_before_the_service_was_unavailable: aRUNNINGread followed by 503s returnsTIMED_OUTthat still carries theRUNNINGreview, after exactly three reads and sleeps of 5, 60 + 30 (theRetry-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 raisesinvalid-responseafter one request, and is notretryable.test_a_rate_limit_problem_carries_its_retry_after(429, 503):retryableandretry_afteron a retriedGETand a non-retriedPOST; 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 becausemainhas noretryableorretry_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 malformedRetry-Aftercases) pass unchanged.scripts/check.pydoes not run on Windows (#87), so I ran its steps directly. On Windows,test_streaming_and_resolution_keep_deadline_outcomes_explicit[sync]fails intermittently onmainas 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:
retryable;test_wait_raises_an_encoded_rate_limit_it_cannot_readdid not raise on the first commit and does now.Retry-Afterwent totime.sleep, which raisesOverflowErrornear 1e10 seconds. The wait now sleeps toward theRetry-Afterin steps of at most a minute.A second run on that commit raised five more warnings:
Retry-Afterwith more digits than a float holds parsed toinfand was discarded, so the wait polled early. Confirmed: it is now kept asinfand the wait holds until its deadline. A single call still refuses it, as before.get_reviewwith no time left. I disagree that this needs a change:limit_timeoutrefuses the request before anything is sent when no budget remains, and the wait returnsTIMED_OUT, the same as the loop onmain.A third run raised three more, all confirmed:
RecursionErrorout of problem parsing before it can be marked retryable (reported three times). Confirmed, but it applies to every error status onmain, and Don't report caller errors as invalid API responses #75 already catchesRecursionErrorthere, so I left it to that PR; with both merged, such a 429 or 503 is retryable.retry_aftercan beinf. They do now.A fourth run raised three more. I fixed one and disagree with two:
TIMED_OUTtest checked only the outcome, which an early exit would also return. Confirmed: it now checks every read and sleep.asyncio.sleep, which does not run under Trio. The wait's existing poll sleep and the transport's retry sleep onmainalready useasyncio.sleep, so a Trio wait already fails there for aRUNNINGreview; the hold follows that. Moving the client toanyio.sleepwould be a separate change.retryablebut 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_reviewreturnsTIMED_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.