Skip to content

Slow-fsync VPS stalls: 100 ms WAL checkpoint polling across all plugin DBs, plugin data.db missing synchronous=NORMAL, and sync main-thread DB writes #8345

Description

@architawr

Summary

On a Linux VPS with typical virtualized storage (fsync median 6.5 ms, vs ~0.1 ms on Apple Silicon), bb server experiences hundreds of 0.5–5.4 s event loop stalls and thousands of slow WAL checkpoints per day even while overall host CPU is 15–20% (steal = 0%) and 26/30 GiB RAM is free.

Three low-effort SQLite/WAL behaviors — all still present on main at b0ddaad731565d7e4de68d824120d802fc78f7d5 — amplify slow fsync into continuous main-thread blocking:

  1. walCheckpointWorker polls PRAGMA wal_checkpoint(PASSIVE) unconditionally every 100 ms for bb.db and every plugin data.db (34 databases on this instance ≈ 340 checkpoints/sec), with 86.5% of slow checkpoints firing when walFrames <= 16 (and 227 when walFrames == 0).
  2. Plugin databases opened in plugin-api.ts omit PRAGMA synchronous = NORMAL (unlike bb.db and the checkpoint worker), leaving every plugin SQLite write at SQLite's default synchronous = FULL — forcing a synchronous fsync() on the main event loop on every plugin commit.
  3. Synchronous single-row/small DB writes on the main event loop (heartbeatSession, plugin_kv, plugin_schedules, INSERT INTO events) contend with that I/O/WAL storm and block the main thread for 100–2,500 ms while spending only 4–35% of wall time on CPU.

Versions and environment

  • bb version: 0.50.1 (bb-app installed via npm; code paths also verified against main at b0ddaad731565d7e4de68d824120d802fc78f7d5)
  • Node: v22.22.1
  • OS / Hardware (remote server): Linux KVM VPS (8 vCPU, Intel Xeon Silver 4216 @ 2.10 GHz, 30 GiB RAM / 26 GiB free, 429 GB disk free, CPU steal = 0%)
  • Setup: Remote server mode on Linux VPS with walCheckpointWorker enabled, 1 remote macOS host-daemon connected, 44 active plugins (33 plugin data.db files + bb.db = 34 SQLite DBs), bb.db size 964 MB (events: 230k rows / 735 MB).
  • Comparison baseline: Same workload previously ran smoothly on macOS (Apple M4), where 4 KB write + fsync is ~65× faster and single-thread JSON.parse is ~3.7× faster.

Steps to reproduce

The underlying disk/WAL amplification is reproducible on any Linux VPS with virtualized storage (fsync ~5–15 ms) running bb server with walCheckpointWorker enabled and multiple plugins that call bb.storage.database():

  1. Measure baseline 4 KB write + fsync latency on the host filesystem (e.g., 15 sequential 4 KB writes + os.fsync()):
    • On this VPS: min=2.0ms median=6.5ms p90=16.9ms max=18.8ms avg=8.9ms.
    • On Apple Silicon macOS (reference): median=0.11ms.
  2. Enable walCheckpointWorker and run bb server with bb.db + ~25–35 plugins that open plugins/<id>/data.db via bb.storage.database().
  3. Connect a remote host-daemon (sending periodic heartbeats) and run 2–3 concurrent agent threads while watching logs/server.*.log for "Slow WAL checkpoint", "Slow DB query", and "Event loop stalled".
  4. Note: wal_checkpoint(PASSIVE) on a 1-frame WAL takes 0.11 ms median on macOS, so this behavior stays mostly invisible on local Apple Silicon dev machines and surfaces sharply on virtualized VPS storage (especially when host iowait rises from 0.05% to 0.3–0.6%).

Expected vs actual

Observed in a single day's server log (server.5.log):

  • Slow WAL checkpoint (>= 100 ms): 3,762 occurrences
    • Duration: min=100.0 ms, median=146.0 ms, p90=313.2 ms, max=2,316.9 ms, avg=187.2 ms.
    • Distribution by WAL size: 3,256 / 3,762 (86.5%) had walFrames <= 16, and 227 had walFrames == 0.
    • Split across DBs: 2,078 on bb.db + 1,684 across plugin data.db files (account-pool: 141, server-watch: 129, burn: 117, agent-calendar: 112, bb--storage-retention: 112, automations: 105, agentation: 86, bb-recap: 82, side-chat: 79, etc.).
  • Event loop stalled (>= 500 ms): 1,220 occurrences (304 >= 1 s, max = 5,447.7 ms)
    • Top stall causes attributed in logs:
      • 254 — ws:daemon heartbeat > db:transaction deferred: update "host_daemon_sessions"...
      • 159 — sweep:thread-event-pruning (until first await)
      • 94 — db:transaction immediate: INSERT INTO events ...
      • 81 — sweep:environment-provider-lifecycle (until first await)
      • 56 — db:all select "threads"...
      • 38 + 23 — timeline-build on large threads
      • 28 — db:run insert into "plugin_kv"...
  • Slow DB query (>= 100 ms) — wall time vs CPU time proves I/O & lock waiting, not just CPU:
    • 1,577 slow transactions total (max = 2,516.7 ms); 414 spent < 35% of wall time on CPU.
    • run insert into "plugin_kv": n=57, avg_wall=178.0 ms, avg_cpu=20.3 ms (11.4% CPU), max=668.4 ms.
    • run update "plugin_schedules": n=15, avg_wall=186.1 ms, avg_cpu=8.0 ms (4.3% CPU), max=763.9 ms.

Trimmed verbatim log samples (paths redacted):

{"level":30,"component":"server","database":"<dataDir>/bb.db","walFrames":12,"checkpointedFrames":12,"durationMs":119.75,"msg":"Slow WAL checkpoint"}
{"level":30,"component":"server","bindingArgumentCount":1,"durationMs":118.4,"cpuDurationMs":118.6,"operation":"transaction","sql":"TRANSACTION IMMEDIATE","thresholdMs":100,"msg":"Slow DB query"}

Expected:

  • Idle or nearly-empty WAL files (walFrames == 0 or a handful of frames) should not trigger 100 ms polling checkpoints across 30+ SQLite databases.
  • Plugin data.db connections should use PRAGMA synchronous = NORMAL consistently with bb.db and walCheckpointWorker, rather than issuing synchronous FULL fsync() calls on the main event loop.
  • Routine single-row writes (like daemon heartbeats / plugin_kv / plugin_schedules / streaming event appends) should not stall the main thread for hundreds of milliseconds due to background checkpoint I/O contention.

Evidence

1. Code permalinks on main (b0ddaad731565d7e4de68d824120d802fc78f7d5)

  1. Unconditional 100 ms wal_checkpoint(PASSIVE) loop for every attached database:

    • packages/db/src/wal-checkpointer.ts#L7-L10 sets WAL_CHECKPOINT_INTERVAL_MS = 100.
    • packages/db/src/wal-checkpointer.ts#L30-L74 unconditionally calls target.database.pragma("wal_checkpoint(PASSIVE)") and reschedules setTimeout(() => run(id, target), catchUp ? 0 : workerData.intervalMs) every 100 ms for every attached DB — regardless of whether any writes happened or how many WAL frames exist, and with no jitter across the 30+ attached databases.
  2. Plugin data.db connections omit PRAGMA synchronous = NORMAL:

  3. Synchronous daemon heartbeat transaction on the main thread:

    • apps/server/src/ws/daemon-protocol.ts#L310-L334 and packages/db/src/data/sessions.ts#L192-L214 run heartbeatSession synchronously inside runEventLoopWorkSync("ws:daemon heartbeat", ...) (updating host_daemon_sessions + hosts.last_seen_at). Although gated by LEASE_RENEWAL_INTERVAL_MS, whenever it collides with WAL checkpoint I/O on a slow disk it stalls the event loop (254 stalls in 24h) and cascades into host-daemon heartbeat-ack-timeout disconnects.

2. Host benchmark measurements (how numbers were measured)

Same scripts run on both the Linux VPS and the macOS Apple M4 machine:

Metric Linux VPS (Xeon Silver 4216, KVM) macOS (Apple M4) Ratio
4 KB write + fsync (15× sequential, median) 6.5 ms (p90 16.9 ms, max 18.8 ms) 0.10 ms ~65× slower
SQLite 1-frame wal_checkpoint(PASSIVE) (15×, median) ~6–19 ms idle / 146 ms median under load 0.11 ms 60–1,300× slower
Node JSON.parse (200× ~500-item payload) 151.8 ms 40.8 ms 3.7× slower
Node single-thread int loop (200M iters) 3,287.9 ms 1,828.6 ms 1.8× slower

3. Concrete low-effort fixes (no full multi-threaded DB rewrite needed)

  1. Add database.pragma("synchronous = NORMAL"); in apps/server/src/services/plugins/plugin-api.ts#L778-L780 to match bb.db and walCheckpointWorker.
  2. Stop polling wal_checkpoint(PASSIVE) every 100 ms on idle/tiny WALs in wal-checkpointer.ts:
    • Check the -wal file size via stat() (or a dirty/write notification from the main thread) before invoking PRAGMA wal_checkpoint(PASSIVE), or only checkpoint when WAL exceeds a frame/byte threshold (e.g., >= 1,000 frames / 4 MB) or after an idle backoff interval (e.g., 5–30 s with per-DB jitter, rather than a flat 100 ms across all 30+ DBs).
    • Keeping WAL_CHECKPOINT_INTERVAL_MS adaptive (fast catch-up when WAL is large, slow/idle when walFrames == 0 or unchanged) eliminates thousands of redundant fsync calls on VPS disks and also cuts disk write amplification on laptops.
  3. Batch/coalesce small frequent writes on the main thread (e.g., INSERT INTO events bursts during active agent turns, and deferring/coalescing hosts.last_seen_at / host_daemon_sessions lease writes off the synchronous WS heartbeat critical path).

What you ruled out

Suggested priority and effort

Medium-High for self-hosted Linux VPS users (no good workaround other than moving to local-NVMe hardware or disabling most plugins); effort is Low for items 1 and 2 (plugin-api.ts one-liner + adaptive/threshold backoff in wal-checkpointer.ts).

Checks

  • I reproduced this on the latest release (0.50.1) and verified the exact code paths are still unchanged on main (b0ddaad731565d7e4de68d824120d802fc78f7d5).
  • I searched open and closed issues for the same problem.
  • If an agent wrote this, the body ends with > AGENT GENERATED.

AGENT GENERATED

Activity

  1. added theissue type on Nov 29, 2026
  2. added
    host
    plugins
    partial-repro
    on Nov 29, 2026
  3. bb-slop-cop commented on Nov 29, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · High priority · Medium effort. Applied host, plugins, perf and reproduction label partial-repro; no existing classification was replaced.
    • Report: Reproduction and root-cause report.
    • Verdict: PARTIALLY REPRODUCED · Root-cause confidence: medium.
    • Root cause confirmed: the worker checkpoints before checking whether to catch up or schedule another 100 ms pass. Two clean runs recorded 407 and 405 idle checkpoint calls across 34 databases in about 1.2 seconds.
    • Important correction: real plugin WAL connections already returned synchronous=1 (NORMAL), with DEFAULT_WAL_SYNCHRONOUS=1 in this build. The reported FULL-default explanation did not reproduce.
    • Reproduction: the report includes the complete real-plugin/worker diagnostic and second-clean-checkout results. Existing WAL and plugin storage suites passed: 5 + 19 tests. Checkpoint calls are not fsync counts; the reported long stalls and their I/O causality remain unverified.
    • No safe PR: no failing slow-storage regression establishes a safe scheduling change, and the proposed synchronization one-liner is behaviorally redundant on the verified build.
  4. alpine-vortex commented on Nov 30, 2026

    @alpine-vortex

    Same pattern on a home Linux server (bb 0.50.1, ext4 on NVMe, no VPS), seen from the disk-wear side rather than stalls.

    • NVMe averages ~2.5 MB/s sustained writes (~150 GB/day, ~9.7 TB over 65 days), mostly small 4 KB writes. jbd2 is the top device-level writer, i.e. many small fsync commits.
    • bb server + host-daemon write ~270 kB/s per pidstat -d, the largest single source attributable to one app.
    • Plugin data.db files (host-monitor, agent-lanes, shell-tasks, etc.) show 4-6 MB WALs touched every few seconds, consistent with the missing synchronous = NORMAL and the 100 ms PASSIVE checkpoint polling described here.

    Setting synchronous = NORMAL on plugin DBs and skipping the checkpoint when the WAL is empty would likely cut this noticeably.

  5. added a commit that references this issue on Dec 1, 2026
    bd85575
  6. added a commit that references this issue on Dec 1, 2026
    d24bf81
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

    hostpartial-reproperfplugins

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions