Skip to content

fix(db): give other tg commands a turn during tg db migrate alone - #169

Merged
neoromantic merged 1 commit into
mainfrom
fix/migrate-give-writers-a-turn
Nov 17, 2026
Merged

neoromantic merged 1 commit into
mainfrom
fix/migrate-give-writers-a-turn

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

What and why

Since #121, tg db migrate pauses 150 ms after each batch while the daemon runs. Without the daemon it ran its batches back to back, so another tg command that wrote the same cache meanwhile (tg messages list caching a page, tg send caching the sent message) could wait for the whole migration and give up with database is locked after its 5 s busy_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 pauses OTHER_PROCESS_PAUSE_MS (150 ms) once ALONE_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

  • Before: with the daemon stopped, a write from another process during a long migration waits for the whole run and fails after its busy_timeout.
  • After: it gets in within about a second. The run takes up to 15% longer.

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.

run 1 run 2
before 63.5 s 59.7 s
after 68.9 s 69.7 s

That is 8 to 17% longer, 13% on average. By design the pauses add at most 15%. The numbers are in docs/storage.md and docs/cli-commands.md ("tg db migrate").

Tests

  • New test in 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 s busy_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 with refused: 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) and bun 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

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]>
@neoromantic
neoromantic merged commit 872247f into main Nov 17, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/migrate-give-writers-a-turn branch November 17, 2026 20:24
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.

tg db migrate without the daemon: give other tg commands a turn

1 participant