mcp-server-cloudflare icon indicating copy to clipboard operation
mcp-server-cloudflare copied to clipboard

Orphaned process busy-loops at 100% CPU on closed stdin / client disconnect

Open FullThrottle83 opened this issue 2 months ago • 3 comments

Client Information

  • LLM Client: Claude Code / Antigravity / Cursor
  • Client Config: stdio configuration running npx @cloudflare/mcp-server-cloudflare run <account_id>
  • MCP Server: @cloudflare/mcp-server-cloudflare (v0.2.0, local stdio)
  • Prompt: N/A (process lifecycle bug)

Describe the bug When the host LLM client or CLI terminates, disconnects, or is closed, @cloudflare/mcp-server-cloudflare fails to detect that stdin reached EOF / closed.

On Linux, the orphaned process (reparented to PPID 1) stays alive indefinitely in an unhandled pipe polling state (Rl), consuming 100% of a CPU core per instance.

To Reproduce Steps to reproduce the behavior:

  1. Start the server via stdio: node dist/index.js run <account_id>
  2. Disconnect/kill the parent process, or close stdin: echo "" | node dist/index.js run <account_id> (while connected)
  3. Check running processes: ps -eo pid,ppid,%cpu,stat,comm | grep mcp-server-cloudflare
  4. The process remains alive with PPID 1 and status Rl at ~100% CPU.

Expected behavior The server process should exit cleanly with exit code 0 immediately when stdin closes or emits end.

Additional context In src/index.ts, the process handles uncaughtException and unhandledRejection, but registers no listeners for stdin termination. Because the bundled @modelcontextprotocol/sdk (v0.6.0) StdioServerTransport also does not close the process on EOF, libuv enters a busy-loop on the closed descriptor.

Fix by adding stream lifecycle listeners to src/index.ts:

process.stdin.on("end", () => process.exit(0));
process.stdin.on("close", () => process.exit(0));

FullThrottle83 avatar Sep 20 '26 00:09 FullThrottle83

Some triage that may save whoever picks this up a detour: the code in the report no longer exists on main.

  • src/index.ts is present at tag v0.2.0, alongside src/main.ts, src/init.ts and src/tools/ — that is the stdio server the report describes.
  • On main there is no src/ directory at all, and no reference to StdioServerTransport anywhere in the tree. The root package.json is private: true, and apps/* are remote MCP servers deployed as Workers rather than stdio processes.

So the suggested change:

process.stdin.on("end", () => process.exit(0));
process.stdin.on("close", () => process.exit(0));

has nowhere to go on main as written, even though the diagnosis looks right for the version being run (npx @cloudflare/mcp-server-cloudflare resolving to 0.2.0).

Could a maintainer confirm whether that npm package is still published from this repository, and if so from which branch or tag? If it is maintained, I'm happy to open a PR adding the stdin lifecycle handling there; if the stdio server is retired in favour of the remote servers, this issue is probably better closed with a note pointing users at the remote setup.

Shubham-Padkonde avatar Sep 20 '26 07:09 Shubham-Padkonde

yea that was wild

Adding a second trigger for the same symptom. The stdin fix proposed above doesn't cover it. I tested that below.

Verified on Windows 11, Node 22.15.0, @cloudflare/mcp-server-cloudflare 0.2.0 from npm, launched by Claude Code.

Root cause: the error handler re-triggers itself

At tag v0.2.0, src/index.ts has:

process.on('uncaughtException', (error) => {
  log('Uncaught exception:', error)
})

and src/utils/helpers.ts:

export function log(...args: any[]) {
  const msg = `[DEBUG ${new Date().toISOString()}] ${args.join(' ')}\n`
  process.stderr.write(msg)
}

If the client has already closed its end of stderr, process.stderr.write fails with EPIPE. Nothing listens for error on stderr, so that becomes a new uncaught exception, which calls log(), which fails again. The handler never exits, so the process loops forever.

I counted it: with stderr closed, the uncaughtException handler fired over 95,000 times in about 7 seconds, every time with EPIPE.

When it happens

It takes an exception that reaches this handler at a time when nobody is reading stderr. If stderr is still open, the first log() call succeeds and the process exits normally.

In my case the exception came from a config without a subcommand:

"args": ["-y", "@cloudflare/mcp-server-cloudflare"]

which throws Unknown command: undefined. Expected 'init' or 'run'. run without an account ID (Missing account ID) goes down the same path. I tested both.

What I saw: Claude Code reported CONNECT_TIMEOUT and CONNECTION_CLOSED for this server, and the leaked processes were node dist/index.js running under cmd.exe, which was started by npx. I didn't capture the exact timing, so this part is my interpretation: the client gives up before npx finishes launching the server. The server then starts with a stderr that nobody reads, throws on startup and loops.

This leaked one process per Claude Code session: 8 processes over almost 6 days, each using close to a full CPU core, around 3.2 GB RAM combined.

Reproduction

const { spawn } = require('child_process')
const child = spawn(process.execPath,
  ['node_modules/@cloudflare/mcp-server-cloudflare/dist/index.js'],
  { stdio: ['pipe', 'pipe', 'pipe'] })
child.stderr.destroy() // the client is gone
// a few seconds later: still alive, using about one CPU-second per second

With stderr left open, the same command exits right away with code 0. The closed stderr is what turns the error into a loop.

Results

Each case was checked a few seconds after start, with stderr closed:

Scenario Result
No subcommand keeps looping, ~1 CPU-s/s
run without account ID keeps looping, ~1 CPU-s/s
No subcommand + the stdin handlers proposed above, stdin closed by the client after 1 s keeps looping, ~1 CPU-s/s
No subcommand + the fix below exits, code 1

Suggested fix

Handle stderr errors, and exit from the handlers instead of only logging:

process.stderr.on('error', () => process.exit(1))

process.on('uncaughtException', (error) => {
  log('Uncaught exception:', error)
  process.exit(1)
})

process.on('unhandledRejection', (error) => {
  log('Unhandled rejection:', error)
  process.exit(1)
})

Together with the stdin handling proposed above, this should cover both cases: the client disconnecting while the server runs, and the server failing when the client is already gone.

Labontese avatar Sep 25 '26 07:09 Labontese