fix: handle large IPC messages in daemon socket communication#218
Open
amitzur wants to merge 1 commit intophilschmid:mainfrom
Open
fix: handle large IPC messages in daemon socket communication#218amitzur wants to merge 1 commit intophilschmid:mainfrom
amitzur wants to merge 1 commit intophilschmid:mainfrom
Conversation
Bun's socket.write() only writes up to the kernel buffer size (~8KB). The daemon was silently dropping the remainder of large responses (e.g. listTools with many tools), causing the client to time out. Extract IPC primitives into src/ipc.ts with drain-based write buffering on the server side and newline-delimited read buffering on the client side. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Problem
The daemon IPC layer silently drops data when responses exceed ~8KB (the kernel socket buffer size). This causes
Daemon request timeouterrors for MCP servers that return many tools (e.g.next-devtools,brainshop, or any server with >~10 tools with schemas).Root cause
Bun's
socket.write()on Unix sockets only writes up to the kernel buffer size (typically 8192 bytes) and returns the number of bytes actually written. The original code ignored this return value — so for a 42KBlistToolsresponse, only the first 8KB was sent, and the client waited forever for the rest.On the client side, the
datacallback assumed the entire response would arrive in a single event. For responses that did partially arrive, the incomplete JSON failed to parse and was discarded.How it manifests
Servers with small tool lists (e.g.
filesystemwith 14 tools,deepwikiwith 3) worked fine because their responses fit within the 8KB buffer. The ping request (60 bytes) always succeeded, so the daemon appeared connected but non-functional.Fix
Extracted the IPC socket primitives into a shared
src/ipc.tsmodule used by bothdaemon.tsanddaemon-client.ts:Server side (
createIpcServer):writeAll()tracks partial writes — whensocket.write()returns less than the payload length, the unsent remainder is stored in a per-socket bufferdraincallback resumes writing when the kernel buffer frees up, handling payloads that require multiple drain cyclesClient side (
sendIpcRequest):datachunks in a buffer until the newline delimiter (\n) is foundclosein case the newline was lostsettledguardTest plan
tests/daemon-socket.test.ts— 3 integration tests using a mock MCP server (tests/fixtures/mock-mcp-server.ts) that registers 50 tools via the official MCP SDK, producing alistToolsresponse well over 8KB:writeAll+ drain + client buffering code path-dflag for an even larger payloadbun run buildcompiles cleanlynext-devtoolsandbrainshopservers🤖 Generated with Claude Code