Repository navigation
Conversation
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.
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. |
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 Also, if there's another issue where an extra pair of hands would help, I'd love to take it on. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 convertedst_dev/st_inowithUInt64(status.st_dev).Node's
child_process.spawnconnects a server's stdio to socketpairs, and on Darwinfstat()on a socket reportsst_dev == -1.dev_tis a signedInt32there, soUInt64(status.st_dev)traps with "Negative value is not representable" duringconnect(). Every Node-based MCP client hits this: Claude Desktop and Claude Code report "Connection closed" right afterinitialize. Shell pipes reportst_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):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_tis unsigned (Linux), and trap-free where it is signed (Darwin).Regression test
Adds a test that connects a
StdioTransportover asocketpair()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 passscripts/run-conformance.sh --mode server— 47/47 passmain, so it is unrelated to this changeAI 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.