Skip to content

Fix StdioTransport trap when stdio is a socket - #334

Open
NayanBhut wants to merge 1 commit into
modelcontextprotocol:mainfrom
NayanBhut:fix/stdio-transport-socket-stdev-trap
Open

NayanBhut wants to merge 1 commit into
modelcontextprotocol:mainfrom
NayanBhut:fix/stdio-transport-socket-stdev-trap

Conversation

@NayanBhut

Copy link
Copy Markdown

Fixes #327

Root cause

Since #297, StdioTransport.connect() records the device and inode of its input/output file descriptors to detect descriptor reuse before restoring blocking mode. It converted st_dev/st_ino with UInt64(status.st_dev).

Node's child_process.spawn connects a server's stdio to socketpairs, and on Darwin fstat() on a socket reports st_dev == -1. dev_t is a signed Int32 there, so UInt64(status.st_dev) traps with "Negative value is not representable" during connect(). Every Node-based MCP client hits this: Claude Desktop and Claude Code report "Connection closed" right after initialize. Shell pipes report st_dev == 0, which is why terminal testing never caught it.

Fix

Convert by bit pattern instead of value in both the save path (setNonBlocking) and the restore path (restoreFlags):

device: UInt64(truncatingIfNeeded: status.st_dev),
inode: UInt64(truncatingIfNeeded: status.st_ino)

The values are only compared for identity between save and restore, so bit-pattern conversion is safe on every platform: a no-op where dev_t is unsigned (Linux), and trap-free where it is signed (Darwin).

Regression test

Adds a test that connects a StdioTransport over a socketpair() and exchanges a message, mirroring how Node clients spawn servers. On Darwin this traps without the fix; on Linux it exercises the socket-backed stdio path.

Testing

  • swift test — 498/498 pass (Swift 6.1, Debian 12, matching CI's Linux job)
  • swift test --filter StdioTransportTests — 12/12 pass
  • scripts/run-conformance.sh --mode server — 47/47 pass
  • Client conformance runner hangs in my environment, but I verified it also hangs on a clean checkout of main, so it is unrelated to this change

AI disclosure

Assisted by an AI coding agent (Darwin) for implementation and testing. The approach follows the analysis in #327; the change was reviewed, built, and tested by me, and I can explain and defend every line.

Convert st_dev and st_ino to UInt64 by bit pattern
(truncatingIfNeeded) instead of value when saving descriptor state
in setNonBlocking and comparing it in restoreFlags.

Node's child_process.spawn connects a server's stdio to socketpairs,
and on Darwin fstat() on a socket reports st_dev == -1. dev_t is a
signed Int32 there, so UInt64(status.st_dev) traps with "Negative
value is not representable" during connect(), killing every
Node-based MCP client (Claude Desktop, Claude Code report
"Connection closed" right after initialize). Shell pipes report
st_dev == 0, which is why terminal testing never caught it.

The device and inode values are only compared for identity between
save and restore, so converting by bit pattern is safe on every
platform: a no-op where dev_t is unsigned (Linux), and trap-free
where it is signed (Darwin).

Adds a regression test that connects a StdioTransport over a
socketpair and exchanges a message, mirroring how Node clients
spawn servers.

Fixes modelcontextprotocol#327

Assisted-by: AI coding agent (Darwin). The approach follows the
analysis in the issue; all code was reviewed and tested by the
author.
@varun06

varun06 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Thanks for working on this code change. We've already got a fix for this in a larger set of changes that should land on main soon, so this one will likely be superseded. I'll link the changes here when it's up.

@NayanBhut

Copy link
Copy Markdown
Author

Thanks for the heads-up, @varun06! Happy to step aside for the larger fix. If it's useful, I'd be glad to verify the new changes against the Node child_process.spawn repro from #327 — I already have a socketpair test setup that exercises this path. Otherwise I'll close this PR once your changes land to avoid duplication.

Also, if there's another issue where an extra pair of hands would help, I'd love to take it on.

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.

StdioTransport traps ("Negative value is not representable") when stdio is a socket, e.g. spawned by Node clients

2 participants