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

Open
opened 2026-02-16 18:08:52 -05:00 by yindo · 2 comments
Owner

Originally created by @raki-1203 on GitHub (Jan 29, 2026).

Originally assigned to: @rekram1-node on GitHub.

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

  • #10563 - Memory Leak: Orphaned processes when terminal closes without explicit exit
  • #6633 - MCP processes not terminated after session ends
  • #7261 - v1.1.6 still has memory bloat + MCP orphan processes

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.

Originally created by @raki-1203 on GitHub (Jan 29, 2026). Originally assigned to: @rekram1-node on GitHub. ## 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) ```go 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 - #10563 - Memory Leak: Orphaned processes when terminal closes without explicit exit - #6633 - MCP processes not terminated after session ends - #7261 - v1.1.6 still has memory bloat + MCP orphan processes ## 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: ```bash # 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.
yindo added the perf label 2026-02-16 18:08:52 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 29, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #10563: Memory Leak: Orphaned processes when terminal closes without explicit exit
  • #6633: MCP processes not terminated after session ends
  • #7261: v1.1.6 still has memory bloat + MCP orphan processes

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 29, 2026): This issue might be a duplicate of existing issues. Please check: - #10563: Memory Leak: Orphaned processes when terminal closes without explicit exit - #6633: MCP processes not terminated after session ends - #7261: v1.1.6 still has memory bloat + MCP orphan processes Feel free to ignore if none of these address your specific case.
Author
Owner

@rekram1-node commented on GitHub (Jan 29, 2026):

If you are using oh my opencode please close here and reopen on their repo, some of what u are describing is not vanilla opencode

@rekram1-node commented on GitHub (Jan 29, 2026): If you are using oh my opencode please close here and reopen on their repo, some of what u are describing is not vanilla opencode
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#7992