Repository navigation
Return TIMED_OUT when the wait deadline ends a request, even if the clock lags - #1
Open
ranajawadriaz wants to merge 4 commits into
Open
ranajawadriaz wants to merge 4 commits into
ranajawadriaz wants to merge 4 commits into
Conversation
…lock lags wait_for_review decided whether a failed request had been cut short by the wait deadline by reading the clock after the failure. A request stops on its own timer, and on Windows before Python 3.13 time.monotonic() advances in 15.6 ms ticks, so the clock can still show a few milliseconds left after the deadline has ended the request. The sync client then raised urn:proofline:sdk:timeout instead of returning TIMED_OUT. On Windows, test_body_stream_deadline_does_not_turn_authority_into_timeout failed in its sync cases in 19 of 30 runs. Each transport attempt now records whether the wait deadline, rather than the attempt's own timeout, bounds it (DEADLINE_BOUND). Both clients end the wait with TIMED_OUT only for a timeout from such an attempt, and reset the flag with DEADLINE when the wait returns. Any other failure is raised, including a non-timeout status-0 error that lands at the deadline, which previously returned TIMED_OUT. The new test makes the wait's clock trail the request's by 4 ms on the fake clock, so it reproduces on any OS, for both clients, including a 503 retry that the wait deadline ends. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A problem document's type comes from the server, so an HTTP error such as a 403 whose type is urn:proofline:sdk:timeout matched the wait's timeout check while the wait deadline bounded the attempt, and the wait returned TIMED_OUT instead of raising the HTTP ProoflineError. The SDK's own timeout always has status 0, so both wait_for_review methods require status 0 again alongside the timeout type and DEADLINE_BOUND. Found by Proofline's review of this branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…line limit_timeout() raises only when the wait's own budget is spent, but it did so without setting DEADLINE_BOUND. If the budget ran out between attempt_deadline() and limit_timeout() on an attempt whose own deadline came first, the flag stayed false and both review waits raised the SDK timeout instead of returning TIMED_OUT. limit_timeout() now sets DEADLINE_BOUND before it raises. Found by Proofline's second review of this branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Three findings from Proofline's third review of this branch: - Both transports computed the attempt's own deadline before limit_timeout(), so time spent in the clamp was charged to the attempt. limit_timeout() now runs first again; it already sets DEADLINE_BOUND when it raises for a spent wait budget. - Both waits kept DEADLINE_BOUND from a successful poll, so a later poll that raised the SDK timeout without reaching the transport, such as an overridden get_review, ended the wait as TIMED_OUT. Each poll now clears the flag first. - The 503 retry test now checks that the first attempt is not bounded by the wait deadline and the retry is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Observed on Windows with Python 3.11.9. Python 3.12 uses the same 15.6 ms clock; Python 3.13 switched
time.monotonic()toQueryPerformanceCounter.ProoflineClient.wait_for_reviewcan raiseProoflineErrorwith typeurn:proofline:sdk:timeoutinstead of returningTIMED_OUTwhen its deadline expires while a request is in flight.AsyncProoflineClient.wait_for_reviewreturnedTIMED_OUTin every run. This is also whytest_body_stream_deadline_does_not_turn_authority_into_timeoutfails intermittently on Windows (noted in prooflinedev#75).How to reproduce
On Windows with Python 3.11:
The
[200-sync]and[403-sync]cases fail in roughly two out of three runs. They fail at thewait_for_review(..., timeout=0.3)call against a peer that holds its response headers for 2 s.The same thing outside the suite:
Root cause
When
get_reviewfailed inside the wait, the wait re-read the clock (remaining() <= 0) to decide whether its own deadline had ended the request.The request stops on the operating system's timer, but
remaining()readstime.monotonic(). On Windows before Python 3.13, that isGetTickCount64with a resolution of 15.6 ms. Right after the socket read times out at the deadline, the clock can still show a few milliseconds left. I measured up to 4 ms left in about a third of attempts. In those cases the wait re-raised the timeout instead of returningTIMED_OUT.The async client made the same decision. anyio only fires its timeout once the loop clock, which is the same
time.monotonic(), reaches the deadline, so the re-read agreed there and it was not seen failing.Fix
The wait now decides from the deadline each attempt was given, not from a clock read after the failure.
limit_timeout()lets an attempt start,attempt_deadline()computes the attempt's own deadline in both transports, as before. It also records whether the caller'sDEADLINEcomes first, in a new context variableDEADLINE_BOUND.wait_for_reviewmethods returnTIMED_OUTonly for an error with status 0 and typeTIMEOUT_TYPEfrom an attempt that the wait deadline bounded. They set and resetDEADLINE_BOUNDtogether withDEADLINE.status == 0check. A problem document'stypecomes from the server, so an HTTP error whose type isurn:proofline:sdk:timeoutcould end the wait asTIMED_OUTinstead of raising. Requiring status 0 fixes that, because the SDK's own timeout always has status 0.limit_timeout()raises only when the wait's own budget is spent, but it did so without settingDEADLINE_BOUND, so the wait could raise the SDK timeout instead of returningTIMED_OUT.limit_timeout()now setsDEADLINE_BOUNDbefore it raises.limit_timeout(), so time spent in the clamp was charged to the attempt. Both transports calllimit_timeout()first again. Finding 2's fix already covers the case where the clamp raises.get_review, could then end the wait asTIMED_OUT. Both waits now clearDEADLINE_BOUNDbefore each poll.TIMEOUT_TYPEreplaces the literal intimeout_error(), so the wait and the transport share one constant.Behaviour change
Before, any status-0 error raised once the clock had passed the deadline returned
TIMED_OUT. Now only the SDK's own timeout (status 0, typeurn:proofline:sdk:timeout) from a request that the wait deadline bounded does.A non-timeout status-0 error right at the deadline now raises instead of returning
TIMED_OUT, because the deadline did not cause it. An example isurn:proofline:sdk:transport-unavailablefrom a refused connection. HTTP errors still raise, whatever their problem type. No existing test depended on the old behaviour.Testing
There are five new tests, each run for both clients.
test_a_request_the_wait_deadline_ends_is_timed_out_while_the_clock_lagsuses the suite's fakeClockto make the wait's clock trail the request's by 4 ms, so it reproduces on any OS. It has three cases:TIMED_OUT.TIMED_OUT.It records
DEADLINE_BOUNDon every attempt. In the retry case, the first 503 attempt must be unbounded and the retry bounded (Proofline finding 5). It also checks that the wait leavesDEADLINEandDEADLINE_BOUNDunset.test_an_http_problem_named_like_the_sdk_timeout_is_not_timed_outcovers finding 1. A 403 whose problem type isurn:proofline:sdk:timeout, arriving while the wait deadline bounds the attempt, must raise as the HTTP error.test_a_wait_budget_spent_before_the_attempt_starts_is_timed_outcovers finding 2. The fake clock moves past the wait's 40 s deadline just beforelimit_timeout()runs. The wait must returnTIMED_OUT.test_an_attempt_deadline_starts_after_its_clampcovers finding 3. The clamp takes 5 s. The attempt's own 30 s deadline must start after it, so the wait's 33 s deadline bounds the attempt and the wait returnsTIMED_OUT.test_a_poll_that_never_reaches_the_transport_is_not_bound_by_the_waitcovers finding 4. After a successful bounded poll, a second poll raises the SDK timeout from an overriddenget_review. The wait must raise.Results on Windows 11 with Python 3.11.9:
TIMED_OUTcase, both clients)TIMED_OUTreturned, without the status check)TIMED_OUTreturned)test_body_stream_deadline_does_not_turn_authority_into_timeout, 30 runs[200-sync], 12 ×[403-sync])wait_for_review(timeout=0.3), 30 runs per clientTIMED_OUT; async: 30TIMED_OUTTIMED_OUTpython scripts/check.pyEach "Before" for findings 1–4 was measured on this branch just before that finding's fix.
I ran
scripts/check.pyon Windows withPYTHONUTF8=1set.scripts/generate.pyreadsopenapi.public.jsonwith the locale encoding, and under cp1252 the contract's single§becomes§, sogenerate.py --checkreports a difference. That is unrelated to this change. I did not run the suite on Linux locally; CI covers it.🤖 Generated with Claude Code