Skip to content

Run a plugin's dispose hooks when its load fails after the factory ran - #8029

Merged
ymichael merged 3 commits into
get-bb:mainfrom
hemaaanth:fix/failed-plugin-load-dispose-hooks
Nov 23, 2026
Merged

ymichael merged 3 commits into
get-bb:mainfrom
hemaaanth:fix/failed-plugin-load-dispose-hooks

Conversation

@hemaaanth

@hemaaanth hemaaanth commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

When a plugin load fails after its server factory ran, discardCandidate in apps/server/src/services/plugins/plugin-runtime.ts throws the new instance away. It aborts stopping, interrupts the instance's interactions, closes its databases, aborts released and invalidates the handle. It never ran handle.disposeHooks; only disposePluginInstance did. So bb.onDispose cleanup that the factory registered never ran. Timers and bb.sdk.subscribe callbacks the factory started kept running against the invalidated bb. Each call threw PluginContextStaleError until the server exited. This happens on a first load whose host entry fails to build, and on a reload whose factory throws. #6464 rewrote discardCandidate but did not add the hooks. The reproduction is in #8025.

What changed

  • plugin-runtime.ts: the LIFO dispose-hook loop moves out of disposePluginInstance into runDisposeHooks. discardCandidate now calls it after the stopping abort and the interaction interrupt, and before databases close and the handle is invalidated. That is the order disposePluginInstance already 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.
  • A failed reload does not touch the previous instance. Its hooks still run only when it is disposed.
  • New test 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 or PluginContextStaleError follows service.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.
  • Docs: the "bb.onDispose and the reload lifecycle" section of plugins/bb-guide/skills/bb-plugin-authoring/references/backend-ui-lifecycle.md now says that hooks also run when a load fails after the factory ran. The onDispose declaration comment in the Plugin SDK is unchanged, so this PR does not need an SDK version bump.
  • Deviation from the issue: Plugin load that fails after its server factory runs never runs bb.onDispose, so timers and subscriptions leak #8025 also suggested that the uncaughtExceptionMonitor in installSafeProcessDiagnostics skip or rate-limit reports for errors the server handler claims. This PR does not change that. With this fix, a plugin that cleans up in onDispose no longer causes the report flood. A plugin that leaks a timer without onDispose still can. That can be a separate change.

How you verified

  • With main's plugin-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.
  • Live check on a pnpm start:worktree server: a path plugin with a 50 ms timer and an unresolvable host import logged its dispose hook 1 ms before failed 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-sdk tests pass too.
  • Full server suite, pnpm exec turbo run test --filter=@bb/server -- --maxWorkers=2: 4428 passed, 3 skipped, 1 failed. The failure was a 15 s timeout in test/ai/commit-message.test.ts. That file passed on rerun together with all of test/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 --check on the changed files and git diff --check: clean.
  • Checks ran on Linux x86-64, Node.js v26.12.0.

Fixes #8025

🤖 Generated with Claude Code

AGENT GENERATED

hemaaanth and others added 2 commits November 22, 2026 13:53
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]>
@ymichael
ymichael merged commit 700d405 into get-bb:main Nov 23, 2026
46 checks passed
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.

Plugin load that fails after its server factory runs never runs bb.onDispose, so timers and subscriptions leak

2 participants