Repository navigation
fix(db): give other tg commands a turn during tg db migrate alone - #169
Merged
Merged
Conversation
Without the daemon, tg db migrate ran its batches back to back, so another tg command that wrote the cache meanwhile could wait for the whole migration and give up with "database is locked" after its 5 s busy_timeout. It now pauses 150 ms after each second of work, so such a write gets in within about a second; the run takes up to 15% longer. With the daemon running, the pacing stays as it was. Fixes #122 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.
What and why
Since #121,
tg db migratepauses 150 ms after each batch while the daemon runs. Without the daemon it ran its batches back to back, so anothertgcommand that wrote the same cache meanwhile (tg messages listcaching a page,tg sendcaching the sent message) could wait for the whole migration and give up withdatabase is lockedafter its 5 sbusy_timeout. On Linux, where Bun bundles its own SQLite, the gaps between back-to-back batches are too short for a waiting writer to catch.Now
foregroundPacing(false)keeps the default batches (500 rows or 50 ms) and pausesOTHER_PROCESS_PAUSE_MS(150 ms) onceALONE_WORK_MS(1 s) of work has passed since the last pause. A waiting write gets the lock within about a second. With the daemon running, the pacing is unchanged.Before / After
busy_timeout.Cost
bun run bench:storage --messages 1000000 --old-form, run back to back on one machine in the order before, after, after, before. Every migration of this version was pending: 3,003,001 rows, including the re-index of names and the index merges. Other heavy jobs were running on the machine at the same time.That is 8 to 17% longer, 13% on average. By design the pauses add at most 15%. The numbers are in
docs/storage.mdanddocs/cli-commands.md("tg db migrate").Tests
src/__tests__/background-migrations.test.ts: the "is never refused" test is now a helper used by two cases. In the new case a writer runs in another process with no daemon and a 2 sbusy_timeout, next to a migration that holds the lock for more than 3 s. It expects no refusal. Before the fix, this test failed on Linux CI withrefused: 1(dispatched run 44162619868, the test commit alone). The macOS system SQLite lets the writer in between batches anyway, so on macOS it passes either way.bun run check,bun run test(2244 pass) andbun run test:e2e(369 pass) ran locally before the rebase onto 0.12.2; the migration tests ran again after it.Fixes #122
🤖 Generated with Claude Code