mirror of
https://github.com/oven-sh/bun
synced 2026-02-28 12:31:00 +01:00
## Summary - Remove `shutdown()` calls on subprocess stdio socketpair file descriptors that were causing Python asyncio-based MCP servers to break ## Root Cause Bun uses `SOCK_STREAM` socketpairs for subprocess stdio pipes. After creating each socketpair, it called `shutdown(SHUT_WR)` on the parent's read end (for stdout/stderr) and `shutdown(SHUT_RD)` on the parent's write end (for stdin) to make them unidirectional. On `SOCK_STREAM` sockets, `shutdown(fd, SHUT_WR)` sends a **FIN** to the peer. Python's `asyncio.connect_write_pipe()` registers an `EPOLLIN` watcher on the write pipe fd to detect peer closure. The FIN from `shutdown()` triggers an immediate `EPOLLIN` event, causing asyncio to interpret it as "connection closed" and tear down the write transport — even though the pipe should remain open. This broke **all Python MCP servers** using the `model_context_protocol` SDK (which uses `connect_write_pipe()` in its stdio transport) whenever they took more than a few seconds to initialize. Node.js does not have this issue because it does not call `shutdown()` on its socketpairs. ## Fix Remove the `shutdown()` calls entirely. The socketpairs are already used unidirectionally by convention, and the `shutdown()` calls provided no functional benefit while causing compatibility issues with any program that polls its stdio fds for readability/writability events. ## Test plan - [x] Added regression test `test/js/bun/spawn/spawn-socketpair-shutdown.test.ts` with 3 test cases: - Subprocess stdout pipe stays writable after idle delay - Python asyncio `connect_write_pipe` works correctly with idle period - Subprocess stdin pipe stays readable for child after idle delay - [x] Verified test fails on system bun (without fix) and passes on debug build (with fix) - [x] Verified existing spawn tests still pass (`spawn-streaming-stdout`, `spawn-stdin-readable-stream`) - [x] Verified original bug report repro script works with the fix 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com>