[PR #9866] fix(serve): add signal handlers for graceful shutdown on Windows #13252

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

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

State: open
Merged: No


Summary

Fixes #9859 - Adds signal handlers for graceful shutdown in opencode serve to prevent ghost port bindings on Windows when the process is terminated by external tools like openralph/opencode-ralph.

Changes

  • packages/opencode/src/cli/cmd/serve.ts: Added signal handlers for SIGINT, SIGTERM, and SIGBREAK (Windows-only) that call server.stop() before exiting
  • packages/opencode/src/server/server.ts: Added reusePort: true to Bun.serve() for defense-in-depth, allowing immediate port reuse even after abnormal termination

Root Cause

The opencode serve command lacked signal handlers to catch termination signals. When plugins like openralph send SIGTERM to terminate the server, the process would die immediately without calling server.stop(), causing:

  1. TCP listener not closed properly
  2. Port binding persists as a "ghost" on Windows
  3. Server restart fails until port times out (2-5 minutes) or system reboot

Solution

  1. Signal handlers: Register handlers for SIGINT, SIGTERM, and SIGBREAK (Windows Ctrl+Break) that gracefully shut down the server
  2. reusePort: Enable SO_REUSEPORT on the server socket, allowing immediate port reuse as a fallback for edge cases
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/9866 **State:** open **Merged:** No --- ## Summary Fixes #9859 - Adds signal handlers for graceful shutdown in `opencode serve` to prevent ghost port bindings on Windows when the process is terminated by external tools like openralph/opencode-ralph. ## Changes - **`packages/opencode/src/cli/cmd/serve.ts`**: Added signal handlers for `SIGINT`, `SIGTERM`, and `SIGBREAK` (Windows-only) that call `server.stop()` before exiting - **`packages/opencode/src/server/server.ts`**: Added `reusePort: true` to `Bun.serve()` for defense-in-depth, allowing immediate port reuse even after abnormal termination ## Root Cause The `opencode serve` command lacked signal handlers to catch termination signals. When plugins like openralph send SIGTERM to terminate the server, the process would die immediately without calling `server.stop()`, causing: 1. TCP listener not closed properly 2. Port binding persists as a "ghost" on Windows 3. Server restart fails until port times out (2-5 minutes) or system reboot ## Solution 1. **Signal handlers**: Register handlers for SIGINT, SIGTERM, and SIGBREAK (Windows Ctrl+Break) that gracefully shut down the server 2. **reusePort**: Enable `SO_REUSEPORT` on the server socket, allowing immediate port reuse as a fallback for edge cases
yindo added the pull-request label 2026-02-16 18:18:07 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13252