Skip to content

fix(core-internal): release the ReadBuffer backing allocation after the last message - #2811

Draft
nikolas-sapa wants to merge 1 commit into
modelcontextprotocol:mainfrom
nikolas-sapa:fix/readbuffer-release-consumed-buffer
Draft

nikolas-sapa wants to merge 1 commit into
modelcontextprotocol:mainfrom
nikolas-sapa:fix/readbuffer-release-consumed-buffer

Conversation

@nikolas-sapa

Copy link
Copy Markdown

Fixes #2536.

ReadBuffer.readMessage() advanced the buffer with this._buffer.subarray(index + 1). When the newline was the final byte of the chunk, that returns a zero-length view that still references the backing allocation of the consumed input. The bytes stay reachable until the next append() replaces the view, and that append() is forced through Buffer.concat even though nothing is actually buffered.

Dropping the buffer when the remainder is empty releases the allocation immediately. append() and clear() already handle _buffer === undefined, so no other call site changes.

Opened as a draft to respect the one-open-PR limit for contributors without write access — I'll mark it ready once #2807 and #2809 clear.

…ast message

readMessage() advanced the buffer with subarray(index + 1). When the
newline was the final byte, that returned an empty view which still
referenced the backing allocation of the consumed input — keeping it
alive until the next append, and forcing that append through
Buffer.concat even though no bytes were buffered.

Drop the buffer entirely when the remainder is empty: append() and
clear() already handle _buffer === undefined.

Fixes modelcontextprotocol#2536
@changeset-bot

changeset-bot Bot commented Sep 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e16a162

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

This PR includes changesets to release 6 packages
Name Type
@modelcontextprotocol/client Patch
@modelcontextprotocol/server Patch
@modelcontextprotocol/codemod Patch
@modelcontextprotocol/core Patch
@modelcontextprotocol/server-legacy Patch
@modelcontextprotocol/core-internal Patch

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

pkg-pr-new Bot commented Sep 13, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2811

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@2811

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@2811

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2811

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@2811

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2811

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2811

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2811

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2811

commit: e16a162

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.

stdio.ts/ReadBuffer retains consumed input and copies the next stdio chunk

1 participant