Open
Conversation
Adds a `shutdown` hook to the Hooks interface that is called during Instance dispose. Plugins can use this to gracefully clean up background processes (e.g. stop a Dolt SQL server) before OpenCode exits. The hook is invoked via the dispose callback registered on Plugin.state(), which is triggered by Instance.disposeAll() on the existing TUI and bootstrap shutdown paths. Closes anomalyco#10524 Generated-by: github-copilot/claude-sonnet-4.6
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Author
|
Opus written fix, working on local testing and review still, will update when I have. OK tested locally (TUI only), works with my beads shutdown plugin. Added docs |
Adds two tests for the shutdown hook: - Verifies shutdown is called during Instance.dispose() - Verifies a throwing shutdown hook does not prevent others from running The second test caught a real bug: the dispose callback had no per-hook error handling, so one failing plugin would skip all subsequent shutdown hooks. Fixed by adding .catch() around each hook invocation. Generated-by: github-copilot/claude-opus-4.6
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Add shutdown hook to the Events reference list and add an Examples section showing how to use it for background process cleanup. Generated-by: claude-opus-4.6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #10524
Related: #10003
Type of change
What does this PR do?
Plugins currently have no way to run cleanup when OpenCode exits. If a plugin manages a long-lived child process (like a database server), Ctrl+C kills it mid-write and corrupts data.
This adds a
"shutdown"hook to theHooksinterface. The hook is called duringInstance.disposeAll()— the existing shutdown path that both the TUI (worker.ts) andbootstrap.tsalready invoke on exit. The implementation registers adisposecallback on theInstance.state()call inPlugin, which iterates the loaded hooks and calls each plugin'sshutdown()sequentially. Each hook invocation is wrapped in.catch()so one failing plugin doesn't prevent others from cleaning up.I also added
"shutdown"to theExcludelist on thetrigger()type parameter sinceshutdowntakes no input/output args and shouldn't be callable viatrigger().How did you verify your code works?
test/plugin/shutdown.test.ts:shutdownhook is called whenInstance.dispose()runs (writes a marker file)shutdownhook does not prevent subsequent hooks from running (the second test actually caught a bug — the original implementation had no per-hook error handling)bun test test/plugin/— 14 existing + 2 new)turbo typecheck)Screenshots / recordings
N/A — no UI change.
Checklist