Repository navigation
Run a plugin's dispose hooks when its load fails after the factory ran - #8029
Merged
ymichael merged 3 commits intoNov 23, 2026
Merged
Conversation
discardCandidate now runs the candidate's bb.onDispose hooks in LIFO order before closing its databases and invalidating its bb, so timers and subscriptions a failed load started no longer outlive it. Fixes get-bb#8025 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The onDispose comment edit changed the published SDK types, which needs an SDK version bump. The plugin authoring guide already describes the failed-load behavior, so drop the comment edit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The fixture now starts a real timer and registers sync, throwing, and async dispose hooks. Each case asserts LIFO order, that a throwing hook does not stop later ones, and that no tick or stale-handle error follows service.stop(). Adds the first-load factory-throw case and checks that a failed reload leaves the previous instance serving. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Human comments
What was wrong
When a plugin load fails after its server factory ran,
discardCandidateinapps/server/src/services/plugins/plugin-runtime.tsthrows the new instance away. It abortsstopping, interrupts the instance's interactions, closes its databases, abortsreleasedand invalidates the handle. It never ranhandle.disposeHooks; onlydisposePluginInstancedid. Sobb.onDisposecleanup that the factory registered never ran. Timers andbb.sdk.subscribecallbacks the factory started kept running against the invalidatedbb. Each call threwPluginContextStaleErroruntil the server exited. This happens on a first load whose host entry fails to build, and on a reload whose factory throws. #6464 rewrotediscardCandidatebut did not add the hooks. The reproduction is in #8025.What changed
plugin-runtime.ts: the LIFO dispose-hook loop moves out ofdisposePluginInstanceintorunDisposeHooks.discardCandidatenow calls it after thestoppingabort and the interaction interrupt, and before databases close and the handle is invalidated. That is the orderdisposePluginInstancealready uses. A failing hook is still logged and later hooks still run. All three discard paths get this: the factory throws, the host entry has a problem, and services are still hung after the previous instance's teardown.apps/server/test/services/plugins/plugin-failed-load-dispose.test.ts. The fixture starts a real 10 ms timer and registers a sync, a throwing, and an async dispose hook. It covers a first load whose host entry fails to build, a first load whose factory throws, and a reload whose factory throws. Each case asserts the hooks ran in LIFO order, that the throwing hook did not stop later ones, and that no timer tick orPluginContextStaleErrorfollowsservice.stop(). The reload case also checks that the previous instance keeps serving its CLI command and timer, and that its hooks run only at its own disposal.plugins/bb-guide/skills/bb-plugin-authoring/references/backend-ui-lifecycle.mdnow says that hooks also run when a load fails after the factory ran. TheonDisposedeclaration comment in the Plugin SDK is unchanged, so this PR does not need an SDK version bump.uncaughtExceptionMonitorininstallSafeProcessDiagnosticsskip or rate-limit reports for errors the server handler claims. This PR does not change that. With this fix, a plugin that cleans up inonDisposeno longer causes the report flood. A plugin that leaks a timer withoutonDisposestill can. That can be a separate change.How you verified
main'splugin-runtime.ts, all three cases fail (expected [] to deeply equal [ '1:async', '1:throw', '1:timer' ], and the same for generation 2 on reload). All three pass with the change.pnpm exec turbo run test --filter=@bb/server --force -- test/services/plugins/plugin-failed-load-dispose.test.ts test/services/plugins/plugin-instance-lifecycle.test.ts: 16 passed, three runs in a row.pnpm start:worktreeserver: a path plugin with a 50 ms timer and an unresolvable host import logged its dispose hook 1 ms beforefailed to load, and no stale-handle errors or crash reports followed.pnpm exec turbo run typecheck lint --filter=@bb/server --filter=@get-bb/plugin-sdk: 8 tasks passed. The@get-bb/plugin-sdktests pass too.pnpm exec turbo run test --filter=@bb/server -- --maxWorkers=2: 4428 passed, 3 skipped, 1 failed. The failure was a 15 s timeout intest/ai/commit-message.test.ts. That file passed on rerun together with all oftest/services/plugins/(64 files, 632 tests). This machine caps each command at 4 GiB. The kernel killed one Vitest worker for memory during the full run, so Vitest also reported one "Worker exited unexpectedly" error. Every test file still reported a result. With default workers the whole run was killed, which is why these commands pass--maxWorkers.pnpm exec oxfmt --checkon the changed files andgit diff --check: clean.Fixes #8025
🤖 Generated with Claude Code