Skip to content

[v1.x] fix(client): drain piped stderr so unread pipe cannot deadlock - #2794

Draft
BerkantACUN wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
BerkantACUN:v1.x-drain-stderr
Draft

[v1.x] fix(client): drain piped stderr so unread pipe cannot deadlock#2794
BerkantACUN wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
BerkantACUN:v1.x-drain-stderr

Conversation

@BerkantACUN

Copy link
Copy Markdown

v1.x backport of #2788 by @tiagovilasboas. Opened as a draft until #2788 lands; I will follow whatever changes review makes there.

StdioClientTransport with stderr: 'pipe' (or 'overlapped') pipes child stderr into a PassThrough that stays paused until a reader attaches. A server that logs a normal amount to stderr fills that buffer (16 KiB highWaterMark) and then the OS pipe, blocks on write(2), stops reading stdin, and the session hangs — no onerror, no rejection, no timeout. await client.listTools() never returns. #2776 was reported on this line (@modelcontextprotocol/sdk@1.30.0).

Motivation and Context

Same as #2788: after child.stderr.pipe(passThrough), call passThrough.resume() so the stream is in flowing mode and unread chunks drain. Listeners attached before start() / connect() still receive every chunk. A late listener sees only post-attach data — lost log lines beat a hung session. Both docstrings say so, including that paused-mode read() is not supported after start().

Differences from #2788

  • Paths: src/client/stdio.ts and test/client/stdio.test.ts (single-package layout).
  • No changeset (not used on 1.x) and no version bump.
  • Test imports use the 1.x .js-suffixed relative style; the three test cases are otherwise identical.

How Has This Been Tested?

The three regression tests from #2788 (no reader → listTools() completes; listener before start() receives startup and post-request chunks; late listener → session completes, sees only post-attach chunks). The first and third hang on v1.x before the change and fail on the 8 s test timeout. npm run check passes; test/client/stdio.test.ts 8/8.

Also ran the issue's repro (a server writing 200 × 45 KB lines to stderr on the first request, no reader) against this branch on Windows/Node 22: hangs on 1.30.0, returns in ~220 ms here.

Breaking Changes

None to the API. Behavioural: a listener attached to transport.stderr after start() no longer sees data written before it attached.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Disclosure: prepared with Claude Code; I reviewed the diff against #2788 and ran the tests and the repro myself.

v1.x backport of modelcontextprotocol#2788.

`StdioClientTransport` with `stderr: 'pipe'` (or `'overlapped'`) pipes
child stderr into a PassThrough that stays paused until a reader
attaches. A server that logs a normal amount fills that buffer and the
OS pipe, blocks on write(2), stops reading stdin, and the session hangs
with no error, no rejection and no timeout. After piping, the
PassThrough is put in flowing mode so unread chunks drain; listeners
attached before start() still receive every chunk, a late listener sees
only post-attach data.

Fixes modelcontextprotocol#2776 on v1.x.
@changeset-bot

changeset-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: f05ff04

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.

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 Sep 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: f05ff04

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