Skip to content

fix: clean up orphaned processes and LSP state on shutdown#20006

Open
nianyi778 wants to merge 2 commits intoanomalyco:devfrom
nianyi778:fix/orphan-process-cleanup
Open

fix: clean up orphaned processes and LSP state on shutdown#20006
nianyi778 wants to merge 2 commits intoanomalyco:devfrom
nianyi778:fix/orphan-process-cleanup

Conversation

@nianyi778
Copy link
Copy Markdown

@nianyi778 nianyi778 commented Mar 30, 2026

Issue for this PR

Closes #12767
Related to #18632

Type of change

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

What does this PR do?

This fixes two shutdown paths that still look leaky on current dev.

First, packages/opencode/src/index.ts does not register process-level handlers for SIGTERM, SIGHUP, or SIGPIPE. The TUI has its own SIGHUP handling, but commands like opencode run, opencode acp, and opencode serve do not. This PR adds a small signal.ts helper that calls Instance.disposeAll() and then exits, with a 5 second timeout in case cleanup hangs.

Second, LSP shutdown was stopping the child process but keeping in-memory state alive. This clears the LSP diagnostics map, file version map, and the top-level LSP client caches during shutdown.

I also clear the prompt msgs array right after processor.process(...) returns, since that array can get large and is not needed for the rest of the loop iteration.

I did not copy over older PR changes around plugin subscriptions or processor cleanup because current dev already uses scoped Effect subscriptions there, and SessionProcessor already runs cleanup through Effect.ensuring(cleanup()).

How did you verify your code works?

I tested the signal path locally with the ACP command, since it is a long-running non-TUI entrypoint:

  • started bun run ./src/index.ts acp
  • sent SIGHUP
  • confirmed the process exited
  • started it again
  • sent SIGTERM
  • confirmed the process exited

I also tried to run bun typecheck in packages/opencode, but this checkout currently does not have tsgo installed in node_modules/.bin, so the script exits before typechecking starts.

Screenshots / recordings

N/A

Checklist

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

@github-actions
Copy link
Copy Markdown
Contributor

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 github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Mar 30, 2026
@github-actions
Copy link
Copy Markdown
Contributor

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.

[BUG] Opencode process survives terminal close — missing SIGHUP handler causes orphaned process accumulation

1 participant