fix(mcp): exit serve process on stdin-close/SIGTERM#692
Closed
joshsteinvc wants to merge 1 commit intogarrytan:masterfrom
Closed
fix(mcp): exit serve process on stdin-close/SIGTERM#692joshsteinvc wants to merge 1 commit intogarrytan:masterfrom
joshsteinvc wants to merge 1 commit intogarrytan:masterfrom
Conversation
MCP stdio server was keeping the bun process alive indefinitely after the client disconnected. Over days this accumulated 20+ orphaned gbrain serve processes, all holding the PGLite directory open. Since PGLite is single-writer, this caused write-lock contention that made email-sync fail its 15s per-put timeout: 114 puts x 15s = 28.5min runs with 0 emails written. Now listens for stdin end/close, transport close, and SIGTERM/SIGINT/ SIGHUP; calls engine.disconnect() and exits cleanly. Root cause for the no-gbrain-run-in-50h alert.
5 tasks
Owner
|
Closing — your fix landed in master via the v0.30.3 fix-wave PR #776 (merged at Thank you for the contribution — credit is preserved in the v0.30.3 CHANGELOG entry. 🙏 |
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.
Summary
The MCP stdio server in
src/mcp/server.tsdoesn't exit when its client disconnects. Thebunprocess keeps running, holding open the PGLite data directory.Symptom that surfaced this
Over days of normal use, 20+ orphaned
gbrain serveprocesses accumulated. Because PGLite is single-writer, they contended for the write lock. Email sync hit its 15s per-put timeout on every page: 114 puts × 15s = 28.5min runs that wrote 0 emails. This was the root cause behind a "no gbrain run in 50h" alert in my setup.Fix
After
server.connect(transport), listen for the standard MCP stdio shutdown signals:process.stdinendandclose(client disconnect → EOF on stdin)transport.onclose(MCP SDK's own signal)SIGTERM/SIGINT/SIGHUP(orchestrator shutdown)Each path calls
engine.disconnect?.()andprocess.exit(0). AshuttingDownflag prevents double-execution if multiple signals fire.Test plan
gbrain serveleft running after Claude Code disconnected; orphans stacked up across days[gbrain-serve] shutdown: stdin end\non stderr → process exits cleanlyengine.disconnect()is awaited before exit so PGLite lock releasesFiles
src/mcp/server.ts— +20 lines, no removals🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmithwith what you need.