Repository navigation
Conversation
The WAL checkpoint worker ran wal_checkpoint(PASSIVE) every 100 ms on bb.db and every plugin data.db. Any commit in that window got its frames copied into the database file right away, with an fsync of the WAL and one of the database. Under synchronous=NORMAL commits themselves never fsync, so this turned each small commit into two fsyncs and copied hot pages into the database file again and again. On slow-fsync Linux disks that stalled the server; on NVMe it showed up as sustained small writes. The worker now reads mxFrame and nBackfill from the WAL index header every 100 ms and checkpoints once 1,000 frames are pending, the point where SQLite's default autocheckpoint would have run inside a commit. Explicit checkpoints, catch-up passes and stop() are unchanged. Fixes #8345 Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
deleted the
bb/8345-slow-fsync-vps-stalls-100-ms-wal-checkpoint-thr_3dvi4j3nau
branch
December 1, 2026 00:53
scowalt
pushed a commit
to scowalt/bb
that referenced
this pull request
Dec 1, 2026
…et-bb#8430) ## Human comments ## What was wrong The WAL checkpoint worker (get-bb#7108) ran `wal_checkpoint(PASSIVE)` every 100 ms on `bb.db` and every plugin `data.db`. Any commit in that window had its frames copied into the database file straight away, with one fsync of the WAL and one of the database file. Under `synchronous=NORMAL` commits never fsync on their own, so this turned each small commit (heartbeats, plugin kv rows, event appends) into two fsyncs. It also rewrote hot pages into the database file again and again, where SQLite's default 1,000-frame autocheckpoint writes each page once per batch. On slow-fsync Linux disks this saturated the device and stalled the server. On NVMe it showed up as sustained small 4 KB writes (alpine-vortex: about 150 GB/day). The issue's second claim, that plugin `data.db` connections run at `synchronous=FULL`, does not hold. better-sqlite3 is built with `SQLITE_DEFAULT_WAL_SYNCHRONOUS=1`, and plugin connections read back `synchronous=1` once in WAL mode, so that one-liner would change nothing. The main-thread stalls in the issue's third point are a symptom of the fsync storm, so this PR does not coalesce writes. ## What changed - `packages/db/src/wal-checkpointer.ts`: every 100 ms the worker reads `mxFrame` and `nBackfill` from the WAL-index (`-shm`) header with a plain `pread` of the first 100 bytes, which stays clear of the lock bytes Windows enforces at offset 120 and up. It runs `wal_checkpoint(PASSIVE)` only once 1,000 frames are not yet backfilled, the point where SQLite's default autocheckpoint would have run inside a commit. If the header cannot be read or has an unexpected version, the worker checkpoints as before. Explicit `checkpoint()` requests (the vacuum sweep), the 4,096-frame catch-up, the starvation backstop and `stop()` are unchanged. - `docs/configuration.md`: describes the new trigger. - Durability matches bb before get-bb#7108 and SQLite's `synchronous=NORMAL` default: up to 1,000 frames of recent commits can sit un-fsynced, so a power loss can lose them but cannot corrupt the database. ## How you verified - New test `leaves a WAL alone until it holds a thousand frames the database lacks` in `packages/db/test/wal-checkpointer.test.ts`. It fails on main (the WAL restarts after one small commit) and passes on this branch. All 6 checkpointer tests pass. - `turbo run test --filter=@bb/server` on database-maintenance-sweep, plugin-settings-storage, periodic-sweeps, plugin-state-snapshot and plugin-update: 90 passed. `turbo run typecheck --filter=@bb/db`: clean. - **Live production server (macOS, 60 s, sampling the `-shm` headers):** 626 commits caused 299 backfills (about 600 fsyncs/min). On `bb.db` about 6,000 WAL frames written led to 6,714 frames backfilled, so almost every write was copied straight into the database file. The process wrote about 2.1 MB/s. - **Linux (`node:22-bookworm`, strace), real checkpointer module, 34 databases, about 16 small commits/s, 60 s, two runs each:** | | fsyncs (setup excluded) | block `write_bytes` | slow checkpoint logs | |---|---|---|---| | main | about 2,200–2,450 | 123–132 MB | 0–29 | | this branch | about 30 | 15 MB | 0–1 | | no worker (commit autocheckpoint) | 0 | 15 MB | 0 | - **`pnpm start:worktree` on this branch, 90 s:** `bb.db` took 4 commits and 16 frames with 0 backfills, and no "Slow WAL checkpoint" lines. Fixes get-bb#8345 > AGENT GENERATED 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
The WAL checkpoint worker (#7108) ran
wal_checkpoint(PASSIVE)every 100 ms onbb.dband every plugindata.db. Any commit in that window had its frames copied into the database file straight away, with one fsync of the WAL and one of the database file. Undersynchronous=NORMALcommits never fsync on their own, so this turned each small commit (heartbeats, plugin kv rows, event appends) into two fsyncs. It also rewrote hot pages into the database file again and again, where SQLite's default 1,000-frame autocheckpoint writes each page once per batch. On slow-fsync Linux disks this saturated the device and stalled the server. On NVMe it showed up as sustained small 4 KB writes (alpine-vortex: about 150 GB/day).The issue's second claim, that plugin
data.dbconnections run atsynchronous=FULL, does not hold. better-sqlite3 is built withSQLITE_DEFAULT_WAL_SYNCHRONOUS=1, and plugin connections read backsynchronous=1once in WAL mode, so that one-liner would change nothing. The main-thread stalls in the issue's third point are a symptom of the fsync storm, so this PR does not coalesce writes.What changed
packages/db/src/wal-checkpointer.ts: every 100 ms the worker readsmxFrameandnBackfillfrom the WAL-index (-shm) header with a plainpreadof the first 100 bytes, which stays clear of the lock bytes Windows enforces at offset 120 and up. It runswal_checkpoint(PASSIVE)only once 1,000 frames are not yet backfilled, the point where SQLite's default autocheckpoint would have run inside a commit. If the header cannot be read or has an unexpected version, the worker checkpoints as before. Explicitcheckpoint()requests (the vacuum sweep), the 4,096-frame catch-up, the starvation backstop andstop()are unchanged.docs/configuration.md: describes the new trigger.synchronous=NORMALdefault: up to 1,000 frames of recent commits can sit un-fsynced, so a power loss can lose them but cannot corrupt the database.How you verified
New test
leaves a WAL alone until it holds a thousand frames the database lacksinpackages/db/test/wal-checkpointer.test.ts. It fails on main (the WAL restarts after one small commit) and passes on this branch. All 6 checkpointer tests pass.turbo run test --filter=@bb/serveron database-maintenance-sweep, plugin-settings-storage, periodic-sweeps, plugin-state-snapshot and plugin-update: 90 passed.turbo run typecheck --filter=@bb/db: clean.Live production server (macOS, 60 s, sampling the
-shmheaders): 626 commits caused 299 backfills (about 600 fsyncs/min). Onbb.dbabout 6,000 WAL frames written led to 6,714 frames backfilled, so almost every write was copied straight into the database file. The process wrote about 2.1 MB/s.Linux (
node:22-bookworm, strace), real checkpointer module, 34 databases, about 16 small commits/s, 60 s, two runs each:write_bytespnpm start:worktreeon this branch, 90 s:bb.dbtook 4 commits and 16 frames with 0 backfills, and no "Slow WAL checkpoint" lines.Fixes #8345
🤖 Generated with Claude Code