Skip to content

stdio server spins forever at 100%+ CPU when the client dies: uncaughtException handler re-throws EPIPE on a broken stderr #1274

Closed

Description

@vagos-pro

Summary

When the MCP client process dies without sending SIGINT/SIGTERM (editor window closed, client crash, kill -9), all three stdio pipes break at once. The server then hits EPIPE writing to stdout, which triggers uncaughtException. The handler's very first statement writes to stderr — which is also broken — throwing a new EPIPE inside the handler itself. Node re-invokes the same handler, and it recurses forever.

The process never exits. It gets reparented to PID 1 and burns 100–300% CPU on GC thrash with RSS growing into the gigabytes, until it is killed manually or the machine reboots. Since every new client session spawns a fresh server, orphans accumulate silently.

Affected code

packages/mcp-server/src/index.ts:328 — present in current main and shipped in 0.39.0:

process.on("uncaughtException", async (error) => {
  console.error("Uncaught exception:", error);   // throws EPIPE when stderr is gone
  Sentry.captureException(error);
  await Sentry.flush(SENTRY_TIMEOUT);
  process.exit(1);
});

Same shape for unhandledRejection at :335. Because process.exit(1) sits after the throwing statement (and after an awaited Sentry.flush), it is never reached.

Reproduction

Standalone, no editor involved:

const { spawn, execSync } = require("child_process");
const p = spawn("npx", ["-y", "@sentry/[email protected]",
  "--host=sentry.example.com", "--organization-slug=org", "--project-slug=proj"],
  { stdio: ["pipe", "pipe", "pipe"], env: { ...process.env, SENTRY_ACCESS_TOKEN: "<token>" } });
p.stdout.resume(); p.stderr.resume();
p.on("exit", (c) => console.log("EXITED code=" + c));

const send = (o) => { try { p.stdin.write(JSON.stringify(o) + "\n"); } catch {} };
setTimeout(() => send({ jsonrpc: "2.0", id: 1, method: "initialize",
  params: { protocolVersion: "2025-11-25", capabilities: {}, clientInfo: { name: "t", version: "1" } } }), 500);

setTimeout(() => {
  p.stderr.destroy();          // <-- the client is gone, so stderr is gone too
  send({ jsonrpc: "2.0", id: 2, method: "tools/list", params: {} });
  p.stdout.destroy();
  p.stdin.destroy();
}, 4000);

[3, 8, 16].forEach((t) => setTimeout(() => {
  try { console.log(`+${t}s:`, execSync(`ps -o pid,%cpu,rss,stat -p ${p.pid} | tail -1`).toString().trim()); }
  catch { console.log(`+${t}s: dead`); }
}, 4000 + t * 1000));

Observed

Scenario Result
Close stdin only exits cleanly, code=0
Break stdin+stdout, stderr alive exits, code=1 — handler works as intended
Break all three, stderr broken spins forever at ~110% CPU, needs SIGKILL

Output of the third case:

+3s:  14714 111.2 382288 R
+8s:  14714 108.7 397584 R
+16s: 14714 107.8 217152 R

Reproduces identically on a supported Node version — v26.7.0, where RSS grows monotonically instead:

+4s:  20529 115.1  649760 R
+10s: 20529 111.5 1281088 R
+18s: 20529 112.1 2111648 R

2 GB in 18 seconds, and it keeps going. So this is not an artifact of running below the declared engines floor.

sample <pid> on a stuck process:

node::errors::TriggerUncaughtException
  v8::internal::MessageHandler::ReportMessage
    v8::internal::Isolate::ReportPendingMessages
      [JS frame recursing into itself]
        v8::internal::Runtime_AllocateInYoungGeneration
          v8::internal::Heap::CollectGarbage
            v8::internal::Sweeper::LocalSweeper::ParallelSweepSpace

lsof shows zero network connections on the stuck process, so this is not a hung request to Sentry.

Impact

On one developer machine after a normal working day: 4 server instances alive, 3 of them orphans (PPID=1), together 343% CPU and 2.7 GB RSS, none doing any work. The client had logged the disconnects as STDIO connection closed (cleanly) — from the client's point of view nothing was wrong.

Suggested fix

Make the fatal handler re-entrancy-safe and never let logging throw:

let handlingFatal = false;

function onFatal(err: unknown) {
  if (handlingFatal) process.exit(1);   // stderr is gone too — just die
  handlingFatal = true;
  try { console.error("Fatal error:", err); } catch {}
  Sentry.captureException(err);
  const t = setTimeout(() => process.exit(1), SENTRY_TIMEOUT);
  t.unref();
  Sentry.flush(SENTRY_TIMEOUT).finally(() => process.exit(1));
}

process.on("uncaughtException", onFatal);
process.on("unhandledRejection", onFatal);

Three separate things this addresses:

  1. try/catch around the log — a broken stderr must not throw.
  2. A re-entrancy guard — the second fatal error exits immediately instead of recursing.
  3. process.exit(1) no longer depends on Sentry.flush resolving; the timer guarantees the exit.

Worth considering alongside: exit when stdin reaches EOF, and attach an error listener to process.stdout/process.stderr so EPIPE is handled at the source.

Workaround

Redirect the server's stderr to a file so console.error can't throw, e.g. wrap the command as sh -c 'exec npx ... 2>>/tmp/sentry-mcp.log'. With stderr writable the handler completes and the process exits normally.

Environment

  • @sentry/mcp-server 0.39.0 (current latest), stdio transport
  • Node v20.20.2 and v26.7.0 — reproduces on both
  • macOS 15 (darwin 25.6.0, arm64)
  • Self-hosted Sentry
  • Client: Claude Code

Activity

  1. linear-code commented on Aug 28, 2026

    @linear-code
  2. added a commit that references this issue on Aug 28, 2026
    45872d2
  3. added a commit that references this issue on Aug 28, 2026
    2180999
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions