From 549d2e1a0e7495eb49ba0a6f0f58a3d61ec0a708 Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Butt <168034895+1AbdullahButt@users.noreply.github.com> Date: Mon, 23 Nov 2026 03:00:17 +0500 Subject: [PATCH 1/8] Test that one silent address cannot spend the whole connect budget ProoflineClient connects to each address of a host in turn, but hands the first one all the time left before the request deadline. When that address never answers, the deadline expires before the next address is tried. These tests use a fake clock, resolver and connector, so they open no sockets and never sleep. Seven fail on main; the five that pass guard the single-address, deadline and no-timeout paths that must not change. Co-Authored-By: Claude Opus 5.5 --- tests/test_sync_connect.py | 166 +++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 tests/test_sync_connect.py diff --git a/tests/test_sync_connect.py b/tests/test_sync_connect.py new file mode 100644 index 00000000..196f4d5f --- /dev/null +++ b/tests/test_sync_connect.py @@ -0,0 +1,166 @@ +"""The synchronous backend shares one connect budget across a host's addresses. + +A fake clock, resolver and connector stand in for the network, so these tests open no +sockets and never sleep: an address that never answers uses exactly the time it is +given, and a refused address fails at once. +""" + +from __future__ import annotations + +from collections.abc import Iterable, Iterator +from contextlib import contextmanager +from typing import Any + +import httpcore2 +import pytest + +from proofline import _sync_http + +# Documentation addresses (RFC 3849, RFC 5737): nothing here reaches a network. +SILENT = ("2001:db8::1", "2001:db8::2", "2001:db8::3", "2001:db8::4") +REFUSED = "2001:db8::dead" +HEALTHY = ("192.0.2.1", "192.0.2.2") + + +class Connected(httpcore2.NetworkStream): + def __init__(self, address: str) -> None: + self.address = address + + def get_extra_info(self, info: str) -> Any: + return self.address if info == "server_addr" else None + + +class Network: + def __init__( + self, monkeypatch: pytest.MonkeyPatch, addresses: Iterable[str], lookup_seconds: float = 0 + ) -> None: + self.now = 100.0 + self.attempts: list[tuple[str, float | None]] = [] + resolved = list(addresses) + + def resolve(host: str, port: int, timeout: float | None) -> list[str]: + self.now += lookup_seconds + return resolved + + def connect( + backend: httpcore2.SyncBackend, + host: str, + port: int, + timeout: float | None = None, + local_address: str | None = None, + socket_options: Any = None, + ) -> httpcore2.NetworkStream: + self.attempts.append((host, timeout)) + if host in SILENT: + assert timeout is not None, "an unbounded attempt at a silent address never ends" + self.now += timeout + raise httpcore2.ConnectTimeout("timed out") + if host == REFUSED: + raise httpcore2.ConnectError("refused") + return Connected(host) + + monkeypatch.setattr(_sync_http, "monotonic", lambda: self.now) + monkeypatch.setattr(_sync_http, "resolve", resolve) + monkeypatch.setattr(httpcore2.SyncBackend, "connect_tcp", connect) + + @contextmanager + def deadline(self, seconds: float | None) -> Iterator[None]: + token = _sync_http.IO_DEADLINE.set(None if seconds is None else self.now + seconds) + try: + yield + finally: + _sync_http.IO_DEADLINE.reset(token) + + def connect(self, timeout: float | None) -> httpcore2.NetworkStream: + return _sync_http.NetworkBackend().connect_tcp("dual.proofline.invalid", 443, timeout) + + +def test_an_address_that_never_answers_leaves_time_for_the_next( + monkeypatch: pytest.MonkeyPatch, +) -> None: + network = Network(monkeypatch, [SILENT[0], HEALTHY[0]]) + with network.deadline(30): + stream = network.connect(30) + assert stream.get_extra_info("server_addr") == HEALTHY[0] + assert network.attempts == [(SILENT[0], 15.0), (HEALTHY[0], 15.0)] + + +def test_two_silent_ipv6_addresses_still_reach_ipv4(monkeypatch: pytest.MonkeyPatch) -> None: + # proofline.sh publishes two AAAA and two A records; a broken IPv6 route drops both. + network = Network(monkeypatch, [SILENT[0], SILENT[1], HEALTHY[0], HEALTHY[1]]) + with network.deadline(30): + stream = network.connect(30) + assert stream.get_extra_info("server_addr") == HEALTHY[0] + assert network.attempts == [(SILENT[0], 7.5), (SILENT[1], 7.5), (HEALTHY[0], 7.5)] + assert network.now == 115.0 + + +def test_addresses_that_never_answer_share_the_deadline(monkeypatch: pytest.MonkeyPatch) -> None: + network = Network(monkeypatch, SILENT[:3]) + with network.deadline(30), pytest.raises(httpcore2.ConnectTimeout): + network.connect(30) + assert network.attempts == [(SILENT[0], 10.0), (SILENT[1], 10.0), (SILENT[2], 10.0)] + assert network.now == 130.0 + + +def test_without_a_request_deadline_the_connect_timeout_bounds_all_addresses( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # ProoflineClient always sets a request deadline; the bare transport does not, so + # the shares must come from the time actually left, not from the full timeout. + network = Network(monkeypatch, SILENT) + with network.deadline(None), pytest.raises(httpcore2.ConnectTimeout): + network.connect(30) + shares = [timeout for _, timeout in network.attempts] + assert shares == [7.5, 7.5, 7.5, 7.5] + assert sum(share or 0 for share in shares) <= 30 + assert network.now - 100.0 <= 30 + + +def test_the_lookup_and_the_attempts_share_one_connect_timeout( + monkeypatch: pytest.MonkeyPatch, +) -> None: + network = Network(monkeypatch, [SILENT[0], HEALTHY[0]], lookup_seconds=6) + with network.deadline(None): + stream = network.connect(30) + assert stream.get_extra_info("server_addr") == HEALTHY[0] + assert network.attempts == [(SILENT[0], 12.0), (HEALTHY[0], 12.0)] + assert network.now - 100.0 <= 30 + + +def test_a_lookup_that_uses_the_whole_timeout_leaves_no_attempt( + monkeypatch: pytest.MonkeyPatch, +) -> None: + network = Network(monkeypatch, [HEALTHY[0]], lookup_seconds=30) + with network.deadline(None), pytest.raises(httpcore2.ConnectTimeout): + network.connect(30) + assert network.attempts == [] + + +def test_a_refused_address_passes_its_time_to_the_next(monkeypatch: pytest.MonkeyPatch) -> None: + network = Network(monkeypatch, [REFUSED, HEALTHY[0]]) + with network.deadline(30): + stream = network.connect(30) + assert stream.get_extra_info("server_addr") == HEALTHY[0] + assert network.attempts == [(REFUSED, 15.0), (HEALTHY[0], 30.0)] + + +@pytest.mark.parametrize( + ("timeout", "deadline", "expected"), + [(30, 30, 30.0), (30, 12, 12.0), (5, 30, 5.0), (30, None, 30.0)], +) +def test_a_single_address_keeps_the_whole_budget( + monkeypatch: pytest.MonkeyPatch, timeout: float, deadline: float | None, expected: float +) -> None: + network = Network(monkeypatch, [HEALTHY[0]]) + with network.deadline(deadline): + network.connect(timeout) + assert network.attempts == [(HEALTHY[0], expected)] + + +def test_no_timeout_and_no_deadline_stay_unbounded(monkeypatch: pytest.MonkeyPatch) -> None: + network = Network(monkeypatch, [REFUSED, HEALTHY[0]]) + with network.deadline(None): + stream = network.connect(None) + assert stream.get_extra_info("server_addr") == HEALTHY[0] + assert network.attempts == [(REFUSED, None), (HEALTHY[0], None)] From 4399408d0e9304d9c0002f121bc0fbb02fee393a Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Butt <168034895+1AbdullahButt@users.noreply.github.com> Date: Mon, 23 Nov 2026 03:02:12 +0500 Subject: [PATCH 2/8] Share the connect budget across a host's addresses The synchronous backend tried each resolved address in turn but gave the first one all the time left before the request deadline. An address that never answers, such as an IPv6 address on a machine with a broken IPv6 route, therefore used the whole budget, and the deadline expired before a healthy address was tried. The async client and stock httpx2 both connect in that situation. Each attempt now gets an equal share of the time still left, measured from one budget that also covers the name lookup. Unused time carries over, so a refused address costs nothing and the last address gets all that remains. A single address, address order, error mapping and the overall deadline are unchanged. Without a request deadline the connect timeout now bounds the lookup and all attempts together instead of each attempt separately. Co-Authored-By: Claude Opus 5.5 --- proofline/_sync_http.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/proofline/_sync_http.py b/proofline/_sync_http.py index 0ef0b5b0..392a2bd3 100644 --- a/proofline/_sync_http.py +++ b/proofline/_sync_http.py @@ -109,12 +109,22 @@ def connect_tcp( local_address: str | None = None, socket_options: Iterable[Any] | None = None, ) -> httpcore2.NetworkStream: + # One budget covers the lookup and every address. Each attempt gets an equal share + # of the time still left, so an address that never answers cannot spend the time + # of those after it; unused time carries over, and the last gets all that remains. + budget = bounded_timeout(timeout) + end = None if budget is None else monotonic() + budget + addresses = resolve(host, port, budget) failure: httpcore2.ConnectError | httpcore2.ConnectTimeout | None = None - for address in resolve(host, port, bounded_timeout(timeout)): + for position, address in enumerate(addresses): + share: float | None = None + if end is not None: + left = end - monotonic() + if left <= 0: + raise httpcore2.ConnectTimeout("The connection did not finish in time.") + share = left / (len(addresses) - position) try: - stream = super().connect_tcp( - address, port, bounded_timeout(timeout), local_address, socket_options - ) + stream = super().connect_tcp(address, port, share, local_address, socket_options) except (httpcore2.ConnectError, httpcore2.ConnectTimeout) as error: failure = error else: From 810581218d75c068ed8f5efb6ed0e49f2c32b5da Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Butt <168034895+1AbdullahButt@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:53:31 +0500 Subject: [PATCH 3/8] Test that time between clock reads cannot move the request deadline connect_tcp takes the time left from one clock reading and the end time from a second, so when the request deadline is the tighter limit the end lands after it by the time between the two. The new test advances the fake clock between readings and fails on the current code (the attempt gets 30 s instead of 29 s). Co-Authored-By: Claude Opus 5.5 --- tests/test_sync_connect.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/test_sync_connect.py b/tests/test_sync_connect.py index 196f4d5f..af896615 100644 --- a/tests/test_sync_connect.py +++ b/tests/test_sync_connect.py @@ -117,6 +117,29 @@ def test_without_a_request_deadline_the_connect_timeout_bounds_all_addresses( assert network.now - 100.0 <= 30 +def test_time_between_clock_reads_does_not_move_the_request_deadline( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # The clock moves on right after its first reading (here by 1 s, in practice by + # microseconds). The attempt must still end at the request deadline, not after it. + network = Network(monkeypatch, [SILENT[0]]) + readings = 0 + + def monotonic() -> float: + nonlocal readings + readings += 1 + now = network.now + if readings == 1: + network.now += 1 + return now + + monkeypatch.setattr(_sync_http, "monotonic", monotonic) + with network.deadline(30), pytest.raises(httpcore2.ConnectTimeout): + network.connect(60) + assert network.attempts == [(SILENT[0], 29.0)] + assert network.now == 130.0 + + def test_the_lookup_and_the_attempts_share_one_connect_timeout( monkeypatch: pytest.MonkeyPatch, ) -> None: From c55b1f7409ea1c09df2784f174bbced5ca2b4ef8 Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Butt <168034895+1AbdullahButt@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:53:59 +0500 Subject: [PATCH 4/8] Take the connect budget and its end from one clock reading bounded_timeout read the clock and connect_tcp read it again to place the end, so the end could land after the request deadline by the time between the two readings. Read the clock once and pass that reading to bounded_timeout. Co-Authored-By: Claude Opus 5.5 --- proofline/_sync_http.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/proofline/_sync_http.py b/proofline/_sync_http.py index 392a2bd3..61e99867 100644 --- a/proofline/_sync_http.py +++ b/proofline/_sync_http.py @@ -23,11 +23,11 @@ IO_DEADLINE: ContextVar[float | None] = ContextVar("proofline_io_deadline", default=None) -def bounded_timeout(timeout: float | None) -> float | None: +def bounded_timeout(timeout: float | None, now: float | None = None) -> float | None: deadline = IO_DEADLINE.get() if deadline is None: return timeout - remaining = deadline - monotonic() + remaining = deadline - (monotonic() if now is None else now) if remaining <= 0: raise httpcore2.ReadTimeout("The request deadline expired.") return remaining if timeout is None else min(timeout, remaining) @@ -112,8 +112,11 @@ def connect_tcp( # One budget covers the lookup and every address. Each attempt gets an equal share # of the time still left, so an address that never answers cannot spend the time # of those after it; unused time carries over, and the last gets all that remains. - budget = bounded_timeout(timeout) - end = None if budget is None else monotonic() + budget + # The budget and its end come from one clock reading, so the end is never later + # than the request deadline. + now = monotonic() + budget = bounded_timeout(timeout, now) + end = None if budget is None else now + budget addresses = resolve(host, port, budget) failure: httpcore2.ConnectError | httpcore2.ConnectTimeout | None = None for position, address in enumerate(addresses): From 6439ff7caf5295fcd567e229cac2a2608d267523 Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Butt <168034895+1AbdullahButt@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:27:00 +0500 Subject: [PATCH 5/8] Check the timeout connect_tcp gives the host name lookup The fake resolver ignored its timeout, so tests/test_sync_connect.py still passed when connect_tcp gave the lookup no timeout at all. It now records the timeout, and the lookup and single-address tests assert it equals the budget; with resolve(host, port, None) six of them fail. Co-Authored-By: Claude Opus 5.5 --- tests/test_sync_connect.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_sync_connect.py b/tests/test_sync_connect.py index af896615..c7593064 100644 --- a/tests/test_sync_connect.py +++ b/tests/test_sync_connect.py @@ -36,9 +36,11 @@ def __init__( ) -> None: self.now = 100.0 self.attempts: list[tuple[str, float | None]] = [] + self.lookups: list[float | None] = [] resolved = list(addresses) def resolve(host: str, port: int, timeout: float | None) -> list[str]: + self.lookups.append(timeout) self.now += lookup_seconds return resolved @@ -147,6 +149,7 @@ def test_the_lookup_and_the_attempts_share_one_connect_timeout( with network.deadline(None): stream = network.connect(30) assert stream.get_extra_info("server_addr") == HEALTHY[0] + assert network.lookups == [30] assert network.attempts == [(SILENT[0], 12.0), (HEALTHY[0], 12.0)] assert network.now - 100.0 <= 30 @@ -157,6 +160,7 @@ def test_a_lookup_that_uses_the_whole_timeout_leaves_no_attempt( network = Network(monkeypatch, [HEALTHY[0]], lookup_seconds=30) with network.deadline(None), pytest.raises(httpcore2.ConnectTimeout): network.connect(30) + assert network.lookups == [30] assert network.attempts == [] @@ -178,6 +182,7 @@ def test_a_single_address_keeps_the_whole_budget( network = Network(monkeypatch, [HEALTHY[0]]) with network.deadline(deadline): network.connect(timeout) + assert network.lookups == [expected] assert network.attempts == [(HEALTHY[0], expected)] From 7c285ded1ecd5b8d993745cabf47f0b07783cbd9 Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Butt <168034895+1AbdullahButt@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:27:00 +0500 Subject: [PATCH 6/8] Say what the connect budget covers for the host name lookup resolve() measures its wait from once its worker thread has started, so a lookup that times out can pass the end of the budget by that start-up time. The comment said one budget covers the lookup; it now says exactly what holds. resolve() is left unchanged: open PR #53 rewrites those lines with an absolute deadline. Co-Authored-By: Claude Opus 5.5 --- proofline/_sync_http.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/proofline/_sync_http.py b/proofline/_sync_http.py index 61e99867..737878b2 100644 --- a/proofline/_sync_http.py +++ b/proofline/_sync_http.py @@ -109,11 +109,13 @@ def connect_tcp( local_address: str | None = None, socket_options: Iterable[Any] | None = None, ) -> httpcore2.NetworkStream: - # One budget covers the lookup and every address. Each attempt gets an equal share - # of the time still left, so an address that never answers cannot spend the time - # of those after it; unused time carries over, and the last gets all that remains. - # The budget and its end come from one clock reading, so the end is never later - # than the request deadline. + # The lookup may wait the whole budget, and the addresses share what it leaves before + # the end. Each attempt gets an equal share of the time still left, so an address that + # never answers cannot spend the time of those after it; unused time carries over, and + # the last gets all that remains. The budget and the end come from one clock reading, + # so the end is never later than the request deadline. resolve() starts its wait once + # its worker thread is running, so a lookup that times out can pass the end by that + # start-up time; no address is tried after the end. now = monotonic() budget = bounded_timeout(timeout, now) end = None if budget is None else now + budget From 9b8f0c28e089586cce2525adead7a536d1f0b1bb Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Butt <168034895+1AbdullahButt@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:39:06 +0500 Subject: [PATCH 7/8] Check the lookup timeout when there is no request deadline test_without_a_request_deadline_the_connect_timeout_bounds_all_addresses still passed when connect_tcp gave the lookup no timeout. It now asserts the lookup gets the 30-second budget, and fails with resolve(host, port, None). Co-Authored-By: Claude Opus 5.5 --- tests/test_sync_connect.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_sync_connect.py b/tests/test_sync_connect.py index c7593064..8db408a1 100644 --- a/tests/test_sync_connect.py +++ b/tests/test_sync_connect.py @@ -113,6 +113,7 @@ def test_without_a_request_deadline_the_connect_timeout_bounds_all_addresses( network = Network(monkeypatch, SILENT) with network.deadline(None), pytest.raises(httpcore2.ConnectTimeout): network.connect(30) + assert network.lookups == [30] shares = [timeout for _, timeout in network.attempts] assert shares == [7.5, 7.5, 7.5, 7.5] assert sum(share or 0 for share in shares) <= 30 From f35a2f7f2dc6581ece3fb0e0dcb77bf17778bdb1 Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Butt <168034895+1AbdullahButt@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:48:26 +0500 Subject: [PATCH 8/8] Check the lookup stays unbounded with no timeout and no deadline test_no_timeout_and_no_deadline_stay_unbounded still passed if connect_tcp gave the lookup a finite timeout when there was no budget. It now asserts the lookup gets None, and fails when resolve() is given 5 s. Co-Authored-By: Claude Opus 5.5 --- tests/test_sync_connect.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_sync_connect.py b/tests/test_sync_connect.py index 8db408a1..48081935 100644 --- a/tests/test_sync_connect.py +++ b/tests/test_sync_connect.py @@ -192,4 +192,5 @@ def test_no_timeout_and_no_deadline_stay_unbounded(monkeypatch: pytest.MonkeyPat with network.deadline(None): stream = network.connect(None) assert stream.get_extra_info("server_addr") == HEALTHY[0] + assert network.lookups == [None] assert network.attempts == [(REFUSED, None), (HEALTHY[0], None)]