Skip to content

fix(process): prevent orphaned opencode subprocesses on shutdown#15924

Merged
thdxr merged 8 commits intodevfrom
fix/tui-thread-exit-cleanup
Mar 4, 2026
Merged

fix(process): prevent orphaned opencode subprocesses on shutdown#15924
thdxr merged 8 commits intodevfrom
fix/tui-thread-exit-cleanup

Conversation

@thdxr
Copy link
Contributor

@thdxr thdxr commented Mar 4, 2026

Summary

  • make TUI thread shutdown idempotent and timeout-bounded so worker teardown cannot block process exit
  • always terminate the TUI worker and unregister process listeners during cleanup
  • harden MCP local client teardown (including BUN_BE_BUN opencode subprocesses) with timeout-bounded close and process-tree termination fallback

Root Cause

  • some shutdown paths relied on RPC/client close calls that could hang indefinitely when a worker/subprocess became unhealthy
  • when MCP local startup or reconnect failed, child processes could outlive the parent connection lifecycle

Changes

  • packages/opencode/src/cli/cmd/tui/thread.ts
    • centralize shutdown in an idempotent stop() function
    • enforce bounded shutdown with timeout and guaranteed worker.terminate()
    • simplify flow using helper functions for worker path and prompt text
  • packages/opencode/src/mcp/index.ts
    • add closeClient() helper that:
      • attempts graceful close with timeout
      • signals descendant processes
      • escalates to stronger termination if needed
    • use closeClient() consistently on dispose, reconnect/replace, disconnect, and failed startup/tool-list paths

Testing

  • bun run typecheck
  • bun test test/cli/tui/transcript.test.ts
  • bun test test/mcp/headers.test.ts test/mcp/oauth-browser.test.ts

@thdxr thdxr changed the title fix(tui): avoid hanging processes on thread exit fix(process): prevent orphaned opencode subprocesses on shutdown Mar 4, 2026
thdxr added 2 commits March 3, 2026 21:37
Previously, MCP servers like chrome-devtools-mcp that spawn grandchild
processes (e.g. Chrome browser instances) would leave those processes
orphaned when the connection closed. Now all descendant processes are
properly terminated, preventing resource leaks and zombie processes.
The worker was racing instance disposal against a 5s timeout, which
could leave active sessions in an inconsistent state. Now waits 10s
to ensure graceful cleanup completes before shutting down.
@thdxr thdxr enabled auto-merge (squash) March 4, 2026 02:44
@thdxr thdxr disabled auto-merge March 4, 2026 02:50
opencode-agent bot added a commit that referenced this pull request Mar 4, 2026
@thdxr thdxr merged commit 3ebebe0 into dev Mar 4, 2026
9 checks passed
@thdxr thdxr deleted the fix/tui-thread-exit-cleanup branch March 4, 2026 03:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant