Release the SSE request when a remote MCP connection closes - #1716
Open
xav-ie wants to merge 1 commit into
Open
Conversation
xav-ie
force-pushed
the
fix/mcp-abort-releases-response-body
branch
from
August 21, 2026 19:19
271d5ea to
15878ae
Compare
The fetch adapter wired the caller's AbortSignal only to the response promise, never to the body, so streamable-http's long-lived GET stayed in flight after close(): one abandoned request per dial, each holding one of Bun's 256 concurrent-request slots until discovery times out everywhere.
xav-ie
force-pushed
the
fix/mcp-abort-releases-response-body
branch
from
August 21, 2026 19:30
15878ae to
9f76528
Compare
xav-ie
marked this pull request as ready for review
August 21, 2026 19:37
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.
The bug
fetchFromHttpClientLayerwires the caller'sAbortSignalinto aPromise.raceon the response promise only. The body it hands the SDK isStream.toReadableStream(response.stream)— a detached fiber that outlives the effect that promise settles — so nothing connects the signal to the body.sequenceDiagram participant SDK as MCP SDK participant A as fetch adapter participant S as MCP server SDK->>A: fetch(GET, signal) A->>S: GET /mcp S-->>A: SSE stream (never ends) A-->>SDK: Response(detached body) SDK->>A: close() aborts signal Note over A: signal reaches the promise,<br/>not the body A--xS: GET still in flightFor a short POST that is invisible. For streamable-http's long-lived SSE
GETit meansclose()reaches nothing: one abandoned request per health check, discovery, or invocation.Why it bites
Under Bun each abandoned request also holds one of the 256
BUN_CONFIG_MAX_HTTP_REQUESTSslots. Once a long-running process exhausts the pool, every outboundfetchqueues forever and every connection reportsMCP discovery timed out after 15000ms— whilecurlagainst the same endpoint answers in ~30 ms. Connections with a cached catalog logplugin returned an incomplete tool catalogand keep serving stale data, so the first visible symptom is usually one unrelated-looking connection going degraded.The fix
Interrupt the response stream at the source when the signal aborts:
Cancelling the
ReadableStreaminstead does not work: the SDK holds a locked reader on exactly the SSE channel that leaks, sobody.cancel()rejects withTypeError: Invalid state: ReadableStream is locked. The POST bodies cancel fine; the one that matters does not.Separate from #1654 — that fixes the interrupted-connect path, this leaks on the successful path.
Verification
New
connection-socket-release.test.ts, asserting on a newinFlightRequests()on the test server.sessionCount()cannot see this (session gone, request not), and counting sockets cannot either, since a keep-alive pool holds idle sockets open regardless.Confirmed to fail with the
connection.tschange stashed.format:check,lint,typecheck(44/44) clean.test: 37/38 packages —@executor-js/mcp-apps-shellfails only onmcp-app.browser.test.ts(no Chromium at/usr/bin/google-chromeon this machine), unrelated.