Repository navigation
fix(gateway): keep idle connections open for an hour so pooled clients aren't reset - #802
Merged
Merged
Conversation
…s aren't reset uvicorn's default keep-alive closed a connection after 5 s idle. An agent that paused about 5 s between tool calls sent the next one on its pooled connection as the gateway closed it, and the call was reset unread (ECONNRESET from Claude Code's fetch). IDLE_CONNECTION_TIMEOUT_S = 3600 outlasts any pause between an agent's calls. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
The gateway ran uvicorn with its default keep-alive timeout, so it closed any connection idle for 5 seconds. Agents pool their connections: Claude Code's
fetch, for one, keeps them longer than 5 s. When an agent paused about 5 seconds between two tool calls, it sent the next call on a pooled connection just as the gateway closed it. That call was reset before the gateway read it:A write was lost the same way: the reset call never ran, so nothing on the server side knew to report it.
The gateway now keeps an idle connection open for an hour (
IDLE_CONNECTION_TIMEOUT_S = 3600). That is longer than any pause between an agent's tool calls, so the gateway no longer closes a connection a client may reuse. Clients still close their own idle connections whenever they like.Evidence
Changes
constants.py:IDLE_CONNECTION_TIMEOUT_S = 3600, with why.gateway.py:run()builds its server from a new_server_config(), which passestimeout_keep_alive=IDLE_CONNECTION_TIMEOUT_S;Not changed: the gateway's own connections to its child MCP servers. They expire idle connections at httpx's 5 s against the children's uvicorn 5 s, so the same race exists in principle. On the in-VM network its window is a fraction of a millisecond, and no trajectory showed it.
How it was tested
New test
tst/unit/env/gateway/idle_connection_socket_test.py. It runs the gateway's own server config on a loopback socket, with the timeout patched to 2 s:A second test pins the real value at 600 s or more and checks it reaches the uvicorn config. With
timeout_keep_aliveremoved, both tests fail: the server closes at uvicorn's 5 s instead.Suite:
pytest tst/unit/env/gateway -n 8passed, 1,172 tests.ruff checkis clean.Live: the measurements above came from two deployments of the same environment. One ran the current default gateway, the other a gateway image built from this branch.
🤖 Generated with Claude Code


Confidence Score: 5/5
The PR appears safe to merge; no new actionable issues were found.Summary
The gateway keeps idle HTTP connections open for one hour to reduce resets when agents reuse pooled connections.
Reviews (2) · Last reviewed commit: "test(gateway): name the idle-connection ..." · Reviewed by Greptile