Skip to content

fix(stdio): release ReadBuffer backing after final byte#2540

Open
piyushbag wants to merge 1 commit into
modelcontextprotocol:mainfrom
piyushbag:fix/readbuffer-clear-consumed-buffer
Open

fix(stdio): release ReadBuffer backing after final byte#2540
piyushbag wants to merge 1 commit into
modelcontextprotocol:mainfrom
piyushbag:fix/readbuffer-clear-consumed-buffer

Conversation

@piyushbag

Copy link
Copy Markdown

Problem

ReadBuffer.readMessage() leaves an empty Buffer subarray after consuming a complete line. The subarray still references the accumulated backing allocation, so idle stdio transports retain memory longer than necessary and the next append always copies via Buffer.concat() even when no bytes remain buffered.

Approach

When the remainder after a newline is empty, set _buffer to undefined instead of an empty subarray. Non-empty remainders are unchanged.

Test plan

  • Added regression test asserting Buffer.concat is not called on the next append after a fully consumed message
  • pnpm --filter @modelcontextprotocol/core-internal test (1294 passed)

Fixes #2536

@piyushbag
piyushbag requested a review from a team as a code owner July 23, 2026 15:57
@changeset-bot

changeset-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 8a36bde

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@pkg-pr-new

pkg-pr-new Bot commented Jul 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/codemod

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

@modelcontextprotocol/core

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

@modelcontextprotocol/server

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

@modelcontextprotocol/server-legacy

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

@modelcontextprotocol/express

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

@modelcontextprotocol/fastify

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 8a36bde

Clear _buffer when readMessage consumes through the trailing newline so
idle stdio transports do not retain the previous allocation and the next
append can assign directly instead of Buffer.concat.
@piyushbag
piyushbag force-pushed the fix/readbuffer-clear-consumed-buffer branch from 8f6051f to 8a36bde Compare July 23, 2026 16:18
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