The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(agent): cover inline hard timeout and in-flight pool-request abort
HexLab98c5788b5ea35e
diff --git a/tests/cron/test_cron_direct_api_call_watchdog.py b/tests/cron/test_cron_direct_api_call_watchdog.pyindex 38b3b5bf8f1..bee17ecad22 100644--- a/tests/cron/test_cron_direct_api_call_watchdog.py+++ b/tests/cron/test_cron_direct_api_call_watchdog.py@@ -11,7 +11,10 @@ read timeout, and the job-level inactivity monitor was observed not to fire.These tests pin the watchdog contract: it aborts the in-flight sockets throughthe already-registered abort hook, surfaces a retryable ``TimeoutError`` (never``InterruptedError``), feeds the cross-turn stale circuit breaker, and stays-out of the way of a healthy call.+out of the way of a healthy call. They also pin #85252: the keepalive httpx+client uses ``read=None``, so a stranger-thread abort that finds no sockets+must not leave the call unbounded — ``direct_api_call`` injects a per-call+read timeout matching the stale budget as a hard backstop."""import sys@@ -382,3 +385,84 @@ def test_resolver_exception_propagates_instead_of_disarming_the_watchdog():with pytest.raises(RuntimeError, match="resolver regression"):direct_api_call(agent, {"model": "m", "messages": []})+++# ---------------------------------------------------------------------------+# #85252: hard socket bound when stranger-thread abort cannot kill the recv.+# ---------------------------------------------------------------------------+++def test_inline_hard_timeout_matches_stale_budget():+ """Keepalive httpx uses read=None. The injected timeout's read budget+ must equal the stale watchdog so a no-op abort cannot hang for hours."""+ from agent.chat_completion_helpers import _inline_nonstream_hard_timeout++ timeout = _inline_nonstream_hard_timeout(600.0)+ assert timeout is not None+ assert timeout.read == 600.0+ assert timeout.connect == 60.0+ assert timeout.write == 60.0+ assert timeout.pool == 60.0+++def test_inline_hard_timeout_disarmed_when_watchdog_is_disarmed():+ from agent.chat_completion_helpers import _inline_nonstream_hard_timeout++ assert _inline_nonstream_hard_timeout(float("inf")) is None+ assert _inline_nonstream_hard_timeout(0) is None+ assert _inline_nonstream_hard_timeout(-1) is None+++def test_inline_call_passes_hard_read_timeout_to_the_sdk():+ """The bound has to actually reach chat.completions.create — a helper+ that is never wired in would leave cron on read=None (#85252)."""+ agent = _make_agent(stale_timeout=0.5)+ fake_client = MagicMock()+ captured = {}++ def _create(**kwargs):+ captured["timeout"] = kwargs.get("timeout")+ return SimpleNamespace(id="ok")++ fake_client.chat.completions.create.side_effect = _create+ agent._create_request_openai_client.return_value = fake_client++ assert direct_api_call(agent, {"model": "m", "messages": []}).id == "ok"+ timeout = captured["timeout"]+ assert timeout is not None+ assert timeout.read == 0.5+++def test_inline_call_does_not_override_explicit_timeout():+ """A transport/provider that already set timeout= must keep it."""+ agent = _make_agent(stale_timeout=30.0)+ fake_client = MagicMock()+ captured = {}++ def _create(**kwargs):+ captured["timeout"] = kwargs.get("timeout")+ return SimpleNamespace(id="ok")++ fake_client.chat.completions.create.side_effect = _create+ agent._create_request_openai_client.return_value = fake_client++ assert direct_api_call(+ agent, {"model": "m", "messages": [], "timeout": 12.0}+ ).id == "ok"+ assert captured["timeout"] == 12.0+++def test_infinite_budget_does_not_inject_a_hard_timeout():+ agent = _make_agent(stale_timeout=float("inf"))+ fake_client = MagicMock()+ captured = {}++ def _create(**kwargs):+ captured["timeout"] = kwargs.get("timeout")+ return SimpleNamespace(id="ok")++ fake_client.chat.completions.create.side_effect = _create+ agent._create_request_openai_client.return_value = fake_client++ assert direct_api_call(agent, {"model": "m", "messages": []}).id == "ok"+ assert "timeout" not in captured or captured["timeout"] is Nonediff --git a/tests/run_agent/test_create_openai_client_reuse.py b/tests/run_agent/test_create_openai_client_reuse.pyindex 600b6b4f751..d42ceb2176d 100644--- a/tests/run_agent/test_create_openai_client_reuse.py+++ b/tests/run_agent/test_create_openai_client_reuse.py@@ -267,3 +267,81 @@ def test_force_close_tcp_sockets_finds_sockets_on_httpx_mounts():assert sock.close_calls == 0+def test_force_close_tcp_sockets_finds_in_flight_pool_request_sockets():+ """httpcore keeps the live connection on PoolRequest.connection.++ #85252: walking only ``_connections`` (and treating an empty list as+ falsy) returned tcp_force_closed=0 while the hung recv was still on+ the in-flight request. Must shut that socket down without close().+ """+ from agent.agent_runtime_helpers import force_close_tcp_sockets++ class FakeSocket:+ def __init__(self):+ self.shutdown_calls = 0+ self.close_calls = 0+ self.timeouts = []++ def settimeout(self, value):+ self.timeouts.append(value)++ def shutdown(self, _how):+ self.shutdown_calls += 1++ def close(self):+ self.close_calls += 1++ sock = FakeSocket()+ stream = SimpleNamespace(_sock=sock)+ http11 = SimpleNamespace(_network_stream=stream)+ in_flight = SimpleNamespace(_connection=http11)+ pool_req = SimpleNamespace(connection=in_flight)+ # Empty _connections is the failing layout: the live socket lives+ # only on the in-flight PoolRequest.+ pool = SimpleNamespace(_connections=[], _requests=[pool_req])+ transport = SimpleNamespace(_pool=pool)+ http_client = SimpleNamespace(_transport=transport)+ openai_client = SimpleNamespace(_client=http_client)++ assert force_close_tcp_sockets(openai_client) == 1+ assert sock.shutdown_calls == 1+ assert sock.close_calls == 0+ assert sock.timeouts == [0]+++def test_force_close_tcp_sockets_clears_timeout_before_shutdown():+ """Hung SSL recv with timeout=None can ignore SHUT_RDWR until the+ socket timeout is cleared (#85252). Still no close() (#29507)."""+ from agent.agent_runtime_helpers import force_close_tcp_sockets++ class FakeSocket:+ def __init__(self):+ self.order = []++ def settimeout(self, value):+ self.order.append(("settimeout", value))++ def shutdown(self, _how):+ self.order.append(("shutdown", _how))++ def close(self):+ self.order.append(("close", None))++ sock = FakeSocket()+ stream = SimpleNamespace(_sock=sock)+ http11 = SimpleNamespace(_network_stream=stream)+ pool_entry = SimpleNamespace(_connection=http11)+ pool = SimpleNamespace(_connections=[pool_entry])+ transport = SimpleNamespace(_pool=pool)+ http_client = SimpleNamespace(_transport=transport)+ openai_client = SimpleNamespace(_client=http_client)++ import socket as _socket++ assert force_close_tcp_sockets(openai_client) == 1+ assert sock.order == [+ ("settimeout", 0),+ ("shutdown", _socket.SHUT_RDWR),+ ]++