modelcontextprotocol / modelcontextprotocol/typescript-sdk
Bug: `_handleSseStream` retains reader lock on stream close, leaking ~50MB per reconnection
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 13.4k
- Forks
- 2.2k
- Avg merge
- 3d 15h
- Merged PRs (30d)
- 4
Description
Summary
StreamableHTTPClientTransport._handleSseStream (dist/esm/client/streamableHttp.js:158-255, present in 1.29.0) acquires a ReadableStream reader via .getReader() but never calls reader.releaseLock() on either path:
- Success path (
while (true) { ... if (done) break }): the loop breaks without releasing. - Error path (
catch (error)): the catch branch invokesonerrorand possibly_scheduleReconnection, but never releases.
When the stream closes (server-initiated disconnect, abort, or error), the reader keeps its lock on the upstream ReadableStream. The TextDecoderStream and EventSourceParserStream buffers it pipes through remain reachable from the locked reader and stay GC-pinned. Each subsequent reconnection allocates a fresh pipeline, so memory grows ~50 MB per reconnect cycle in long-running clients.
Repro
A client that reconnects to an HTTP/SSE MCP server (streamableHttp transport) over a session that experiences any disconnect:
- Connect via
StreamableHTTPClientTransport. - Force a server-side disconnect (e.g., 504, idle timeout, restart).
- Heap-snapshot before/after the reconnect — observe retained
Uint8Arraydecoder buffer + parser state withReadableStreamDefaultReaderas the dominator.
Proposed fix
Wrap the while (true) { reader.read() } loop in try { ... } finally { reader.releaseLock() }:
const reader = stream.pipeThrough(...).pipeThrough(...).getReader()
try {
while (true) {
const { value, done } = await reader.read()
if (done) break
// ... process event
}
} finally {
reader.releaseLock()
}
The fix needs to live inside the existing outer try/catch so reconnection scheduling on catch still fires after the lock is released.
Notes
- Upstream Claude Code (the
claude-codeCLI, internal source) has this fix as of v2.1.117. The published@modelcontextprotocol/sdkpackage does not. - I'm carrying a subclass override (
LeakFreeStreamableHTTPClientTransport) that copies_handleSseStreambyte-for-byte from1.29.0and adds thetry/finally. Happy to open a PR with the upstream fix if it'd be useful.
🤖 Generated with Claude Code
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in dist/esm/client/streamableHttp.js:158-255 and inspect StreamableHTTPClientTransport._handleSseStream, especially the reader loop and its surrounding try/catch. Reproduce a disconnect and reconnect if possible, then verify that the reader lock is released on both success and error paths while reconnection scheduling still occurs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, networking
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 74/100