Skip to content

fix(client/sse): release reader lock on disconnect to prevent ~50MB leak#1961

Open
MukundaKatta wants to merge 2 commits intomodelcontextprotocol:v1.xfrom
MukundaKatta:fix/sse-reader-lock-leak
Open

fix(client/sse): release reader lock on disconnect to prevent ~50MB leak#1961
MukundaKatta wants to merge 2 commits intomodelcontextprotocol:v1.xfrom
MukundaKatta:fix/sse-reader-lock-leak

Conversation

@MukundaKatta
Copy link
Copy Markdown

Closes #1959.

_handleSseStream acquired a reader via getReader() but never called releaseLock() on disconnect, so the underlying stream and its buffered chunks (~50MB) stayed alive across reconnects. Wrapped the read loop in try/finally so the lock is always released, and added a regression test in test/client/streamableHttp.test.ts that asserts releaseLock fires on both graceful close and error paths.

Note: I prepared this change via the GitHub API (no shell access to a local clone in this environment), so I could not run pnpm test locally. Happy to iterate if CI flags anything.

@MukundaKatta MukundaKatta requested a review from a team as a code owner April 25, 2026 18:20
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 25, 2026

🦋 Changeset detected

Latest commit: 06b2f9e

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 25, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@modelcontextprotocol/sdk@1961

commit: 06b2f9e

_handleSseStream acquired a ReadableStream reader via getReader() but
never called releaseLock() on either the success or error path. The
upstream decoder + parser pipeline therefore stayed pinned across
reconnects, leaking ~50MB per reconnect cycle for long-running clients.

Wrap the read loop in try/finally so the lock is always released, then
the outer catch can still schedule reconnection. Add a regression test
in test/client/streamableHttp.test.ts that asserts releaseLock fires on
both graceful close and error paths.

Fixes modelcontextprotocol#1959.
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