From d8957be616fdee7746d06aef2b1425e1a280b2d0 Mon Sep 17 00:00:00 2001 From: rushmithanadipally Date: Sun, 6 Dec 2026 13:58:17 -0500 Subject: [PATCH] Return TIMED_OUT on early socket timeout at deadline --- proofline/_transport.py | 12 ++++++++++ proofline/client.py | 4 ++-- tests/test_http.py | 49 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/proofline/_transport.py b/proofline/_transport.py index 7fa914b4..273f3019 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -319,6 +319,18 @@ def remaining() -> float | None: return None if deadline is None else max(0.0, deadline - monotonic()) +# Some platforms (Windows) end a socket wait a fraction of a millisecond before +# the timeout it was given. A transport timeout that fires this close to the +# review deadline is the deadline expiring, not a separate failure. +DEADLINE_SLACK = 0.005 + + +def deadline_reached() -> bool: + """True when the review wait budget is spent, allowing for early socket timeouts.""" + budget = remaining() + return budget is None or budget <= DEADLINE_SLACK + + def limit_timeout(request: httpx2.Request) -> None: budget = remaining() if budget is None: diff --git a/proofline/client.py b/proofline/client.py index 72f2514e..56f5a9e2 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -570,7 +570,7 @@ def wait_for_review( except _transport.RetryDeadlineExceeded: break except ProoflineError as error: - if timeout > 0 and error.status == 0 and (_transport.remaining() or 0) <= 0: + if timeout > 0 and error.status == 0 and _transport.deadline_reached(): break raise result = outcome(last, url, review_id=review_id, head_commit_sha=head_commit_sha) @@ -958,7 +958,7 @@ async def wait_for_review( except _transport.RetryDeadlineExceeded: break except ProoflineError as error: - if timeout > 0 and error.status == 0 and (_transport.remaining() or 0) <= 0: + if timeout > 0 and error.status == 0 and _transport.deadline_reached(): break raise result = outcome(last, url, review_id=review_id, head_commit_sha=head_commit_sha) diff --git a/tests/test_http.py b/tests/test_http.py index 456185ed..e4d93728 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -2952,3 +2952,52 @@ def respond(request: Request, index: int) -> Reply: assert len(exported.rows) == 1 assert exported.rows[0]["claim"] == row["claim"] assert exported.next_cursor == "next-page" + + +@MODES +async def test_wait_for_review_treats_early_socket_timeout_as_deadline( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + # Windows can end a socket wait a fraction of a millisecond early, leaving a + # tiny positive budget when the transport reports its timeout. That is the + # deadline expiring, so wait_for_review must return TIMED_OUT. + with monkeypatch.context() as patch: + clock = Clock(patch) + + def early_timeout() -> ProoflineError: + clock.now += 0.5 - 0.0002 + return _transport.timeout_error() + + async def get_review_async(*args: Any, **kwargs: Any) -> Any: + raise early_timeout() + + def get_review_sync(*args: Any, **kwargs: Any) -> Any: + raise early_timeout() + + patch.setattr(ProoflineClient, "get_review", get_review_sync) + patch.setattr(AsyncProoflineClient, "get_review", get_review_async) + async with session(mode, "http://127.0.0.1:9") as client: + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=0.5) + assert result.status == "TIMED_OUT" + + +@MODES +async def test_wait_for_review_still_raises_timeout_with_budget_left( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + # A transport timeout well before the deadline is a real error, not TIMED_OUT. + with monkeypatch.context() as patch: + Clock(patch) + + async def get_review_async(*args: Any, **kwargs: Any) -> Any: + raise _transport.timeout_error() + + def get_review_sync(*args: Any, **kwargs: Any) -> Any: + raise _transport.timeout_error() + + patch.setattr(ProoflineClient, "get_review", get_review_sync) + patch.setattr(AsyncProoflineClient, "get_review", get_review_async) + async with session(mode, "http://127.0.0.1:9") as client: + with pytest.raises(ProoflineError) as raised: + await call(client, "wait_for_review", ORG, REVIEW, timeout=30) + assert raised.value.status == 0