fix: harden dynamic tool handlers against deadlock, hangs, and runaway output#41
Open
electronicBlacksmith wants to merge 1 commit intoghostwright:mainfrom
Conversation
…y output Adds timeout protection, streaming drain to prevent pipe deadlock, and output byte capping for dynamic MCP tool handlers. - drainProcessWithLimits() drains stdout/stderr concurrently with a hard timeout (SIGTERM then SIGKILL after 2s grace period) - readStreamWithCap() caps output at 1MB but keeps reading past the cap so the child process never blocks on a full pipe - Configurable via PHANTOM_DYNAMIC_HANDLER_TIMEOUT_MS (default 60s) and PHANTOM_DYNAMIC_HANDLER_MAX_OUTPUT_BYTES (default 1MB)
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.
What Changed
drainProcessWithLimits()that drains stdout/stderr concurrently with a hard timeout (SIGTERM, then SIGKILL after 2s grace period)readStreamWithCap()that caps output at 1MB but keeps reading past the cap so the child process never blocks on a full pipePHANTOM_DYNAMIC_HANDLER_TIMEOUT_MS(default 60s) andPHANTOM_DYNAMIC_HANDLER_MAX_OUTPUT_BYTES(default 1MB)Why
Dynamic MCP tool handlers (shell/script) can hang indefinitely or produce unbounded output. A runaway
find /or infinite loop blocks the agent forever. If both stdout and stderr fill their OS pipe buffers (~64KB each), the child deadlocks waiting for a reader while the parent blocks reading one pipe.How I Tested
bun test src/mcp/__tests__/dynamic-handlers.test.ts- 25 pass (timeout, output cap, concurrent drain, graceful shutdown)bun test src/mcp/__tests__/dynamic-tools.test.ts- 8 passbun run lint- cleanbun run typecheck- cleanChecklist
bun test)bun run lint)bun run typecheck).envfiles included