Skip to content

Bug: MCP stdio server processes orphaned on session end — 2.4GB RAM leak from context7-mcp #69465

Description

@Mertolgahan

Summary

MCP stdio servers using npx are spawned per-session but never cleaned up when sessions end. Over time, orphaned processes accumulate and consume significant RAM.

Observed Behavior

On a 30GB RAM server, 41 orphaned context7-mcp processes were found consuming ~2.4GB of RAM total (~60-100MB each). Each subagent/session spawn creates a new npm exec @upstash/context7-mcp process chain that persists after the parent session ends.

Timeline of accumulation:

  • 19:30 → 6 processes
  • 19:39 → +3 (9 total)
  • 20:35 → +3 (18 total)
  • 23:32 → +9 (33 total, burst from parallel subagent spawns)
  • 23:49 → +3 (42 total)

Root Cause

  1. MCP stdio protocol requires each client to spawn its own server process
  2. OpenClaw gateway spawns a new MCP server per session/subagent invocation
  3. When the session/subagent ends, the MCP server process is not terminated (no SIGTERM/SIGKILL sent)
  4. npx exacerbates this by spawning a parent npm process + child node process (double the overhead)

Workaround

Installing the MCP server globally (npm install -g @upstash/context7-mcp) and changing the config from:

"context7": {
  "command": "npx",
  "args": ["-y", "@upstash/context7-mcp", "--api-key", "..."] 
}

to:

"context7": {
  "command": "context7-mcp",
  "args": ["--api-key", "..."] 
}

This reduces per-process overhead (~40MB vs ~100MB) but does not fix the orphaning issue. Processes still accumulate; they just use less RAM each.

Expected Behavior

The gateway should track child MCP server PIDs and send SIGTERM when the parent session/subagent exits, regardless of exit reason (completion, error, timeout, cancellation).

Environment

  • OpenClaw version: 2026.4.14
  • Host OS: Linux 6.8.0-110-generic (x64)
  • Host: Hetzner VPS, 32GB RAM
  • Session type: Telegram direct chat + subagent spawns

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions