Skip to content

Plugin background services can kill the server: supervisor only sees the promise chain #1746

Closed

Description

@Joesirven

Summary

A plugin background service can terminate the entire bb server process, and with it every host daemon connection. plugin-runtime.ts already supervises services well, but that supervision only observes the promise chain. Errors raised out-of-band — an EventEmitter 'error' with no listener, a detached promise rejection, a throw inside a timer callback — bypass it entirely and reach uncaughtExceptionMonitor, which by design observes and lets the process die.

This is a feature request, so per CONTRIBUTING.md I am opening an issue before any pull request.

Production evidence

On bb 0.35.1, Ubuntu 22.04, 4 cores / 4.9 GiB:

  • 1321 identical crash dumps over five days, in ~/.bb/logs/process-server-uncaughtException-*.json
  • Steady cadence of one every ~5m11s: 278, 278, 276, 279 crashes per day
  • Every dump identical: Error: Socket timeout, thrown from imapflow
  • Each crash dropped the host daemon's connection. The database recorded 1613 host_daemon_sessions closed as replaced against only 7 as daemon-disconnect
  • User-visible symptom was Thread interrupted because the host daemon disconnected

The cause was a bug in my own plugin: an ImapFlow client constructed without an 'error' listener. That part is entirely my fault and is fixed. The issue I am raising is that a plugin-level mistake of this kind can take down the host.

Mechanism

runService at apps/server/src/services/plugins/plugin-runtime.ts:427 wraps the service's start() and routes rejections to onServiceSettled, which restarts with capped exponential backoff. That is a good supervisor, and for promise-chain failures it works exactly as intended.

But the failure above never enters that chain. imapflow raises its socket timeout from a timer callback on a later tick:

Error: Socket timeout
    at TLSSocket.<anonymous> (imapflow/lib/imap-flow.js:1263:29)
    at TLSSocket.emit (node:events:509:28)
    at Socket._onTimeout (node:net:740:8)
    at Timeout.onStreamTimeout (node:internal/stream_base_commons:237:10)

ImapFlow is an EventEmitter, and Node rethrows an 'error' event with no listener as an uncaught exception. There is no promise linking that throw back to start(), so:

  • the try/finally inside the plugin's own IMAP wrapper cannot see it
  • the .catch() the plugin had around its poll cycle cannot see it
  • runService's rejection handler cannot see it

It goes straight to installSafeProcessDiagnostics (packages/process-utils/src/index.ts:257), which registers uncaughtExceptionMonitor. The monitor variant deliberately does not intervene, so Node's default behavior applies and the process exits. The comment at line 271 makes clear this is intentional, and for server-owned code it is the right call.

The same hole applies to any detached promise rejection or raw setTimeout/setInterval throw originating in plugin code.

Why the attribution gap is the real cost

All 1321 dumps name imapflow. Not one names the plugin that owns it. Nothing in bb records which plugin's async context the throw came from.

From an operator's seat, a misbehaving plugin is indistinguishable from bb itself being broken. I spent five days assuming I had a host daemon connectivity problem, because that is what the symptom looks like from the UI. The daemon had 7 days of uptime the entire time.

Proposed direction

  1. Run each plugin background service inside its own AsyncResource (or AsyncLocalStorage context) so out-of-band throws can be attributed to the owning plugin.
  2. Route attributed failures into the backoff-and-restart path that onServiceSettled already implements.
  3. Auto-disable a plugin that crash-loops, surfacing it the same way reportNeedsConfiguration surfaces configuration failures.
  4. Include the owning plugin id in the crash diagnostic when attribution succeeds.

Known tradeoff

Point 2 requires switching from uncaughtExceptionMonitor to uncaughtException so the handler can actually intervene. Node's documentation is explicit that resuming after an uncaught exception can leave corrupt state, and the current choice looks deliberate rather than accidental. I do not want to hand-wave that.

The fully robust alternative is out-of-process plugin hosting: run each plugin in a worker thread or child process so a crash is structurally unable to reach the server. That is a much larger change. BbPluginApi currently hands plugins live objects (bb.sdk, bb.log, bb.slots) and callbacks that do not serialize across a process boundary, every existing plugin would need rewriting or shimming, and each plugin process carries its own ~40-80 MB Node runtime. On a 4.9 GiB host running 17 plugins that overhead is significant.

I would happily take the cheaper in-process boundary if out-of-process hosting is not on the roadmap. Step 4 alone (attribution in the crash dump) would have saved me most of those five days, and carries none of the uncaughtException risk.

Related

Happy to work on this once there is agreement on which direction is wanted.

Activity

  1. added theissue type on Aug 18, 2026
  2. added
    plugins
    host
    on Aug 18, 2026
  3. SawyerHood commented on Aug 18, 2026

    @SawyerHood
    Collaborator

    Reproduction & root-cause report: https://get-bb.github.io/reports/issues/1746.html

    • Verdict: REPRODUCED · root-cause confidence: high
    • Root cause (short): Plugins run in-process and background services are supervised only via the start() promise (plugin-runtime.ts runService), while the server registers only an observe-only uncaughtExceptionMonitor (process-utils installSafeProcessDiagnostics) and no unhandledRejection handler, so any out-of-band plugin error (unlistened EventEmitter 'error', timer throw, detached rejection) exits the server, which restarts, reloads the plugin, and crash-loops with no plugin attribution in the dump.
    • Proposed fix: (1) Attribution: wrap every plugin entry point (factory, runService, schedule ticks, handlers) in an AsyncLocalStorage store {pluginId, surface} owned by plugin-runtime, and let installSafeProcessDiagnostics accept an attribution() hook merged into the crash report; also add an unhandledRejection handler that writes a report (verified ALS store is visible in uncaughtExceptionMonitor/unhandledRejection for emitter error, timer throw, and detached rejection on Node 24). (2) Containment: add an uncaughtException handler that, only when the store attributes the throw to a plugin, routes it into on…
    • Caveats: Reporter's production counts (1321 dumps, 1613 replaced sessions) are unverifiable but consistent with the mechanism. In my repro the daemon reconnected to the same session across several fast restarts, so replaced-session count (4) was lower than crash count (14). I did not run a live agent thread during the crash loop, so the "Thread interrupted because the host daemon disconnected" message was …

    The report has copy-paste repro steps, screenshots where the bug is visual, code permalinks at 16ceb3a54, and repro artifacts. It was independently re-run by a second agent (see its Verification section).

    AGENT GENERATED: by Claude Opus 5

  4. added
    confirmed-repro
    on Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reprohostplugins

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions