Skip to content

fix(sdk): ensure opencode server closes cleanly via process groups#21626

Open
sigmaSd wants to merge 2 commits intoanomalyco:devfrom
sigmaSd:patch-1
Open

fix(sdk): ensure opencode server closes cleanly via process groups#21626
sigmaSd wants to merge 2 commits intoanomalyco:devfrom
sigmaSd:patch-1

Conversation

@sigmaSd
Copy link
Copy Markdown

@sigmaSd sigmaSd commented Apr 9, 2026

Issue for this PR

Closes #21628

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

This PR fixes an issue where calling server.close() would only terminate the immediate process wrapper, leaving the underlying opencode server orphaned and the network port occupied.
I updated the SDK to spawn the process in a new process group (detached: true). This change uses a negative PID (-proc.pid) on POSIX systems to signal the entire process group. In windows this seems already handled via taskkill /T /F .

The use case I have opencode installed with deno, when I use the close api it kills the deno wrapper but not the actual opencode binary, this fixes it.
If you paste a large clearly AI generated description here your PR may be IGNORED or CLOSED!

How did you verify your code works?

With this change close kills deno wraooer and opencode

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 9, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

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

Based on my search, here are related PRs that address similar process cleanup issues:

Potentially Related PRs:

  1. fix(core): clean up idle instances and stale subprocess state #21573 - fix(core): clean up idle instances and stale subprocess state

    • Related to subprocess cleanup and state management
  2. fix: force kill MCP server processes on dispose to prevent orphan processes #7424 - fix: force kill MCP server processes on dispose to prevent orphan processes

    • Directly addresses orphaned process prevention, similar cleanup intent
  3. fix: add signal handlers to prevent orphaned processes #12877 - fix: add signal handlers to prevent orphaned processes

    • Handles orphaned process prevention through signal handlers
  4. fix(cli): handle SIGHUP to prevent orphaned processes on terminal close #12718 - fix(cli): handle SIGHUP to prevent orphaned processes on terminal close

    • Related to orphaned process prevention in similar contexts
  5. fix(opencode): prevent resource leaks in serve/web shutdown and SSE stream cleanup #14092 - fix(opencode): prevent resource leaks in serve/web shutdown and SSE stream cleanup

    • Addresses shutdown and cleanup issues in opencode server

These PRs all address related concerns around process cleanup, orphaned processes, and server shutdown—though none appear to be exact duplicates of PR #21626. Your PR takes a specific approach using process groups and detached spawning for the SDK's opencode server management.

Add check for process PID before killing process.
@github-actions github-actions bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Apr 9, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

sdk: server.close() does not terminate underlying opencode process when run via wrappers (e.g., Deno)

1 participant