fix(lsp-daemon): terminate proxy on stdin close to prevent orphaned processes#5561
Open
PaoloC68 wants to merge 2 commits into
Open
fix(lsp-daemon): terminate proxy on stdin close to prevent orphaned processes#5561PaoloC68 wants to merge 2 commits into
PaoloC68 wants to merge 2 commits into
Conversation
…rocesses When the parent process (opencode) exits, the MCP proxy's stdin closes but the proxy can survive indefinitely due to dangling daemon socket handles keeping the Node event loop alive. Over multiple sessions this accumulates orphaned proxy processes (and their associated language servers), consuming hundreds of MB of RAM. Two fixes: - Listen for stdin 'close' and force-exit after a 500ms grace period - Catch ERR_STREAM_PREMATURE_CLOSE from the server loop (parent killed mid-request) instead of propagating it as an unhandled error - Force process.exit(0) after the server loop returns normally when running on real stdin, so dangling handles cannot keep the process alive Includes regression test: stdin destroyed mid-session → proxy resolves.
Collaborator
|
Thanks @PaoloC68. Your orphan-prevention fix for the lsp-daemon proxy reviews cleanly and pairs well with the idle-timeout change in #5461. I cannot merge it yet: the test (ubuntu-latest) check is red on dev itself due to a pre-existing failure in packages/shared-skills/provenance-gate.test.ts, unrelated to your change. Once dev is green again I will re-run and merge. Nothing needed from you. |
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
Fixes #5560 — lsp-daemon MCP proxy processes accumulate as orphans across sessions.
When the parent process (opencode) exits, the proxy's stdin closes but dangling daemon socket handles keep the Node event loop alive, leaving the proxy (and its language server children) running indefinitely. Over multiple sessions this accumulates orphaned processes consuming 1+ GB of RAM.
Changes
packages/lsp-daemon/src/proxy.ts:closeevent and forceprocess.exit(0)after a 500ms grace period (handles parent killed via SIGKILL)ERR_STREAM_PREMATURE_CLOSEfrom the server loop instead of propagating it as unhandled (stdin destroyed mid-request)process.exit(0)after the server loop returns normally when running on real stdinAll three paths are guarded by
isRealStdin(input === process.stdin) so test harnesses using synthetic streams are unaffected.packages/lsp-daemon/test/proxy.test.ts:Test Results
Root Cause Analysis
The
for await (const chunk of input)loop inreadStdioJsonRpcMessagesexits on EOF, but:destroy()on stdin throwsERR_STREAM_PREMATURE_CLOSEwhich was unhandledSummary by cubic
Terminate the
lsp-daemonMCP proxy when stdin closes to prevent orphaned processes and memory leaks. Also handle premature stream closes and exit after the server loop so dangling sockets can’t keep the process alive.ERR_STREAM_PREMATURE_CLOSEfrom the server loop instead of propagating it.Written for commit 6d081a8. Summary will update on new commits.