Repository navigation
fix(db): let tg db migrate give way to the daemon's writes - #121
Merged
Merged
Conversation
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]>
This was referenced Oct 29, 2026
Merged
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.
Refs #89
Problem
background-migrations.test.ts› a writer next to a running migration failed on the Linux runner withduringMigrationReceived: 0, onmainat a8c87e9 and on #120 (run 40795952241, jobbuild / 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 migrateset 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 withdatabase is lockedafter its 5 sbusy_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)insrc/db/background-migrations.ts, used bytg db migrate. With a livedaemon.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.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 aCHANGELOG.mdentry 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 withbusy_timeout = 500writes every millisecond. It must never be refused, must see the migrationrunning, and the migration must enddonewith 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
bun test src/__tests__/background-migrations.test.ts -t 'tg db migrate': 30/30 in a row; 15/15 with two fullbun run testrunning in parallel (both 2105 pass, 0 fail).oven/bun:1.4.3container): 30/30 in a row; 15/15 with two full unit runs in parallel; with the pause removed, 0/10.background-migrations.test.ts(14 pass),src/__e2e__/db-migrate.e2e.test.ts(2 pass),bun run check: clean.🤖 Generated with Claude Code