Skip to content

destroyed-environment-prune blocks the event loop for 1–8 s per statement #4161

Closed

Description

@mattwyckhouse

Environment. bb 0.43.3 desktop; bb.db 5.2 GB with 1.9 M events; ~1,250 destroyed
git-worktree environments (about 1,240 destroyed per week); ~986k events still attached to
destroyed environments awaiting the 7-day prune.

Symptom. Slow DB query lines for UPDATE events SET environment_id = NULL WHERE rowid IN (…)
with durations of 1–8.3 s: 80 lines / 89 s in the first 80 minutes after a restart (p50 1.04 s,
max 3.96 s); 23 lines / 55 s (max 8.3 s) plus 11 lines / 54 s (max 9.4 s) for the companion
SELECT rowid, octet_length(data) … INDEXED BY events_environment_idx in the previous process.
sweep:destroyed-environment-prune:advance is the top tracked slowestWork in
Event loop stalled records (125 s over 19.5 h).

Evidence (installed bundle server/dist/start-server.js).

  • The sweep runs on the 10 s periodic tick (setInterval(… runPeriodicSweeps …, 1e4), 352459–352464;
    cadenceMs: 0 for destroyed-environment-prune, 349309–349311).
  • runDestroyedEnvironmentPruneSweep (349255–349270) performs up to 10 iterations, each one
    synchronous inside runEventLoopWorkSync, yielding with setImmediate between them.
  • pruneDestroyedEnvironments (39606–39660) picks one stale environment, selects up to 50
    candidate rows computing octet_length(data) per row, stops at a 256 KiB byte budget, runs
    UPDATE events SET environment_id = NULL WHERE rowid IN (…), and deletes the environment row
    when nothing is left. Constants at 39671–39680.
  • Each detach rewrites the whole events row (the column lives in the wide record whose data
    can be hundreds of KB) and the candidate select reads those overflow pages to measure them.
    The per-statement latency, not the backlog, is the stall: the sweep drains up to 500 events per
    10 s, far above inflow, and on this host no destroyed environment is currently past the TTL.

Reproduction. Populate events with wide data (tens to hundreds of KB) attached to ~1,000
environments, destroy them, advance 7 days (or lower the TTL), and watch the UPDATE statements in
the slow-query log and slowestWorkMs.

Proposed fix. Give the detach a time budget (stop after ~50 ms of wall clock) and a smaller
byte budget instead of a fixed 50 rows; avoid octet_length(data) on the candidate scan (store
the size, or select by rowid range); consider keeping environment_id out of the wide events
row (a side table keyed by rowid) so detaching is index-only; or run retention sweeps on a worker
thread with its own connection. Note: relying on the existing ON DELETE SET NULL foreign key
instead of chunking would perform the same update over every attached row in one synchronous
statement and make the stall worse.


Filed from an audit of bb 0.43.3 by the Factory plugin (github.com/mattwyckhouse/bb-plugin-factory). Method, re-derived numbers and the adversarial review of this report: plans/076-host-sustainable-line/bb-core-issues/REVIEW.md in that repository.

Activity

  1. added theissue type on Sep 23, 2026
  2. added
    workspaces
    partial-repro
    on Sep 23, 2026
  3. bb-slop-cop commented on Sep 23, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    Classified Bug · Priority Medium · Effort Medium, with workspaces, perf, and partial-repro.

    Reproduction and root-cause report: PARTIALLY REPRODUCED, medium confidence. Two clean trusted-main checkouts confirmed synchronous cleanup and the oversized-row budget exception; measured calls took 0.322–6.282 ms, not seconds. The report includes the repeatable test. SQLite optimized the candidate byte-length lookup without loading full payloads.

    Root cause established: database work completes synchronously before the sweep yields; the byte budget cannot bound individual statement duration. The reported multi-second latency remains unverified. No PR: there is no failing regression for that stall supporting a safe small fix.

  4. added 2 commits that reference this issue on Sep 24, 2026
    9320ce7
    6984eba
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

    partial-reproperfworkspaces

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions