Skip to content

Disable Bun idle timeout on AI proxy, MCP, and connector long-poll routes - #999

Merged
lavaman131 merged 1 commit into
mainfrom
server-idle-timeout
Nov 23, 2026
Merged

lavaman131 merged 1 commit into
mainfrom
server-idle-timeout

Conversation

@lavaman131

Copy link
Copy Markdown
Collaborator

Problem

Bun.serve() in apps/server/src/main.ts left idleTimeout at 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 the ReadableStream (recording status: 200, error: "cancelled" in ai_usage) and closes the socket mid-stream (UND_ERR_SOCKET / stream-ended-early on the client), even after all client retries.

The same 10-second default also undercuts /connector/mcp wait_for_work long-polls (20 s), /mcp streams, and standby requests.hold() windows during handover (default 20 s, configurable up to 120 s). By comparison, apps/connector/src/ai-relay.ts already sets idleTimeout: 0.

Changes

  • Add apps/server/src/http/timeout.ts:
    • applyRequestTimeout(req, server) calls server.timeout(req, 0) on /api/v1/ai/*, /mcp, and /connector/mcp.
    • serverIdleTimeoutSeconds(holdMs) computes clamp(max(60, ceil(holdMs / 1000) + 10), 60, 255) for ordinary routes.
  • Wire both into listen() in apps/server/src/main.ts before requests.handle(...) so standby-held requests are covered too.
  • Add unit + live Bun.serve regression tests in apps/server/src/http/timeout.test.ts.

Verification

  • bun run fix
  • bun run ci
  • bun run types
  • bun 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)

…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]>
@lavaman131
lavaman131 merged commit 6ba1e82 into main Nov 23, 2026
26 checks passed
@lavaman131
lavaman131 deleted the server-idle-timeout branch November 23, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant