Skip to content

fix(db): let tg db migrate give way to the daemon's writes - #121

Merged
neoromantic merged 2 commits into
mainfrom
test/migration-writer-overlap
Oct 29, 2026
Merged

neoromantic merged 2 commits into
mainfrom
test/migration-writer-overlap

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Refs #89

Problem

background-migrations.test.ts › a writer next to a running migration failed on the Linux runner with duringMigration Received: 0, on main at a8c87e9 and on #120 (run 40795952241, job build / bun (ubuntu-latest)).

The flake shows a real bug. A write that waits for the lock sleeps in SQLite's busy handler between tries, 1, 2, 5 … 50 ms and then 100 ms. Batches that run back to back leave gaps of microseconds, and such a write misses all of them. tg db migrate set no pause between batches, so next to a running daemon on a big cache (about 19 s for 1M messages, minutes for 10M) a daemon write could give up with database is locked after its 5 s busy_timeout. The docs promised that it waits for one batch at most.

The daemon's own migrations are fine: at most 250 ms of batches per tick, then the loop sleeps for a second.

Change

  • foregroundPacing(daemonRunning) in src/db/background-migrations.ts, used by tg db migrate. With a live daemon.pid, batches take about 50 ms (SHARED_BATCH_LIMITS, at most 2,500 rows, instead of 500) and each is followed by a 150 ms pause (OTHER_PROCESS_PAUSE_MS). The pause is longer than the busy handler's longest sleep, so a waiting write always has a try inside it. Without the daemon, batches run back to back as before. The daemon's settings don't change.
  • Cost with the daemon running: the migration works 50 ms of every 200. Measured on 30,000 made-up old-form messages: 15.2k rows/s alone, 3.3k rows/s paced (4–5×). At the rates in docs/storage.md, 1M messages take about 19 s alone and about 80 s next to the daemon.
  • docs/cli-commands.md (tg db migrate), docs/daemon.md (background migrations) and a CHANGELOG.md entry under Fixed.

Test

a writer in another process next to tg db migrate › is never refused replaces the old test. The child runs foregroundPacing(true) with each messages batch slowed to 100 ms inside its transaction, 7 batches or more: at least 700 ms of the write lock. Meanwhile a writer with busy_timeout = 500 writes every millisecond. It must never be refused, must see the migration running, and the migration must end done with every row. The child reports its longest batch, so the test also checks that the slowdown really happened. The overlap is certain because the pause is longer than any sleep of the busy handler. The old test's signal file is no longer needed.

Without the pause the writer is refused: 10 of 10 runs failed in Linux. macOS's system SQLite lets the write in between batches anyway, so on a Mac the test can't catch a regression. CI on Linux does.

A tighter bound on each write's wait (2 batches, or the longest batch plus the pause) failed about 1 run in 70 in a Linux container. The wait is the rest of the batch plus the busy handler's sleep over its end, which is 50 to 100 ms by then. So the bound is the writer's busy_timeout.

Runs

  • macOS, bun test src/__tests__/background-migrations.test.ts -t 'tg db migrate': 30/30 in a row; 15/15 with two full bun run test running in parallel (both 2105 pass, 0 fail).
  • Linux (oven/bun:1.4.3 container): 30/30 in a row; 15/15 with two full unit runs in parallel; with the pause removed, 0/10.
  • The whole background-migrations.test.ts (14 pass), src/__e2e__/db-migrate.e2e.test.ts (2 pass), bun run check: clean.

🤖 Generated with Claude Code

neoromantic and others added 2 commits October 29, 2026 04:49
The child migration could finish before the writer ever read its state:
a write waiting in SQLite's busy handler can miss every 1 ms gap between
batches. The child now holds after the first batch of
messages_raw_json_v2, without the write lock, until the writer has seen
it running; it gives up after 4 s instead of hanging.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tg db migrate ran its batches back to back. A write in another process
waits in SQLite's busy handler, whose sleeps grow to 100 ms, and missed
every gap of microseconds between batches: on a big cache a daemon write
could give up with "database is locked" after its 5 s busy_timeout.

With the daemon running, tg db migrate now runs batches of about 50 ms
(at most 2,500 rows) and pauses 150 ms after each, longer than the
busy handler's longest sleep, so a waiting write gets the lock after one
batch. The run takes four to five times as long; alone it goes at full
speed as before.

The writer test now runs that pacing in another process with batches
slowed to 100 ms, 700 ms of the lock in all, and a writer that gives up
after 500 ms: it fails without the pause on Linux.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic neoromantic changed the title test(db): make the writer-during-migration test overlap for certain fix(db): let tg db migrate give way to the daemon's writes Oct 29, 2026
@neoromantic
neoromantic merged commit c2b2d1a into main Oct 29, 2026
5 checks passed
@neoromantic
neoromantic deleted the test/migration-writer-overlap branch October 29, 2026 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant