Skip to content

fix(serve): add signal handlers for graceful shutdown on Windows#9866

Closed
JosXa wants to merge 1 commit into
anomalyco:devfrom
JosXa:opencode-agent/issue-9859
Closed

fix(serve): add signal handlers for graceful shutdown on Windows#9866
JosXa wants to merge 1 commit into
anomalyco:devfrom
JosXa:opencode-agent/issue-9859

Conversation

@JosXa
Copy link
Copy Markdown
Contributor

@JosXa JosXa commented Jan 21, 2026

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

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

@rekram1-node
Copy link
Copy Markdown
Collaborator

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: Ghost port bindings when server killed via SIGTERM (affects plugins like openralph)

2 participants