Skip to content

MCP child processes orphaned on restart — memory leak #1892

Description

@Cipher208

Description

When MiMoCode restarts (crash, manual restart, or session end), child MCP processes are NOT terminated. They get reparented to PID 1 (systemd/init) and persist as orphans, consuming memory indefinitely.

Each restart leaks 2 processes (~81MB RAM). After multiple restarts in a session:

  • 22 orphaned shell+MCP pairs = 1.7GB RAM wasted
  • 47 total MCP processes visible (only 2 legitimate)

Reproduction

  1. Configure MCP servers in mimocode.json (e.g., ariel-memory with Python stdio)
  2. Start MiMoCode
  3. Restart MiMoCode 10+ times (e.g., during plugin/skill development)
  4. Check: ps aux | grep mcp_server | wc -l → shows 20+ processes
  5. Check orphans: ps -eo pid,ppid,comm | grep 'python3.*mcp_server' | awk '$2==1' → lists orphans

Root Cause Analysis

MiMoCode spawns MCP servers via shell wrapper:

sh -c 'cd /path && python3 -m mcp_server.server --transport stdio'

Process chain: MiMoCode (Node.js) → sh (shell wrapper) → python3 (MCP server)

When MiMoCode exits/restarts:

  1. Node.js process dies
  2. Shell wrapper (sh) reparented to PID 1 (systemd)
  3. Python MCP server also reparented to PID 1
  4. New MiMoCode instance creates new MCP processes
  5. Old processes remain as orphans indefinitely

Each orphan pair costs ~81MB RAM (1MB shell + 80MB Python MCP server).

Expected Behavior

MCP child processes should be terminated when MiMoCode exits or restarts. Options:

  • Use process group killing (setsid + kill -- -PGID)
  • Track child PIDs and kill them in shutdown handler
  • Use prctl(PR_SET_PDEATHSIG, SIGTERM) in the MCP launcher so children die when parent dies

Actual Behavior

MCP child processes become orphans (PPID=1) and consume ~80MB each until manually killed or system reboot.

Environment

  • MiMoCode version: v0.38.9
  • OS: Debian 12 (Linux)
  • Node.js: system Node
  • MCP config: ariel-memory (Python, stdio), context-mode (Node, stdio)
  • Install method: npm global (npm install -g mimocode)

Workaround

Cron job to clean up orphaned MCP processes every 15 minutes:

#!/bin/bash
# Kill mcp_server processes orphaned to PID 1
ps -eo pid,ppid,comm | grep 'python3.*mcp_server' | awk '$2==1 {system("kill -9 " $1)}'
ps -eo pid,ppid,args | grep 'sh.*-c.*mcp_server' | awk '$2==1 {system("kill -9 " $1)}'

Impact

On a development VPS with frequent MiMoCode restarts (plugin development, debugging), this leaks ~1.7GB RAM per session. Without the workaround, the system runs out of memory within hours.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions