From c7c4b60bf9ef67965bb50ff5674661517a635043 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 25 Oct 2026 00:31:56 +0000 Subject: [PATCH] Time out retryable problems whose body outlives the deadline When the request or wait deadline passed after a 429 or 503 body chunk arrived, check_stream raised unreadable_problem(status) instead of the timeout that an expired read of the same body already produced. A wait_for_review racing its deadline against a 503 therefore raised HTTP 503 rather than returning TIMED_OUT, which failed test_retry_after_is_bounded_by_wait_deadline[async] intermittently in CI. Treat a deadline seen in check_stream for 429 and 503 the same way as an expired read. Other problems, including 403, keep their status. The new test advances a fake clock while a received chunk is checked, so it reproduces the race on every run in both sync and async modes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01G8YzhJ7iLeZF8kY4HTma3u --- proofline/_transport.py | 3 ++- tests/test_http.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/proofline/_transport.py b/proofline/_transport.py index 852dc55d..0eabf2fa 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -211,7 +211,8 @@ def check_stream(size: int, status: int, deadline: float) -> None: ) budget = remaining() if monotonic() >= deadline or (budget is not None and budget <= 0): - if 200 <= status < 300: + # Retryable statuses time out as an expired read does; other problems keep their status. + if 200 <= status < 300 or status in (429, 503): raise timeout_error() raise unreadable_problem(status) diff --git a/tests/test_http.py b/tests/test_http.py index 2a2287da..15e11fdf 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1008,6 +1008,36 @@ async def test_retry_after_is_bounded_by_wait_deadline(mode: str) -> None: assert len(server.requests) == 1 +@MODES +@pytest.mark.parametrize("status", [429, 503, 403]) +async def test_deadline_expiring_with_a_received_problem_chunk_keeps_retry_policy( + mode: str, status: int, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + checked = _transport.check_stream + + def late(size: int, response_status: int, deadline: float) -> None: + # The wait deadline passes while a received body chunk is still being checked. + clock.now += 10 + checked(size, response_status, deadline) + + monkeypatch.setattr(_transport, "check_stream", late) + with Server( + lambda request, index: Reply(status, {"detail": PRIVATE}, {"Retry-After": "2"}) + ) as server: + async with session(mode, server.url) as client: + if status == 403: + with pytest.raises(ProoflineError) as raised: + await call(client, "wait_for_review", ORG, REVIEW, timeout=1) + assert raised.value.status == 403 + else: + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=1) + assert result.status == "TIMED_OUT" + assert result.review_url == f"{server.url}/orgs/by-id/{ORG}/reviews/{REVIEW}" + assert len(server.requests) == 1 + assert clock.delays == [] + + @MODES @pytest.mark.parametrize("status", [200, 403]) async def test_body_stream_deadline_does_not_turn_authority_into_timeout(