Skip to content

Commit 8912491

Browse files
cliffhallclaude
andcommitted
fix(fetch): count the connect-time lookup against the connect timeout
The pinned backend's lookup ran before the connect timer started, so a stalled resolution could outlast the configured timeout. The lookup is now bounded by it, reported as a ConnectTimeout, and the attempts get what it left. Also give a timing test several ticks of Windows' coarse clock. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent c2a9b01 commit 8912491

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎src/fetch/src/mcp_server_fetch/pinning.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,21 @@ async def connect_tcp(
8383
local_address: str | None = None,
8484
socket_options: Iterable[Any] | None = None,
8585
) -> httpcore.AsyncNetworkStream:
86+
# The connect timeout covers the lookup too, as it does for a
87+
# name-based connect: the attempts below get what the lookup left.
88+
loop = asyncio.get_running_loop()
89+
started = loop.time()
8690
try:
87-
resolved = await self._resolve(host)
91+
resolved = await asyncio.wait_for(self._resolve(host), timeout)
8892
except socket.gaierror as e:
8993
# What the inner backend reports for a name it cannot resolve.
9094
raise httpcore.ConnectError(str(e)) from e
95+
except asyncio.TimeoutError as e:
96+
raise httpcore.ConnectTimeout(f"Timed out resolving {host}") from e
9197
# getaddrinfo lists an address once per socket type.
9298
addresses = list(dict.fromkeys(resolved))
9399
self._vet(host, addresses)
94100

95-
loop = asyncio.get_running_loop()
96-
started = loop.time()
97101
waiting = list(addresses)
98102
attempts: set[asyncio.Task[httpcore.AsyncNetworkStream]] = set()
99103
failures: list[Exception] = []

‎src/fetch/tests/test_pinning.py‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,30 @@ async def resolve(host: str) -> Sequence[str]:
747747
await backend.connect_tcp("name.test", 80)
748748

749749

750+
async def test_backend_bounds_the_lookup_by_the_connect_timeout() -> None:
751+
async def resolve(host: str) -> Sequence[str]:
752+
await asyncio.Event().wait()
753+
return []
754+
755+
backend = PinnedBackend(resolve, lambda host, found: None, FakeInner())
756+
with pytest.raises(httpcore.ConnectTimeout, match="Timed out resolving name.test"):
757+
await backend.connect_tcp("name.test", 80, timeout=0.05)
758+
759+
760+
async def test_backend_takes_the_lookup_out_of_the_time_left_to_connect() -> None:
761+
inner = FakeInner()
762+
763+
async def resolve(host: str) -> Sequence[str]:
764+
await asyncio.sleep(0.1)
765+
return ["192.0.2.1"]
766+
767+
backend = PinnedBackend(resolve, lambda host, found: None, inner)
768+
await backend.connect_tcp("name.test", 80, timeout=30)
769+
770+
((_, _, left),) = inner.calls
771+
assert left is not None and 0 < left < 30
772+
773+
750774
async def test_backend_reports_a_host_with_no_addresses_as_a_connect_error() -> None:
751775
backend, _ = backend_for([], FakeInner())
752776
with pytest.raises(httpcore.ConnectError, match="name.test has no addresses"):
@@ -775,7 +799,9 @@ async def test_backend_raises_the_first_failure_when_every_address_fails() -> No
775799
async def test_backend_tries_the_next_address_beside_one_that_does_not_answer(
776800
monkeypatch: pytest.MonkeyPatch,
777801
) -> None:
778-
monkeypatch.setattr(pinning, "CONNECTION_ATTEMPT_DELAY", 0.01)
802+
# Several ticks of Windows' ~16 ms clock, so the second attempt is
803+
# measurably later than the first.
804+
monkeypatch.setattr(pinning, "CONNECTION_ATTEMPT_DELAY", 0.1)
779805
inner = FakeInner(hang=["192.0.2.1"])
780806
backend, _ = backend_for(["192.0.2.1", "192.0.2.2"], inner)
781807

0 commit comments

Comments
 (0)