fix: add configurable timeout_keep_alive to prevent ReadError on keep-alive connection reuse#468
Open
nihongye wants to merge 2 commits intoagentscope-ai:mainfrom
Open
Conversation
…-alive connection reuse The SandboxManager client uses a long-lived httpx.AsyncClient with connection pooling to access the sandbox manager server. uvicorn's default timeout_keep_alive is 5 seconds, while httpx's default keepalive_expiry is also 5 seconds. When the client reuses a connection that the server has already started closing (FIN sent), the client receives a RST packet instead of a response, causing httpx.ReadError. Changes: - Add TIMEOUT_KEEP_ALIVE setting (default 120s) to sandbox manager Settings - Pass timeout_keep_alive to uvicorn.run() in sandbox manager server - Add timeout_keep_alive parameter to AgentApp.run() (default 120s) By setting server timeout_keep_alive > client keepalive_expiry, the server keeps idle connections alive longer than clients expect, eliminating the race condition.
86f9a02 to
a1d859b
Compare
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.
Problem
The SandboxManager client uses a long-lived
httpx.AsyncClientwith connection pooling to access the sandbox manager server.uvicorn's default
timeout_keep_aliveis 5 seconds, while httpx's defaultkeepalive_expiryis also 5 seconds. This creates a race condition:httpx.ReadErrorinstead of a responseTiming pattern observed:
T+0.000s: Request/Response completes, connection idle T+5.000s: Server sends FIN (timeout_keep_alive=5s default) T+5.001s: Client sends new request on same connection T+5.002s: Server sends RST → httpx.ReadError
Solution
Set
timeout_keep_aliveto 120 seconds (configurable) so the server keeps idle connections alive longer than clients expect. This ensures the server never closes a connection that the client might still try to reuse.Key principle: Server
timeout_keep_alive> Clientkeepalive_expiryChanges
TIMEOUT_KEEP_ALIVE: int = 120to Settings (configurable via env var)timeout_keep_alive=settings.TIMEOUT_KEEP_ALIVEtouvicorn.run()timeout_keep_alive=120parameter toAgentApp.run()for clients using connection pools