Repository navigation
Leave time for later addresses when the first one does not answer (sync client) - #1
1AbdullahButt wants to merge 8 commits into
Conversation
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
| def resolve(host: str, port: int, timeout: float | None) -> list[str]: | ||
| self.now += lookup_seconds | ||
| return resolved |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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 <noreply@anthropic.com>
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 prooflinedev#53 rewrites those lines with an absolute deadline. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| remaining = deadline - monotonic() | ||
| remaining = deadline - (monotonic() if now is None else now) | ||
| if remaining <= 0: | ||
| raise httpcore2.ReadTimeout("The request deadline expired.") |
There was a problem hiding this comment.
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.
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
| def bounded_timeout(timeout: float | None, now: float | None = None) -> float | None: | ||
| deadline = IO_DEADLINE.get() | ||
| if deadline is None: | ||
| return timeout |
There was a problem hiding this comment.
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.
| @pytest.mark.parametrize( | ||
| ("timeout", "deadline", "expected"), | ||
| [(30, 30, 30.0), (30, 12, 12.0), (5, 30, 5.0), (30, None, 30.0)], | ||
| ) |
There was a problem hiding this comment.
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.
ProoflineClientcannot reach the API when the first address a host resolves to never answers, even though a later address is healthy.AsyncProoflineClientand stock httpx2 both connect in that situation. This change gives each address an equal share of the connect time still left, so a silent address no longer uses up the time of the ones after it. One function changes:NetworkBackend.connect_tcpinproofline/_sync_http.py.Known limit: IPv4 behind two silent IPv6 addresses waits half the budget
proofline.shpublishes two AAAA and two A records, and IPv6 addresses come first. With a broken IPv6 route, the sync client now spends 7.5 s on each IPv6 address and reaches IPv4 after 15 s at the default 30 s timeout. Before this change it never reached IPv4 at all. The async client gets there in about 0.25 s because anyio races addresses ("Happy Eyeballs", RFC 8305).I did not add a race to the sync client. It would need threads to run connection attempts in parallel, plus cancellation of the losers, inside a transport that is deliberately small and bound to one deadline. That is a larger change than this fix.
Possible follow-up: interleave address families (IPv6, IPv4, IPv6, IPv4), as anyio and RFC 8305 do. With the same sharing rule, IPv4 would be tried second and connect after 7.5 s instead of 15 s, still without threads. I left it out to keep this PR to one behaviour change, and can send it separately if you want it.
Problem
connect_tcpresolves the host, then tries each address in turn. It remembers each error and moves on, and the first success wins. Each attempt's timeout comes frombounded_timeout(timeout), which is all the time left before the request deadline.So the first address is told it may use all the remaining time. If it never answers (packets dropped rather than refused), it uses all of it. When the loop reaches the second address,
bounded_timeoutfinds no time left and raises before trying it. The loop only helps when an address fails fast, for example a refused connection; the existing testtest_sync_transport_connects_to_each_resolved_address_under_the_original_hostcovers that case.The other clients do not have this problem:
socket.create_connection. It gives every address the full connect timeout and has no overall deadline.AsyncProoflineClientuses anyio'sconnect_tcp, which starts the next address 0.25 s after the previous one.Who it affects: anyone using
ProoflineClienton a dual-stack machine whose IPv6 path drops packets. This happens with some VPNs, container networks and home routers. Every sync call times out, withurn:proofline:sdk:timeout, whileAsyncProoflineClientandcurlwork on the same machine.Change
Each attempt gets (time left) ÷ (addresses not yet tried). Time an attempt does not use carries over to the next one. The budget is taken once, before the name lookup, so the lookup and all attempts share one connect timeout.
Unchanged: address order, error types and their mapping, the single request deadline, the public API and the README. No threads, retries, options or constants are added.
One stricter case:
ProoflineClientalways sets a request deadline before connecting; I checked that REST, MCP and export calls all connect with one set. Only when_sync_http.Transportis used directly, with no deadline, did each attempt previously get the full connect timeout. Four silent addresses at 30 s could take 120 s. They now share the 30 s.Trade-off: an address that is alive but slower to accept than its share is abandoned for the next one. At the default 30 s and four addresses the share is 7.5 s for a TCP handshake, and the last address always gets everything that is left.
Alternatives considered
How to verify
From the repository root:
The new tests replace the clock, the resolver and the connector, so they open no sockets and never sleep. A silent address uses exactly the time it is given, and a refused address fails at once. They also assert that the attempts never add up to more than the timeout, with or without a request deadline.
End-to-end repro with a real loopback server (
pr-repro.py, run from the repository root)Results
New tests. On
main, with only the test commit applied, 7 fail and 5 pass. The first three fail withhttpcore2.ReadTimeout: The request deadline expired.before the second address is attempted. With the fix all 13 pass.Review follow-up. A Proofline review of this branch found that the budget and its end came from two clock readings. When the request deadline was the tighter limit, the end could land after it by the time between the two readings (microseconds in practice).
connect_tcpnow reads the clock once and passes that reading tobounded_timeout, which takes it as a new optionalnowargument.test_time_between_clock_reads_does_not_move_the_request_deadlineadvances the fake clock between readings. It failed before that commit and passes onmain, which read the clock once per attempt, so the drift was never in a release.A second review raised two more points:
test_sync_connect.pypassed even whenconnect_tcpgave the lookup no timeout. The full suite did catch that, throughtest_streaming_and_resolution_keep_deadline_outcomes_explicit[sync]. The fake now records the timeout, and the lookup and single-address tests assert it equals the budget. Withresolve(host, port, None), 6 of them fail.resolve()starts its wait once its worker thread is running, so a lookup that times out can pass the end by the thread's start-up time: a median of 130 µs and at most about 1.2 ms over 2,000 starts here. No address is tried after the end. The comment now says this. I leftresolve()alone because Fix: Bound DNS Lookup Workers and Add Regression Test prooflinedev/sdk-python#53 rewrites those exact lines to wait against an absolute deadline, and that fix belongs there.A third review withdrew the comment finding and raised two more:
resolve(host, port, None). It now asserts that the lookup gets the 30-second budget, and fails without it.httpcore2.ConnectTimeoutrather thanReadTimeout. Not changed.mapped_errors()turns every httpcore2 timeout intohttpx2.ReadTimeout, so callers see the same error either way.A fourth review found the last test that didn't check the lookup timeout:
test_no_timeout_and_no_deadline_stay_unbounded. It now asserts that the lookup gets no timeout, and fails if it is given 5 s.Repository gate. I ran
python scripts/check.pyon Linux inpython:3.13.12-bookworm, matching CI, on a clean clone of this branch. Generation check, contracts, ruff check, ruff format, mypy and pytest (232 passed) all pass, the wheel and sdist build, and both install smokes pass. The sameChecksworkflow also passes on GitHub Actions for this branch's head commit (f35a2f7): run 45253172785.End-to-end repro on Windows 11 with Python 3.12, using a real loopback server:
connect=3AsyncProoflineClient, timeout 3ProoflineClient, timeout 3urn:proofline:sdk:timeoutafter 3.02 s; no request reached the serverProoflineClient, timeout 10urn:proofline:sdk:timeoutafter 10.01 s; no request reached the serverOpen PRs touching
_sync_http.py. I trial-merged this branch locally; nothing was pushed.Transport.__init__and merges cleanly. All tests pass except the Windows-flaky test that Return TIMED_OUT when the wait deadline ends a request, even if the clock lags prooflinedev/sdk-python#85 addresses.resolve()and merges cleanly, and the new tests pass on the merged tree. On its own, its new test file importshttpcore(nowhttpcore2), andtest_mcp_public_types_accept_legacy_and_targeted_callsfails onmain+ Fix: Bound DNS Lookup Workers and Add Regression Test prooflinedev/sdk-python#53 with or without this change.Not tested: a real network with broken IPv6. The silent address is simulated with an unroutable IPv4 address in the repro and with a fake connector in the tests.
🤖 Generated with Claude Code