Skip to content
27 changes: 21 additions & 6 deletions proofline/_sync_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +26 to 29

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Zero connect timeout skips the nonblocking socket attempt

For an IP literal and a live or unset request deadline, timeout=0 sets the budget end to the initial clock reading, so the pre-attempt check always raises.

The caller gets a synthetic ConnectTimeout instead of the socket attempt’s result.

Forward an explicit zero timeout to the socket backend while still enforcing the request deadline.

View finding in Proofline

remaining = deadline - monotonic()
remaining = deadline - (monotonic() if now is None else now)
if remaining <= 0:
raise httpcore2.ReadTimeout("The request deadline expired.")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Request deadline expiry is reported as a connect timeout

Before an address attempt, connect_tcp treats expiry of the request deadline as expiry of the connect budget.

The backend raises httpcore2.ConnectTimeout instead of httpcore2.ReadTimeout when the request deadline expires after lookup or between attempts.

Check the request deadline first using the same clock sample, then raise ConnectTimeout only if the connect budget is exhausted.

Details

The public transport maps both exceptions to httpx2.ReadTimeout; the difference is at the backend boundary.

View finding in Proofline

return remaining if timeout is None else min(timeout, remaining)
Expand Down Expand Up @@ -109,12 +109,27 @@ def connect_tcp(
local_address: str | None = None,
socket_options: Iterable[Any] | None = None,
) -> httpcore2.NetworkStream:
# 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
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:
Expand Down
196 changes: 196 additions & 0 deletions tests/test_sync_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
"""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]] = []
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
Comment on lines +42 to +45

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · DNS budget tests pass even if the lookup timeout is dropped

The fake resolve ignores its timeout, so both DNS budget tests still pass if connect_tcp passes None while keeping the address-attempt deadline.

The tests miss a regression that lets a stalled lookup outlast the connect budget.

Record the resolver timeout and assert that both tests pass the expected 30-second budget.

Details

The six-second lookup still leaves two 12-second shares, and the 30-second lookup still triggers the post-lookup deadline check before any address attempt. Neither outcome depends on the resolver argument.

View finding in Proofline

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still present in this version: Proofline found the issue again. Proofline's finding in this version: The unbounded connection test misses DNS timeouts. View review

Comment thread
prooflinedev[bot] marked this conversation as resolved.
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)
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
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:
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.lookups == [30]
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.lookups == [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)],
)
Comment on lines +176 to +179

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Connect tests miss connections bounded only by the request deadline

The budget matrix never pairs timeout=None with a finite IO_DEADLINE.

The tests would still pass if that combination passed None to both DNS and TCP.

Add (None, 12, 12.0) to the matrix and annotate timeout as float | None so the existing assertions check both calls.

View finding in Proofline

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.lookups == [expected]
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.lookups == [None]
assert network.attempts == [(REFUSED, None), (HEALTHY[0], None)]