kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

[Bug] stdio MCP server process can outlive its client when the connection is lost without close()

Open ploteddie-bit opened this issue 3 weeks ago • 4 comments

Version

  • Kimi Code CLI: 2.6.0-canary.20261106122412 (commit eb2035f3cad57cc12d4d5082ec626f7f0ea5f651)
  • Platform: WSL2 (Linux), single-binary distribution
  • MCP client SDK in use: @modelcontextprotocol/sdk 1.32.2 (dist/esm/client/stdio.js)

What I'm seeing

A stdio MCP server process can outlive the client that spawned it.

Concretely, with a local MCP gateway plugin (a stdio server written in Python that proxies other MCP servers), the runner replaced the client instance several times while the previous server processes stayed alive: 3-4 gateway processes, each with 9 upstream MCP servers, about 2 GB of resident memory in total. All of them were in state S, 12-13 threads, 6 open sockets, idle (0 % CPU).

The server process does not exit when the client closes its stdin: the MCP Python server keeps running because its app.run() never returns on EOF. On the client side, the process is only killed when close() is called on a client whose _process handle is still set (client/stdio.js:143-170).

Reproduction

  1. Register a stdio MCP server (for example through a plugin) whose process stays alive after its stdin is closed - the default for a Python MCP server that does not watch stdin.
  2. Let the runner replace that client instance: server restart, config reload, or simply killing the server process by hand.
  3. Observe the previous server processes still running, with their upstream children.

Any stdio MCP server written in Python that does not watch stdin should show the same behaviour, so the plugin itself is not required to reproduce it.

Impact

  • Roughly 500 MB per duplicated instance (the gateway plus its upstream servers).
  • The number of live instances grew over the course of a session, and each duplication is invisible without inspecting ps.

Two questions / suggestions

  1. Cleanup when a client is abandoned. When the runner gives up on a stdio client, is the server process always signalled? StdioClientTransport.close() only kills the child while this._process is still set, and _process is cleared in the close event handler (client/stdio.js:83-84). If the connection is lost without close() being called, the server process can survive. Would it be reasonable to always terminate the child process when the client is dropped, even if the transport already reported itself closed?
  2. Observability. Could the runner expose the number of live MCP server processes per server name (a counter in /mcp, or a log line on connect)? A duplicated instance is currently invisible without inspecting ps.

Workaround used on my side

Relaying stdin through a pipe inside the server process: a daemon thread reads the real stdin and writes it into an os.pipe() whose read end is dup2'd onto fd 0. The SDK then consumes the pipe, and the thread sees the EOF. On EOF the server terminates its process group.

Verified: the process exits 0.26 s after the pipe is closed, with no change to the MCP protocol (the MCP handshake and all tool calls behave identically).

Related

  • #4028 - resident child processes surviving session end. Different code path (Bash tasks rather than MCP stdio servers), but the symptom - no code path signals the process - is similar.

ploteddie-bit avatar Nov 07 '26 18:11 ploteddie-bit

I can take this if nobody else is on it yet.

Reproduced on main (eb2035f3c) with a small vitest case: server finishes the handshake, then closes stdout but keeps running (basically what a Python server that never returns from app.run() looks like). The transport sees the stdout end, runs finish() and just drops the process handle — no kill. After that close() is a no-op because the transport already thinks it's closed, so the child is still alive afterwards (checked with kill(pid, 0)).

So the repo's own transport (packages/agent-core-v2/src/mcpCore/client-stdio.ts) has the same gap described here, on the finish() path.

My plan for the fix: make finish() actually terminate the child if it's still running (same SIGTERM -> grace -> SIGKILL sequence close() already does), plus a regression test with a server that severs stdout after the handshake. Happy to adjust the scope if you'd rather see it handled elsewhere.

GooseKIller avatar Nov 07 '26 22:11 GooseKIller

Confirming this diagnosis: @GooseKIller's analysis is exactly right, and I verified it with a reproduction against main.

finish() in packages/agent-core-v2/src/mcpCore/client-stdio.ts sets closed = true and only calls process.dispose(), which destroys the stdio streams but never signals the child (HostProcess.dispose() in packages/agent-core-v2/src/os/backends/node-local/hostProcessService.ts:216). A later close() is a no-op because closed is already true, so the SIGTERM → grace → SIGKILL sequence never runs. A child that keeps running after closing its stdout (common with Python MCP servers) is therefore orphaned, and each reconnect spawns a replacement — matching the leak in #4794.

I have a fix ready that extracts the terminate sequence from close() into a terminate() helper and calls it from finish() too, plus a regression test (fixture server severs stdout at 150ms and keeps running; test asserts the child is dead after the transport finishes). Branch: https://github.com/Sathwik-giddi/kimi-code/tree/fix/stdio-mcp-terminate-on-finish

Full write-up and repro details are in https://github.com/MoonshotAI/kimi-code/issues/4794#issuecomment-6822344377. Since this is an external fix, I'll open the PR once the linked issue is approved — could a maintainer weigh in on which issue to link? Thanks!

Sathwik-giddi avatar Nov 12 '26 11:11 Sathwik-giddi

Nice work confirming this, @Sathwik-giddi. Your fix matches what I prepared after my first comment here: terminate() pulled out of close() and called from finish() too, plus a regression test where the server severs stdout and keeps running. Branch: https://github.com/GooseKIller/kimi-code/tree/fix/mcp-stdio-terminate-orphan-on-finish Maintainers, take whichever version works for you. I can open mine once approved, or help review the other one.

GooseKIller avatar Nov 12 '26 11:11 GooseKIller

Thanks @GooseKIller — good to see the same root cause converge from two independent investigations; that is a strong signal the diagnosis is right. For whichever version the maintainers pick: my branch (linked from my #4794 comment) carries the full verification trail — a red→green regression test (the child survives 8s on main, exits with the fix), a changeset, and a full agent-core-v2 suite run where the four unrelated failures were verified pre-existing on a clean checkout. Happy to review yours too — either way the leak gets fixed.

Sathwik-giddi avatar Nov 12 '26 12:11 Sathwik-giddi