diff --git a/src/agent_env/env/gateway/constants.py b/src/agent_env/env/gateway/constants.py index c66934f07..26e3133d9 100644 --- a/src/agent_env/env/gateway/constants.py +++ b/src/agent_env/env/gateway/constants.py @@ -118,6 +118,10 @@ def data_plane_load_timeout_s(size_bytes: int | None) -> int: # here fails the whole discovery, to be retried on the next connect. CHILD_DISCOVERY_TIMEOUT_S = 30.0 +# How long the gateway keeps an idle connection open (uvicorn's default is 5 s). A request a client sends on a pooled +# connection as the server closes it is reset unread, so this outlasts any pause between an agent's tool calls. +IDLE_CONNECTION_TIMEOUT_S = 3600 + # The GET /state field naming each tool name more than one source serves, with its sources: backing servers by name, # `gateway` for the gateway's own tools. Empty unless discovery found such a clash, which leaves the env serving none # of its servers' tools; a deploy reads it to fail with the clash instead. diff --git a/src/agent_env/env/gateway/gateway.py b/src/agent_env/env/gateway/gateway.py index a503d8c84..32021167f 100644 --- a/src/agent_env/env/gateway/gateway.py +++ b/src/agent_env/env/gateway/gateway.py @@ -58,6 +58,7 @@ GATEWAY_COMPOSE_HOST, GATEWAY_EXTENSIONS, GATEWAY_TRAJECTORY_FILE, + IDLE_CONNECTION_TIMEOUT_S, MCP_TRANSPORT, METHOD_ADD, METHOD_GET, @@ -565,15 +566,7 @@ async def env_data_plane(request: Request) -> Response: def run(self) -> None: logger.info(f"Starting Gateway on {self.host}:{self.port} (mode={self.gateway_mode.value})") - config = uvicorn.Config( - self._asgi_app(), - host=self._mcp.settings.host, - port=self._mcp.settings.port, - log_level=self._mcp.settings.log_level.lower(), - ws_ping_interval=PING_INTERVAL_S, - ws_ping_timeout=PING_INTERVAL_S, - ) - server = uvicorn.Server(config) + server = uvicorn.Server(self._server_config()) if self._resolve_i6pn_host(): # On Modal's i6pn, serve both families: IPv4 for the orchestrator's HTTPS tunnel and # IPv6 for backing servers reaching the gateway over the i6pn mesh. A lone `::` bind @@ -583,6 +576,17 @@ def run(self) -> None: else: anyio.run(server.serve) + def _server_config(self) -> uvicorn.Config: + return uvicorn.Config( + self._asgi_app(), + host=self._mcp.settings.host, + port=self._mcp.settings.port, + log_level=self._mcp.settings.log_level.lower(), + ws_ping_interval=PING_INTERVAL_S, + ws_ping_timeout=PING_INTERVAL_S, + timeout_keep_alive=IDLE_CONNECTION_TIMEOUT_S, + ) + # The three scopes a Caller is bound in, innermost first. Request: the served app binds the role the # AgentEnv-Role header named. Session: mcp's Server.run enters the session lifespan once per inbound MCP # session, in the session's server task before any handler task is spawned, so the key bound there is inherited diff --git a/tst/unit/env/gateway/idle_connection_socket_test.py b/tst/unit/env/gateway/idle_connection_socket_test.py new file mode 100644 index 000000000..c214ec359 --- /dev/null +++ b/tst/unit/env/gateway/idle_connection_socket_test.py @@ -0,0 +1,76 @@ +"""The gateway's server keeps an idle connection open for IDLE_CONNECTION_TIMEOUT_S, over a real loopback socket. + +A client that pools connections (Claude Code's fetch does) sends a request on one the server is closing as its idle +timer fires; the request is reset unread. uvicorn's default timer, 5 s, sat inside an agent's ordinary pause between +tool calls.""" +from __future__ import annotations + +import asyncio +import socket +import time + +import pytest +import uvicorn +from pytest_socket import enable_socket + +from agent_env.env.gateway import gateway as gateway_module +from agent_env.env.gateway.constants import IDLE_CONNECTION_TIMEOUT_S +from agent_env.env.gateway.gateway import Gateway + + +@pytest.fixture(autouse=True) +def _disable_network(): + """Override the suite-wide socket block (tst/unit/conftest.py) for this module: loopback is the point here.""" + enable_socket() + yield + + +def test_an_idle_connection_outlasts_any_pause_between_tool_calls(): + assert IDLE_CONNECTION_TIMEOUT_S >= MIN_IDLE_TIMEOUT_S + gw = Gateway(host="127.0.0.1", port=0, server_name="t", internal_mcp_servers=[]) + assert gw._server_config().timeout_keep_alive == IDLE_CONNECTION_TIMEOUT_S + + +@pytest.mark.asyncio +async def test_the_server_closes_an_idle_connection_only_when_the_timeout_runs_out(monkeypatch): + monkeypatch.setattr(gateway_module, "IDLE_CONNECTION_TIMEOUT_S", TEST_IDLE_TIMEOUT_S) + gw = Gateway(host="127.0.0.1", port=0, server_name="t", internal_mcp_servers=[]) + config = gw._server_config() + config.log_level = "critical" + config.access_log = False + config.load() + server = uvicorn.Server(config) + server.lifespan = config.lifespan_class(config) + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.bind(("127.0.0.1", 0)) + await server.startup(sockets=[sock]) + reader, writer = await asyncio.open_connection("127.0.0.1", sock.getsockname()[1]) + try: + for idle_s in REUSE_AFTER_S: # reused within the timeout: still served + await asyncio.sleep(idle_s) + writer.write(REQUEST) + await writer.drain() + head = await reader.readuntil(b"\r\n\r\n") + assert head.startswith(b"HTTP/1.1 404"), head + length = next(int(line.split(b":")[1]) for line in head.split(b"\r\n") + if line.lower().startswith(b"content-length")) + await reader.readexactly(length) + idle_from = time.monotonic() + closed = await asyncio.wait_for(reader.read(1), timeout=TEST_IDLE_TIMEOUT_S + 2 * SCHEDULING_SLACK_S) + assert closed == b"" # then closed by the server + assert abs(time.monotonic() - idle_from - TEST_IDLE_TIMEOUT_S) < SCHEDULING_SLACK_S + finally: + writer.close() + await server.shutdown() + + +# An agent pauses seconds between tool calls, minutes at most; the real timeout must sit well past that. +MIN_IDLE_TIMEOUT_S = 600 +# The timeout patched in for the socket test: short enough for a unit test, well under uvicorn's default 5 s. +TEST_IDLE_TIMEOUT_S = 2 +# Idle times the connection is reused after, both inside TEST_IDLE_TIMEOUT_S. +REUSE_AFTER_S = (0, 1) +# How far the measured close may land from TEST_IDLE_TIMEOUT_S under event-loop scheduling. +SCHEDULING_SLACK_S = 1.0 +REQUEST = b"GET /nothing-here HTTP/1.1\r\nHost: gateway\r\n\r\n"