Skip to content

[BUG]: opencode attach processes not terminated when tmux pane is killed (zombie process accumulation) #11225

Description

@raki-1203

Problem Description

When using opencode --port 4096 (server mode) with tmux-based subagent visualization, opencode attach processes accumulate and are not properly cleaned up when their tmux panes are closed/killed.

Evidence

  Pid: Program:        Command:                                                                              Threads: User:       MemB       Cpu%
93351 opencode         opencode --port 4096                                                                  23       raki-1203   1.4G       3.9
 6228 opencode         opencode attach http://127.0.0.1:4096/ --session ses_3f37d334affePb4V7d8AFrWQLA       11       raki-1203   518M       1.8
 2068 opencode         opencode attach http://127.0.0.1:4096/ --session ses_3f384731fffeY2Y4xwWoF8tfYi       11       raki-1203   471M       1.8
 2063 opencode         opencode attach http://127.0.0.1:4096/ --session ses_3f3847c7affe1uu6zJosot0t17       7        raki-1203   491M       1.7
 3393 opencode         opencode attach http://127.0.0.1:4096/ --session ses_3f37fed31ffeMHtS0K9IZZIk4X       7        raki-1203   535M       1.7
 6399 opencode         opencode attach http://127.0.0.1:4096/ --session ses_3f37cdd7bffeVZS3hyof474y73       11       raki-1203   524M       1.8
 2046 opencode         opencode attach http://127.0.0.1:4096/ --session ses_3f3849decffesz37To7b736MlR       7        raki-1203   537M       1.8

These sessions have completed, their tmux panes have been killed, but the opencode attach processes remain running.

Steps to Reproduce

  1. Start opencode in server mode: opencode --port 4096
  2. Run tasks that spawn subagent sessions (e.g., using delegate_task or background agents)
  3. Subagent sessions create tmux split panes via tmux split-window "opencode attach http://127.0.0.1:4096/ --session ses_xxx"
  4. When subagent work completes, tmux pane is killed via tmux kill-pane -t %paneId
  5. The opencode attach process continues running as orphan

Expected Behavior

When the tmux pane running opencode attach is killed (receives SIGHUP), the attach process should:

  1. Detect terminal/pane closure
  2. Clean up resources
  3. Exit gracefully

Current Behavior

  • opencode attach processes ignore SIGHUP or don't properly handle terminal closure
  • Processes continue running indefinitely consuming memory (~500MB each)
  • CPU usage accumulates causing MacBook to overheat
  • Only manual kill or system restart clears these processes

Technical Analysis

The issue appears to be in how opencode attach handles signal:

  1. SIGHUP not handled: When tmux kill-pane closes the pane, SIGHUP is sent to the process. The attach command doesn't seem to handle this signal properly.

  2. Server connection kept alive: Even after the parent session calls session.abort(), the attach process maintains its server connection and keeps running.

  3. No stdin/tty detection: The process doesn't detect when its stdin/tty becomes invalid.

Proposed Solutions

Option 1: Handle SIGHUP signal (Recommended)

signal.Notify(sigs, syscall.SIGHUP)
// On SIGHUP, exit gracefully

Option 2: Detect stdin/tty closure

When stdin is closed or tty becomes invalid, exit the attach process.

Option 3: Periodic server connection check

If the session is aborted on server side, the attach client should detect this and exit.

Related Issues

Environment

  • macOS (Apple Silicon)
  • opencode v1.1.43
  • Running in server mode with --port 4096
  • Using tmux for subagent visualization (via oh-my-opencode plugin)

Impact

  • Memory leak: ~500MB per orphaned attach process
  • CPU usage accumulation
  • MacBook overheating during extended use
  • Requires manual process cleanup (pkill -f "opencode attach")

Workaround

Currently using this workaround before the tmux pane is closed:

# Get PID of the process in the pane, then kill it
tmux display -p -t %paneId '#{pane_pid}' | xargs kill

But this is fragile and doesn't address the root cause.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

perfIndicates a performance issue or need for optimization

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions