From ccbb558c89168d768c8ee589caab7c1cceca8963 Mon Sep 17 00:00:00 2001 From: P1Manav <22bce281@nirmauni.ac.in> Date: Tue, 24 Nov 2026 14:47:42 +0530 Subject: [PATCH] fix(client): treat timeout errors near deadline as TIMED_OUT on Windows --- proofline/client.py | 12 ++++++++++-- tests/test_http.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/proofline/client.py b/proofline/client.py index 1d8c1519..58ada7eb 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -101,6 +101,8 @@ ReviewRoutingViewType1, ReviewRoutingViewType2, ) +# Socket timeouts on Windows can complete a fraction of a second early due to timer resolution. +_DEADLINE_TOLERANCE_SECONDS = 0.025 class ReviewFilters(TypedDict, total=False): @@ -568,7 +570,10 @@ def wait_for_review( try: last = self.get_review(organization_id, review_id) except ProoflineError as error: - if error.status == 0 and (_transport.remaining() or 0) <= 0: + if ( + error.status == 0 + and (_transport.remaining() or 0) <= _DEADLINE_TOLERANCE_SECONDS + ): break raise if (_transport.remaining() or 0) <= 0: @@ -954,7 +959,10 @@ async def wait_for_review( try: last = await self.get_review(organization_id, review_id) except ProoflineError as error: - if error.status == 0 and (_transport.remaining() or 0) <= 0: + if ( + error.status == 0 + and (_transport.remaining() or 0) <= _DEADLINE_TOLERANCE_SECONDS + ): break raise if (_transport.remaining() or 0) <= 0: diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..c45f3b8c 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -2586,3 +2586,32 @@ 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_tolerates_early_socket_timeout_jitter( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + def raise_timeout(*args: Any, **kwargs: Any) -> Any: + raise _transport.timeout_error() + + async def raise_timeout_async(*args: Any, **kwargs: Any) -> Any: + raise _transport.timeout_error() + + target = raise_timeout_async if mode == "async" else raise_timeout + + with Server(lambda request, index: Reply(status=200, body=review("PENDING"))) as server: + async with session(mode, server.url) as client: + with monkeypatch.context() as patch: + patch.setattr(client, "get_review", target) + patch.setattr(_transport, "remaining", lambda: 0.0005) + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=0.1) + assert result.status == "TIMED_OUT" + + with monkeypatch.context() as patch: + patch.setattr(client, "get_review", target) + patch.setattr(_transport, "remaining", lambda: 0.05) + with pytest.raises(ProoflineError) as raised: + await call(client, "wait_for_review", ORG, REVIEW, timeout=0.1) + assert raised.value.status == 0 + assert raised.value.type == "urn:proofline:sdk:timeout"