Skip to content

opencode run: MCP child processes not terminated on exit (orphan leak) #15808

@the3asic

Description

@the3asic

Summary

opencode run spawns MCP server processes (e.g., mcp-searxng, mcp-tavily, sequential-thinking, etc.) but does not terminate them when the run session exits. These processes get reparented to PID 1 and persist indefinitely, causing significant memory leaks over time.

Reproduction

  1. Configure MCP servers in opencode.json (e.g., mcp-searxng)
  2. Run opencode run "any prompt" — MCP servers are spawned as child processes
  3. After opencode run completes and exits, check: ps -eo pid,ppid,args | grep mcp-searxng
  4. The MCP processes are still alive with PPID=1 (orphaned)

Impact

In a cron-based setup (e.g., heartbeat scripts calling opencode run every 30 minutes), orphaned MCP processes accumulate rapidly:

  • Each mcp-searxng process consumes ~61MB RSS
  • ~90 opencode run invocations/day = ~5.5GB/day leaked
  • Observed: 301 orphaned mcp-searxng processes consuming ~18GB before manual cleanup

Environment

  • OpenCode version: 1.3.0
  • OS: Ubuntu 20.04 (ARM64, Oracle Cloud)
  • MCP servers configured: searxng, tavily, sequential-thinking, zhipu-vision, github-search, websearch

Expected Behavior

opencode run should send SIGTERM to all spawned MCP child processes before exiting (or use a process group so they all terminate together).

Current Workaround

Cron script that periodically kills orphaned MCP processes (PPID=1):

#!/usr/bin/env bash
MCP_PATTERNS="mcp-searxng|mcp-tavily|sequential-thinking|github-search|zhipu-vision|websearch"
while IFS= read -r line; do
    pid=$(echo "$line" | awk '{print $1}')
    ppid=$(echo "$line" | awk '{print $2}')
    [ "$ppid" = "1" ] && kill "$pid" 2>/dev/null
done < <(ps -eo pid,ppid,args | grep -E "$MCP_PATTERNS" | grep -v grep)

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type
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