Skip to content

bb-app: event loop stalls (~2s) from sync SQLite / events truncation without yielding #3196

Closed

Description

@lawrenceluk

Summary

bb-app server (observed 0.42.1 on macOS) chronically stalls its main event loop (~every 5s, maxDelayMs typically 1.9–2.1s, spikes to 5–6s under load). localhost API callers see undiagnosed fetch failed. Restarting the app did not clear the pattern: within ~40s the server was again ~90%+ CPU with continuous stall logs.

Evidence (redacted)

  • Continuous Event loop stalled lines in server logs (intervalMs: 5000, maxDelayMs ~1900–2500).
  • Process sample: main thread often in better_sqlite3 Statement::JS_get under timers; RSS hundreds of MB; long uptime before restart made it worse.
  • Local bb.db can grow large; events table dominated by bulky rows (including large commandExecution payloads).
  • During an incident window, host-daemon logged fetch failed posting events while /hosts latency was ~2s concurrent with usage-limits traffic.
  • Stall monitor often reports slowestWork as a cheap heartbeat (≪10ms) while the loop is actually delayed ~2s — attributable work is under-reported.

Suspected cause

  1. Sync SQLite on the server event loop against a large events table.
  2. Retention sweep completed-event-output-truncation (≈10s) appears to scan/update large JSON blobs without yielding (contrast with sweeps that use setImmediate).
  3. Instrumentation: runEventLoopWork marking sync bodies with blocksEventLoop: false, so attribution blames heartbeats instead of the sweep.

Scale amplifier (not the core bug)

Heavy agent history / large command outputs and host memory pressure make multi-second stalls more likely, but the missing yield + sync DB work on the event loop is the product bug.

Suggested fixes

  1. Mark sync sweep bodies as blocksEventLoop: true (and/or stall-triggered CPU profiles) so operators see the real culprit.
  2. Yield truncation batches; avoid loading full data until needed; shrink batches under load.
  3. Tighten events retention for large command outputs / high-churn rate-limit events.

Impact

First-party and third-party plugins that call the local bb HTTP/SDK API fail spuriously (fetch failed) when the hub is wedged—even when their own logic is correct.

Environment

  • macOS, desktop bb.app embedding bb-app server on 127.0.0.1:38886
  • Observed bb-app 0.42.1

Activity

  1. added theissue type on Sep 7, 2026
  2. added
    host
    confirmed-repro
    on Sep 7, 2026
  3. bb-slop-cop commented on Sep 7, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

  4. SawyerHood commented on Sep 9, 2026

    @SawyerHood
    Collaborator

    Tracked as a sub-issue of #1131. The parent now covers the shared scope and links this implementation. This issue retains its distinct evidence and acceptance criteria and remains open until that work is complete.

    AGENT GENERATED

  5. ymichael commented on Sep 25, 2026

    @ymichael
    Collaborator

    Fixed by #2920

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-reprohostperf

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions