Skip to content

MCP server processes not cleaned up on exit (orphan processes) + eagerly starts all servers #30123

Description

@chenyuan35

Bug Description

When OpenCode exits, MCP server child processes are not killed, leaving orphan processes running indefinitely. Additionally, OpenCode eagerly starts ALL configured stdio-type MCP servers at launch regardless of whether they will be used.

Impact

  • 30 orphan node.exe processes accumulated (from just 2 sessions), each consuming 45-75MB RAM
  • Total wasted memory: ~1.5GB
  • CPU fan noise due to unnecessary load

Root Cause

  1. Process tree: OpenCode -> cmd /c npx -y -> node(npx-cli.js) -> node(MCP server)
  2. When OpenCode exits, it does not cascade SIGTERM/kill to child processes
  3. The cmd /c wrapper exits early (by design of /c), making npx and MCP server orphans
  4. Windows does not auto-clean orphan processes — they run forever waiting on closed stdin

All MCP servers are started eagerly

Even if a session never uses avily, exa, memory, sequential-thinking, or context7, all of them are launched at startup. This wastes CPU and memory on every session.

Expected Behavior

  1. On exit: OpenCode should kill all spawned child processes (the entire process tree)
  2. On startup: MCP servers should be lazily started only when first needed (or at least have a config option for this)
  3. Alternatively: MCP servers should detect stdin EOF and exit themselves

Environment

  • Windows (Win32)
  • OpenCode (latest)
  • MCP servers launched via: \cmd /c npx -y @modelcontextprotocol/server-sequential-thinking\ (and similar)

Workaround

Users can manually kill orphan processes:
\\powershell
taskkill /f /im node.exe
\\

Or disable unused MCP servers in opencode.json to prevent them from starting.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions