Close MCP transport gracefully on shutdown#131
Open
20syldev wants to merge 1 commit into
Open
Conversation
When the MCP host (e.g. Claude Code) sends SIGTERM to terminate the server, the shutdown handler was calling process.exit(0) without first closing the MCP server transport. The host received an abrupt EOF on the stdio channel and interpreted it as a server failure, reporting "1 MCP server failed" even though the exit code was 0. Two related fixes: 1. Call await server.close() in index.ts shutdown() before process.exit() so the MCP SDK can send a proper close notification over the transport. 2. Remove process.exit(0) from browser-eval-manager.ts signal handlers. Those handlers fired concurrently with the ones in index.ts, causing process.exit(0) to be called before index.ts could flush telemetry or close the MCP transport. Signal handling and process exit are now owned exclusively by index.ts. Fixes vercel#126 Relates to vercel#113
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
When the MCP host (e.g. Claude Code) sends
SIGTERMto terminate the server on exit, users see:even though everything worked correctly. The exit code is
0, but the host interprets the shutdown as a failure.Root cause
Two issues were combining to cause this:
1. MCP transport was never closed before
process.exit()index.ts'sshutdown()calledprocess.exit(0)immediately after spawning the telemetry flush process. The MCPServerobject was never told to close, so the stdio transport received an abrupt EOF instead of a clean close notification. The host saw the broken pipe and reported a failure.2. Duplicate signal handlers racing each other
browser-eval-manager.tsregisters its ownSIGINT/SIGTERMhandlers that both stop the Playwright connection and callprocess.exit(0). These fired concurrently with the handlers inindex.ts, causingprocess.exit(0)to be called beforeindex.tshad a chance to flush telemetry or close the MCP transport.Fix
src/index.ts— makeshutdown()async and callawait server.close()beforeprocess.exit():src/_internal/browser-eval-manager.ts— removeprocess.exit(0)from the signal handlers (owned exclusively byindex.ts):process.on("SIGINT", async () => { await stopBrowserEvalMCP() - process.exit(0) }) process.on("SIGTERM", async () => { await stopBrowserEvalMCP() - process.exit(0) })Fixes #126, relates to #113