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

MCP stdio reconnect leaks server processes (old process never terminated; 144 orphaned node processes OOM'd a 128 GB server)

Open Dobroeutro1 opened this issue 3 weeks ago • 3 comments

Summary

On Linux (2.5.2), stdio MCP servers are leaked on reconnect: when the client decides a server is unavailable and schedules a reconnect, the previous server process is not terminated and keeps running for hours. Combined with a double-spawn at session start, long-running sessions accumulate many copies of every MCP server. On our 128 GB server this grew to 144 node processes (~0.9 GB RSS each) and triggered a global OOM that killed unrelated services.

Evidence

Client log (~/.kimi-code/logs/kimi-code.log, two concurrent sessions):

  • mcp server reconnect scheduled — 1589 events; mcp server unavailable ... closed unexpectedly — 178 events.
  • At session start the same server is spawned twice, 3 ms apart (jira-easy-mcp, stderr timestamps 07:25:10.012 and 07:25:10.015). mcp.json has 9 unique servers, no duplicates.
  • ~5 minutes after session start, all 9 servers are declared closed unexpectedly within a 16-second window (mass recycle), and reconnect attempts start spawning replacements.
  • Processes declared closed unexpectedly remain running: the two jira-easy-mcp instances from 07:25:10 were still alive 5+ hours later (ps verified, still children of the live session). The client apparently closes the stdio pipes but never signals/kills the child; the servers also don't exit on stdin EOF.
  • The churn rate correlates with MCP tool usage: during an active agent session new server generations appear every few minutes (pids 6606 → 172071 → 233766 → 285988 for the same session within ~40 min).
  • journalctl on 2026-11-09: kernel OOM killer, 124 GB anonymous memory, page cache at 0; victims included the user's systemd manager and an unrelated task queue daemon.

Expected behavior

  1. One process per configured MCP server per session (no double spawn at init).
  2. On reconnect, terminate the previous server process (ideally its process group) before spawning a replacement.
  3. On client exit (including crash/SIGKILL where possible via pipe close), the server process must not outlive the session — stdio servers should exit on stdin EOF (this part is also on server implementations, but the client should enforce it with a kill).

Environment

  • Kimi Code CLI 2.5.2 (2026-11-06), installed via official script
  • Ubuntu server, Linux x86_64, 128 GB RAM
  • MCP servers: local-coder, jira-easy-mcp, google-docs-mcp, chrome-devtools-mcp, codex-security (plugin), github, confluence, postgres, agent-memory

Happy to provide the full client log or run diagnostics builds.

Dobroeutro1 avatar Nov 10 '26 08:11 Dobroeutro1

Root-cause analysis for the stdio MCP server leak (also matches the diagnosis @GooseKIller laid out in #4745):

What happens

When a stdio MCP server closes its own stdout after the handshake while staying alive (typical for Python-based MCP servers that ignore stdin EOF), the client transport's finish() path fires:

  • packages/agent-core-v2/src/mcpCore/client-stdio.ts — finish() sets closed = true and only calls process.dispose(), which in HostProcess.dispose() (packages/agent-core-v2/src/os/backends/node-local/hostProcessService.ts:216) merely destroys the stdio streams. It never signals the child.
  • Because closed is already true, a later close() is a no-op, so the SIGTERM → grace → SIGKILL sequence that close() implements never runs either.
  • The connection manager sees the transport close as a hard failure and reconnects (reconnectAndJoin), spawning a replacement server. The old child, with no live stdin/stdout pipes but an open event loop, keeps running forever.

Result: each reconnect leaks one orphaned server process — matching the reported 144 orphaned node processes / 1589 reconnect events and eventual OOM.

Fix

Extract the existing terminate sequence from close() into a terminate() helper and invoke it from finish() as well (SIGTERM → 5s grace → SIGKILL, same semantics as before). Reconnect behavior is unchanged; only the orphan cleanup is added.

Regression test: a fixture server that answers initialize, then severs stdout at 150ms and keeps running — the test asserts the child is actually dead after the transport finished. Fails on main (child survives 8s), passes with the fix.

Branch with the fix, test, and changeset ready: https://github.com/Sathwik-giddi/kimi-code/tree/fix/stdio-mcp-terminate-on-finish

Per the contributing guide, this is an external fix, so I'll open the PR linking this issue once it's approved — could a maintainer (/approve) take a look? Thanks!

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

Title: Related observation: client declares a healthy stdio server "closed unexpectedly" at every session start (2.4.1→2.6.0), reconnects, then works

Body:

Related observation, offered as additional evidence — and explicitly not claiming the same root cause as the orphan-process leak you describe:

On our setup (Linux, observed across kimi 2.4.1 → 2.5.2 → 2.5.3 → 2.6.0), every session start logs:

ERROR mcp server unavailable  server=context7 transport=stdio status=failed reason="MCP server \"context7\" closed unexpectedly\nstderr: [context7-rotator] Ready. 4 API keys loaded."
  • 159 such entries between 2026-11-05 and 2026-11-13 — i.e. starting in the 2.4.1 era, before 2.5.2.
  • The server side is healthy: it initializes, answers tools/list, stays alive on idle stdin, and exits cleanly on EOF (verified by driving it directly).
  • After the reconnect, real MCP tool calls (resolve-library-id, query-docs) succeed in the same session — we captured this in a single fresh -p session: mcp server unavailable … closed unexpectedly at start, reconnect scheduled delayMs=3598 attempt=1, then a successful resolve-library-id call returning a real library id.
  • We often see the failure line duplicated ~3 ms apart at startup — which resembles your "servers double-spawn ~3 ms apart" observation, though from the log side we cannot see process ids.
  • What we have not verified is whether the previous server process survives as an orphan (your leak). Our observation only proves client-side disconnect/reconnect churn against a healthy server.

So: the client declares a healthy stdio MCP server "closed unexpectedly" at session start, then reconnects and works. If your reconnect path respawns without terminating the previous process, both symptoms could share that path — but we are not asserting that. Happy to share full logs if useful.

wintrover avatar Nov 13 '26 05:11 wintrover

Thanks @wintrover — this corroborates the diagnosis well. The ~3 ms duplicated failure at startup matches the double-spawn we observed from the process side (two servers for one client entry, both alive after the reconnect). The "closed unexpectedly" on a healthy server is consistent with the client finishing a superseded spawn attempt while the first connection attempt is still in flight — and per the issue above, the abandoned process is never terminated, which is the leak this issue tracks. Your EOF-on-stdin exit behavior on the server side is the third independent confirmation that the server does its part and the client is what abandons it.

Sathwik-giddi avatar Nov 13 '26 07:11 Sathwik-giddi