fix(mcp): shut down scoped executor on session eviction (#1917) - #1971
Open
Adityakk9031 wants to merge 2 commits into
Open
fix(mcp): shut down scoped executor on session eviction (#1917)#1971Adityakk9031 wants to merge 2 commits into
Adityakk9031 wants to merge 2 commits into
Conversation
…reCo#1917) Retain the scoped Executor created for each MCP session and shut it down when an in-memory MCP session is evicted, closed, or fails eager initialization. This ensures child tool subprocesses and connection pools owned by the session are properly cleaned up.
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
Retain the scoped Executor created for each MCP session and shut it down when an in-memory MCP session is evicted, closed, or fails eager initialization. This ensures child tool subprocesses and connection pools are properly released.
Linked issue
Fixes #1917
Root Cause
Executor creates a scoped
Executorfor each MCP session inmakeMcpBuildServerviamakeExecutionStack(principal.accountId, principal.organizationId, ...). This scoped executor owns plugin instances, including@executor-js/plugin-mcp, which manages a connection pool holding spawned child tool subprocesses (e.g. stdio MCP tools).However,
makeMcpBuildServerpreviously returned only{ mcpServer, engine }, discarding theexecutorreference. Furthermore,BuiltMcpServerandmakeInMemoryMcpSessionStoreonly tracked and closedtransport,mcpServer, andengine. When an idle session was evicted or closed:transport.close(),server.close(), andengine.shutdownwere executed.executor.close()was never invoked.runtimes.values()never hadplugin.close()called,connectionPool.close()was never invoked, and tool subprocesses remained running indefinitely in the host system.Changes
@executor-js/host-mcp:BuiltMcpServerto optionally acceptexecutor?: Executorandclose?: () => Promise<void>.makeInMemoryMcpSessionStoreto trackexecutorsandclosersper session ID.shutdownExecutorhelper and wired it intodispose(), eager initialization failure cleanup (onClose), and store shutdown (close()).in-memory-session-store.test.tsverifying executor shutdown upon idle session eviction and store teardown.@executor-js/api:executorfrommakeMcpBuildServeralongsidemcpServerandengine..changeset/mcp-session-executor-leak.md.Verification
bun run format:check— all matched files formattedbun run lint— 0 warnings, 0 errorsbun run --cwd packages/hosts/mcp typecheck— 0 errorsbun run --cwd packages/core/api typecheck— 0 errorsbun run --cwd packages/hosts/mcp test— 223 passedbun run --cwd packages/core/api test— 119 passedbun x vitest run src/mcp/mcp.test.ts(inapps/host-selfhost) — 5 passedChecklist
bun run changeset), or this change needs none.