Repository navigation
Disable Bun idle timeout on AI proxy, MCP, and connector long-poll routes - #999
Merged
Merged
Conversation
…utes Bun.serve() defaults to a 10-second idleTimeout (swept every 4 seconds). When an upstream model stream through the AI credential proxy (/api/v1/ai/*) pauses for more than ~10 seconds between SSE chunks while reasoning, Bun cancels the response stream and closes the socket mid-stream, surfacing as an abrupt socket close on the client and status 200 cancelled in ai_usage. The same 10-second default also undercuts /connector/mcp wait_for_work long-polls (20s), /mcp streams, and standby handover holds (default 20s, up to 120s). Adds apps/server/src/http/timeout.ts and wires listen() in apps/server/src/main.ts to: - call server.timeout(req, 0) on /api/v1/ai/*, /mcp, and /connector/mcp before request handling (matching apps/connector/src/ai-relay.ts); - set Bun.serve() idleTimeout to max(60, ceil(holdMs / 1000) + 10) seconds (clamped to Bun's 255s bound) for ordinary routes. Assistant-model: Gemini 4 Neon Co-authored-by: Alex Lavaee <[email protected]>
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
Bun.serve()inapps/server/src/main.tsleftidleTimeoutat Bun's 10-second default (swept on a 4-second timer). When a reasoning model proxied through/api/v1/ai/*pauses for more than ~10 seconds between SSE chunks, Bun cancels theReadableStream(recordingstatus: 200, error: "cancelled"inai_usage) and closes the socket mid-stream (UND_ERR_SOCKET/stream-ended-earlyon the client), even after all client retries.The same 10-second default also undercuts
/connector/mcpwait_for_worklong-polls (20 s),/mcpstreams, and standbyrequests.hold()windows during handover (default 20 s, configurable up to 120 s). By comparison,apps/connector/src/ai-relay.tsalready setsidleTimeout: 0.Changes
apps/server/src/http/timeout.ts:applyRequestTimeout(req, server)callsserver.timeout(req, 0)on/api/v1/ai/*,/mcp, and/connector/mcp.serverIdleTimeoutSeconds(holdMs)computesclamp(max(60, ceil(holdMs / 1000) + 10), 60, 255)for ordinary routes.listen()inapps/server/src/main.tsbeforerequests.handle(...)so standby-held requests are covered too.Bun.serveregression tests inapps/server/src/http/timeout.test.ts.Verification
bun run fixbun run cibun run typesbun test apps/server/src/http/timeout.test.ts apps/server/src/ai/proxy.test.ts apps/server/src/handover/abandoned.test.ts apps/server/src/handover/requests.test.ts(105 pass, 0 fail)