Skip to content

Return TIMED_OUT when the wait deadline ends a request, even if the clock lags - #85

Open
ranajawadriaz wants to merge 4 commits into
prooflinedev:mainfrom
ranajawadriaz:fix/sync-wait-timed-out
Open

ranajawadriaz wants to merge 4 commits into
prooflinedev:mainfrom
ranajawadriaz:fix/sync-wait-timed-out

Conversation

@ranajawadriaz

@ranajawadriaz ranajawadriaz commented Nov 22, 2026 •

Copy link
Copy Markdown

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() to QueryPerformanceCounter.

ProoflineClient.wait_for_review can raise ProoflineError with type urn:proofline:sdk:timeout instead of returning TIMED_OUT when its deadline expires while a request is in flight. AsyncProoflineClient.wait_for_review returned TIMED_OUT in every run. This is also why test_body_stream_deadline_does_not_turn_authority_into_timeout fails intermittently on Windows (noted in #75).

How to reproduce

On Windows with Python 3.11:

python -m pytest tests/test_http.py::test_body_stream_deadline_does_not_turn_authority_into_timeout

The [200-sync] and [403-sync] cases fail in roughly two out of three runs. They fail at the wait_for_review(..., timeout=0.3) call against a peer that holds its response headers for 2 s.

The same thing outside the suite:

# A peer that holds its response headers for 2 s.
with ProoflineClient(token, base_url=slow_peer) as client:
    client.wait_for_review(organization_id, review_id, timeout=0.3)
# Windows, Python 3.11.9: raised urn:proofline:sdk:timeout in 11 of 30 runs.

Root cause

When get_review failed 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() reads time.monotonic(). On Windows before Python 3.13, that is GetTickCount64 with 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 returning TIMED_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.

  • New helper. After 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's DEADLINE comes first, in a new context variable DEADLINE_BOUND.
  • Wait decision. Both wait_for_review methods return TIMED_OUT only for an error with status 0 and type TIMEOUT_TYPE from an attempt that the wait deadline bounded. They set and reset DEADLINE_BOUND together with DEADLINE.
  • Status check (Proofline finding 1). An earlier version of this branch dropped the status == 0 check. A problem document's type comes from the server, so an HTTP error whose type is urn:proofline:sdk:timeout could end the wait as TIMED_OUT instead of raising. Requiring status 0 fixes that, because the SDK's own timeout always has status 0.
  • Spent wait budget (Proofline finding 2). limit_timeout() raises only when the wait's own budget is spent, but it did so without setting DEADLINE_BOUND, so the wait could raise the SDK timeout instead of returning TIMED_OUT. limit_timeout() now sets DEADLINE_BOUND before it raises.
  • Clamp order (Proofline finding 3). An earlier version computed the attempt deadline before limit_timeout(), so time spent in the clamp was charged to the attempt. Both transports call limit_timeout() first again. Finding 2's fix already covers the case where the clamp raises.
  • Per-poll flag (Proofline finding 4). The flag from a successful poll was still set during the next poll. A poll that raised the SDK timeout without reaching the transport, such as an overridden get_review, could then end the wait as TIMED_OUT. Both waits now clear DEADLINE_BOUND before each poll.
  • Retries. The flag is recorded per attempt, so it also covers a 503 retry that starts within the HTTP timeout of the deadline.
  • Shared constant. TIMEOUT_TYPE replaces the literal in timeout_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, type urn: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 is urn:proofline:sdk:transport-unavailable from 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.

  1. test_a_request_the_wait_deadline_ends_is_timed_out_while_the_clock_lags uses the suite's fake Clock to make the wait's clock trail the request's by 4 ms, so it reproduces on any OS. It has three cases:

    • The wait deadline ends the only attempt: the wait returns TIMED_OUT.
    • The wait deadline ends the retry after a 503: the wait returns TIMED_OUT.
    • The request's own 30 s timeout ends it mid-wait: the wait still raises.

    It records DEADLINE_BOUND on 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 leaves DEADLINE and DEADLINE_BOUND unset.

  2. test_an_http_problem_named_like_the_sdk_timeout_is_not_timed_out covers finding 1. A 403 whose problem type is urn:proofline:sdk:timeout, arriving while the wait deadline bounds the attempt, must raise as the HTTP error.

  3. test_a_wait_budget_spent_before_the_attempt_starts_is_timed_out covers finding 2. The fake clock moves past the wait's 40 s deadline just before limit_timeout() runs. The wait must return TIMED_OUT.

  4. test_an_attempt_deadline_starts_after_its_clamp covers 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 returns TIMED_OUT.

  5. test_a_poll_that_never_reaches_the_transport_is_not_bound_by_the_wait covers finding 4. After a successful bounded poll, a second poll raises the SDK timeout from an overridden get_review. The wait must raise.

Results on Windows 11 with Python 3.11.9:

Check Before After
Clock-lag test, 6 cases 4 failed (every TIMED_OUT case, both clients) 6 passed
HTTP problem typed as the SDK timeout, 2 cases 2 failed (TIMED_OUT returned, without the status check) 2 passed
Wait budget spent before the attempt starts, 2 cases 2 failed (SDK timeout raised) 2 passed
Attempt deadline after the clamp, 2 cases 2 failed (SDK timeout raised) 2 passed
Poll that never reaches the transport, 2 cases 2 failed (TIMED_OUT returned) 2 passed
test_body_stream_deadline_does_not_turn_authority_into_timeout, 30 runs 19 of 30 runs failed (17 × [200-sync], 12 × [403-sync]) 30 of 30 passed
Peer holding headers for 2 s, wait_for_review(timeout=0.3), 30 runs per client sync: 11 raised, 19 TIMED_OUT; async: 30 TIMED_OUT sync and async: 30 TIMED_OUT
Full test suite 4 of 10 runs failed (only the test above) 233 passed
python scripts/check.py — passed

Each "Before" for findings 1–4 was measured on this branch just before that finding's fix.

I ran scripts/check.py on Windows with PYTHONUTF8=1 set. scripts/generate.py reads openapi.public.json with the locale encoding, and under cp1252 the contract's single § becomes §, so generate.py --check reports a difference. That is unrelated to this change. I did not run the suite on Linux locally; CI covers it.

🤖 Generated with Claude Code

…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>
ranajawadriaz and others added 3 commits November 23, 2026 21:26
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 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