Skip to content

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

Open
branben/swift-sdk
#6

Description

@sethwebster

Summary

Since #297 (merged 2026-10-31, on main, not in 0.12.1), StdioTransport.connect() traps when stdin/stdout is a socket:

Fatal error: Negative value is not representable

Node's child_process.spawn with stdio: 'pipe' uses socketpairs on macOS, and fstat on a socket reports st_dev == -1. dev_t is signed (Int32 on Darwin), so UInt64(status.st_dev) traps. Every Node-based MCP client hits this: Claude Desktop and Claude Code both report "Connection closed" right after initialize.

Affected lines: StdioTransport.swift L147, L159–160. Symbolicated crash: StdioTransport.setNonBlocking(fileDescriptor:) (StdioTransport.swift:147) ← StdioTransport.connect() ← Server.start(transport:).

Repro (macOS)

Build any server using StdioTransport(), then:

// node repro.js /path/to/server
const p = require("child_process").spawn(process.argv[2], [], { stdio: ["pipe", "pipe", "inherit"] });
p.on("exit", (c, s) => console.log("exit", c, s)); // → exit null SIGTRAP
p.stdin.write(JSON.stringify({ jsonrpc: "2.0", id: 0, method: "initialize",
  params: { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "n", version: "1" } } }) + "\n");

Shell pipes don't reproduce it (st_dev == 0), which is why terminal testing passes:

$ python3 -c "import os,socket; a,_=socket.socketpair(); r,_=os.pipe(); print(os.fstat(a.fileno()).st_dev, os.fstat(r).st_dev)"
-1 0

Fix

Convert by bit pattern instead of value (the values are only compared for identity):

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

(and the same in restoreFlags). Patch with this change: noisyneighborstudio@b7a26b8. Happy to open a PR with it plus a socketpair regression test.

Activity

  1. NayanBhut commented on Nov 23, 2026

    @NayanBhut

    Hi, I've opened #334 fixing this with the bit-pattern conversion suggested above, plus a socketpair regression test. Happy to adjust if maintainers prefer a different approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions