[PR #11603] fix: prevent orphaned worker process when killed #13863

Open
opened 2026-02-16 18:18:41 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/11603

State: open
Merged: No


Summary

Fixes #11527

When OpenCode is spawned programmatically and then killed, the worker process was left orphaned (PPID becomes 1), blocking the parent process from exiting.

Changes

  • thread.ts: Add SIGTERM/SIGINT handlers to properly shutdown worker before exit
  • worker.ts: Add parent death detection using process.ppid polling (1-second interval)
  • worker.ts: Add SIGTERM/SIGINT handlers for direct signal handling

How it works

  1. Signal handlers in main thread: When SIGTERM or SIGINT is received, the main thread now calls the worker's shutdown RPC before exiting
  2. Parent death detection: Worker polls every 1 second to check if parent is still alive using process.kill(parentPid, 0). If parent dies (even via SIGKILL), worker detects and shuts down
  3. Worker signal handlers: Direct signals to worker are also handled properly

Test plan

  • All 842 existing tests pass
  • Manual verification with reproduction script from issue

🤖 Generated with Claude Code

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/11603 **State:** open **Merged:** No --- ## Summary Fixes #11527 When OpenCode is spawned programmatically and then killed, the worker process was left orphaned (PPID becomes 1), blocking the parent process from exiting. ### Changes - **thread.ts**: Add SIGTERM/SIGINT handlers to properly shutdown worker before exit - **worker.ts**: Add parent death detection using `process.ppid` polling (1-second interval) - **worker.ts**: Add SIGTERM/SIGINT handlers for direct signal handling ### How it works 1. **Signal handlers in main thread**: When SIGTERM or SIGINT is received, the main thread now calls the worker's shutdown RPC before exiting 2. **Parent death detection**: Worker polls every 1 second to check if parent is still alive using `process.kill(parentPid, 0)`. If parent dies (even via SIGKILL), worker detects and shuts down 3. **Worker signal handlers**: Direct signals to worker are also handled properly ## Test plan - [x] All 842 existing tests pass - [x] Manual verification with reproduction script from issue 🤖 Generated with [Claude Code](https://claude.ai/claude-code)
yindo added the pull-request label 2026-02-16 18:18:41 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13863