fix: dispose LSP children on MCP stdio close (#47) - #48
Open
Fortune0001 wants to merge 1 commit into
Open
Conversation
cclsp only handled SIGINT/SIGTERM, but the MCP protocol's canonical shutdown signal on a stdio transport is parent-stdin close, not POSIX signals. Without a stdin close/end handler the server kept running after the parent client disconnected, leaving spawned LSP children (pylsp.exe, typescript-language-server) orphaned. On Windows this accumulated to OOM-class memory pressure across long-running parent sessions (e.g. eval harnesses firing many claude -p calls). Add process.stdin 'close' and 'end' handlers that mirror the existing SIGTERM handler -- both invoke lspClient.dispose() and exit cleanly.
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.
Fixes #47.
Summary
Adds
process.stdincloseandendhandlers inindex.tsthat mirror the existing SIGTERM handler. Without these, when the parent MCP client (e.g.claude -p) exits and closes the stdio transport, cclsp keeps running and its spawned LSP children (pylsp, typescript-language-server) become orphaned. On Windows this accumulates to OOM-class memory pressure across long-running parent sessions.Change
process.on('SIGTERM', () => { lspClient.dispose(); process.exit(0); }); +process.stdin.on('close', () => { + lspClient.dispose(); + process.exit(0); +}); + +process.stdin.on('end', () => { + lspClient.dispose(); + process.exit(0); +});lspClient.dispose()already cleans up LSP children correctly (delegates toServerManager.dispose()insrc/lsp/). The only missing piece was invoking it on the stdio-close path.Test plan
bun test— 218 pass, 9 skip, 0 failclaude -pcalls in a loop in a Python-heavy workspace, then enumerate orphan python.exe processes whose command line containspylsp— should be 0 (was ~100 before the patch).Happy to iterate if you'd prefer a different shape (e.g.,
transport.oncloseinstead ofprocess.stdin.on('close')) — both achieve the same thing and I went with theprocess.stdinform to mirror the surrounding signal handlers. Let me know.