Repository navigation
Commit 2ff8598
Route plugin service uncaught exceptions to the service supervisor (#2128)
## What was wrong
Plugins run inside the server process. A plugin background service is
supervised only through the promise its `start(signal)` returns
(`runService` → `onServiceSettled` in
`apps/server/src/services/plugins/plugin-runtime.ts`). An error the
service raises outside that promise — an unlistened `EventEmitter`
`'error'` (ImapFlow socket timeout), a throw in a timer callback, a
detached rejection — becomes a process-level uncaught exception. The
server only registered an observe-only `uncaughtExceptionMonitor`, so
Node exited, the process manager restarted the server, the plugin
reloaded, and the crash looped; the service reported `running` the whole
time and every restart replaced the host daemon session ("Thread
interrupted because the host daemon disconnected").
Issue: #1746. Report: https://get-bb.github.io/reports/issues/1746.html
## What changed
- `apps/server/src/services/plugins/plugin-runtime.ts`: `runService`
runs each service instance inside an `AsyncLocalStorage` scope that
carries `{ pluginId, service, controller }`. The store follows every
timer, socket callback, and promise the service creates. New
`handleUncaughtException(error)` reads that store: no store → returns
`false`; a stale run (already restarted or disposed) → logs with plugin
and service name, returns `true`; the live run → aborts its controller
and records the error so the run's settlement takes the existing crash
path (`onServiceSettled` → backoff → restart). If the run ignores the
abort for `serviceStopTimeoutMs`, the plugin is marked `degraded` like
`stopServices` does.
- `apps/server/src/services/plugins/plugin-service.ts`: exposes
`handleUncaughtException` on `PluginService`.
- `apps/server/src/start-server.ts`: installs
`process.on("uncaughtException")`. A claimed error restarts that one
service. Anything unattributed keeps Node's default: stack to stderr,
exit 1 (the diagnostics monitor in `index.ts` has already written its
report).
- `packages/plugin-sdk/src/backend-contract.ts`: one sentence in the
`bb.background.service` doc comment stating that out-of-band errors
count as a crash.
No wire change. No CLI change. Deviation from the report's proposal: no
attribution field in the process-utils dump and no per-plugin crash
counter / auto-disable; the attributed error is logged with plugin and
service name and recovery reuses the existing backoff, which already
escalates to a 60 s cap. The scope covers services only (factory,
schedule ticks, and handlers are not wrapped): services are the
long-lived surface that owns sockets, and they are the only surface with
a defined recovery.
Verified the attribution mechanism outside the test too:
`AsyncLocalStorage.getStore()` inside an `uncaughtException` listener
returns the service's store for an unlistened emitter error, a timer
throw, a detached rejection (arrives with `origin=unhandledRejection`),
a `nextTick` throw and a `setImmediate` throw, and `undefined` for a
throw outside the scope, on Node 24.18 (AsyncContextFrame), Node 24 with
`--no-async-context-frame` (the async_hooks path Node 22 uses), and Node
26.5.
## How you verified
New test `apps/server/test/services/plugins/plugin-background.test.ts` ›
"routes an uncaught exception from a service's async context to the
supervisor": a service creates an `EventEmitter` with no `'error'`
listener and emits `'error'` from a timer. The test swaps out vitest's
`uncaughtException` listener for the same listener shape
`start-server.ts` installs, then asserts the service was aborted once
and started twice, its state is back to `running`, and nothing reached
the process unclaimed.
- Before (origin/main sources, listener only records): `AssertionError:
expected 1 to be 2` at `expect(globals.__emitterStarts).toBe(2)` — the
service is never restarted and the error reaches the process listener.
(With the committed test as-is, main fails earlier because
`service.handleUncaughtException` does not exist and the worker exits.)
- After: the test passes; full file 15/15.
- `pnpm exec turbo run typecheck --filter=@bb/server
--filter=@get-bb/plugin-sdk`: pass.
- `pnpm exec turbo run test --filter=@bb/server`: 1819/1820 pass; the
one failure is `internal-skill-trees` mode 420 vs 436, the known local
umask 0002 artifact, unrelated to this change. (A first run under load
average 42 had 51 five-second timeouts spread across unrelated files;
the affected files all passed on rerun.)
- Manual, on my dev instance with the report's 18-line repro plugin
installed: the server log shows one `Server listening`, then per cycle
`service imap-poller raised an uncaught exception outside start():
Socket timeout — aborting it` followed by `service imap-poller crashed:
Socket timeout — restarting in 1000ms` … `32000ms`; `GET
/api/v1/plugins` reports the service as `backoff`; the dev-supervisor
log has zero `Child exited` lines; `host_daemon_sessions` holds one
`active` row and none closed as `replaced`; `bb plugin remove
crashy-service` stops the cycle. On main the report shows the same
plugin killing the server every few seconds with 14 dumps and 4 replaced
daemon sessions.
Known trade-off: the observe-only `uncaughtExceptionMonitor` still
writes a `process-server-uncaughtException-*.json` report for every
contained error (the monitor fires before the listener can attribute
it). The stack is now also in the server log via `{ err }`. Suppressing
the report for attributed errors would need the diagnostics hook to
consult the plugin runtime; left for a follow-up if the reports turn out
to be noise.
Fixes #1746
> AGENT GENERATED: by Claude Opus 5
## Independent verification
Checked out `2048b0d11` (this PR's head) as `verify-1746-r1`;
`origin/main` (`c9aef7514`) is an ancestor, GitHub reports MERGEABLE.
**Root cause review.** Agrees with the issue and the report:
`runService` only watched the `start()` promise and the server process
only had the observe-only `uncaughtExceptionMonitor`. The fix is at the
root (per-run `AsyncLocalStorage` scope + a
`process.on("uncaughtException")` that claims attributed errors and
otherwise keeps Node's exit) and in the right layer (server owns plugin
runtime policy). No wire change, so no `HOST_DAEMON_PROTOCOL_VERSION`
bump is needed; no new public plugin API member (only a JSDoc update on
`PluginBackground.service`). I independently confirmed, in an actual
`uncaughtException` listener on Node 24.18, that the ALS store is
visible for an unlistened emitter `'error'`, a timer throw, a detached
rejection (`origin=unhandledRejection`), and a `nextTick` throw, and
absent for a throw outside the scope; and that a `.then` continuation
attached outside `run()` (the path `onServiceSettled` and the backoff
timer take) sees no store under both the AsyncContextFrame and
`--no-async-context-frame` implementations.
**Fail-before / pass-after.**
- `git checkout origin/main --
apps/server/src/services/plugins/plugin-runtime.ts
apps/server/src/services/plugins/plugin-service.ts
apps/server/src/start-server.ts
packages/plugin-sdk/src/backend-contract.ts`, then `pnpm exec vitest run
test/services/plugins/plugin-background.test.ts -t "routes an uncaught
exception"` in `apps/server`: the test listener throws `TypeError:
service.handleUncaughtException is not a function` and the vitest worker
exits (the uncaught exception does reach the process). With the listener
reduced to record-only: `AssertionError: expected 1 to be 2 // Object.is
equality` at `expect(globals.__emitterStarts).toBe(2)` — the service is
never restarted on main.
- Restored PR sources: `plugin-background.test.ts` 15/15; the new test
passed 5/5 consecutive runs.
**Turbo.** `pnpm exec turbo run typecheck --filter=@bb/server
--filter=@get-bb/plugin-sdk` passes. `pnpm exec turbo run test
--filter=@bb/server --filter=@get-bb/plugin-sdk --force`: plugin-sdk
14/14 files; server 1819/1820, the single failure is
`internal-skill-trees.test.ts` mode 420 vs 436 (known local umask 0002
artifact, unrelated, green in CI).
**Repro on the fixed branch.** Own dev instance (`scripts/bb-dev-app
current`, server :23506), installed the report's 18-line crashy plugin
with `pnpm bb:dev plugin install ... --yes`. Server log: `raised an
uncaught exception outside start(): Socket timeout — aborting it`
followed by `crashed: Socket timeout — restarting in 1000ms`, then
2000/4000/8000ms. `Server listening` appears once; dev-supervisor `Child
exited` count 0; `host_daemon_sessions` has one `active` row and no
`replaced`; `/api/v1/plugins` shows `imap-poller` in state `backoff` (no
longer lies as `running`). No longer reproduces.
**CI.** All checks on `2048b0d11` pass (Checks, Package Smoke x2, Tests
app-1/2/3, integration, packages, server on Node 22.x).
**Residual risks (non-blocking).**
- `uncaughtExceptionMonitor` still writes a
`process-server-uncaughtException-*.json` dump per contained error (I
observed 4 for 4 contained crashes) and nothing prunes that directory; a
service that fails immediately every cycle produces one dump per 60s at
the backoff cap. Follow-up: let the diagnostics hook consult the
runtime, or rotate dumps.
- Only background services are scoped; an out-of-band throw from a
plugin factory, schedule tick, or handler still exits the server. Server
code a service calls synchronously in-process (`bb.log`, kv, etc.;
`bb.sdk` is loopback HTTP so it is unaffected) runs inside the plugin's
scope and would be attributed to the plugin.
- After a service ignores its abort following an uncaught exception, the
plugin stays `degraded` even once the run settles and restarts (same
semantics as the existing stop-timeout path), and further uncaught
errors from that same claimed run are swallowed without a log line. No
per-plugin crash counter / auto-disable (issue point 3) — a permanently
broken service cycles at the 60s cap with a warning each time.
> AGENT GENERATED: by Claude Opus 5
Co-authored-by: Claude <[email protected]>1 parent d20638b commit 2ff8598
5 files changed
Lines changed: 182 additions & 5 deletions
File tree
- apps/server
- src
- services/plugins
- test/services/plugins
- packages/plugin-sdk/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
324 | 325 | | |
325 | 326 | | |
326 | 327 | | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
327 | 337 | | |
328 | 338 | | |
329 | 339 | | |
| |||
553 | 563 | | |
554 | 564 | | |
555 | 565 | | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
556 | 577 | | |
557 | 578 | | |
558 | 579 | | |
559 | 580 | | |
560 | 581 | | |
561 | 582 | | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
562 | 589 | | |
563 | | - | |
| 590 | + | |
564 | 591 | | |
565 | | - | |
| 592 | + | |
566 | 593 | | |
567 | 594 | | |
568 | | - | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
569 | 602 | | |
570 | | - | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
571 | 609 | | |
572 | 610 | | |
573 | 611 | | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
574 | 656 | | |
575 | 657 | | |
576 | 658 | | |
| |||
1789 | 1871 | | |
1790 | 1872 | | |
1791 | 1873 | | |
| 1874 | + | |
1792 | 1875 | | |
1793 | 1876 | | |
1794 | 1877 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
189 | 197 | | |
190 | 198 | | |
191 | 199 | | |
| |||
1003 | 1011 | | |
1004 | 1012 | | |
1005 | 1013 | | |
| 1014 | + | |
1006 | 1015 | | |
1007 | 1016 | | |
1008 | 1017 | | |
| |||
1603 | 1612 | | |
1604 | 1613 | | |
1605 | 1614 | | |
| 1615 | + | |
| 1616 | + | |
1606 | 1617 | | |
1607 | 1618 | | |
1608 | 1619 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
291 | 291 | | |
292 | 292 | | |
293 | 293 | | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
294 | 309 | | |
295 | 310 | | |
296 | 311 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
319 | 319 | | |
320 | 320 | | |
321 | 321 | | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
322 | 387 | | |
323 | 388 | | |
324 | 389 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
226 | 226 | | |
227 | 227 | | |
228 | 228 | | |
229 | | - | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
230 | 233 | | |
231 | 234 | | |
232 | 235 | | |
| |||
0 commit comments