Prevent duplicate migration-stamp failures during concurrent startup - #1454
Prevent duplicate migration-stamp failures during concurrent startup#1454jadch wants to merge 3 commits into
Conversation
When multiple hosts boot against the same database, let a runner accept a failed ledger insert only after confirming another runner committed the exact migration stamp. Preserve unrelated stamp failures and cover both outcomes with concurrent regressions.
Resolve duplicate stamp races in one SQLite statement, avoiding a cross-session verification read. Exercise the behavior against real libSQL and align the changeset with repository conventions.
|
Reopening: a merged downstream vendor-sync PR accidentally referenced this using GitHub’s |
|
Refreshed this onto current The self-host red is the exact failure already present on current Cloud shard 3 also hit a separate test-server failure: after its earlier scenarios passed, sign-in began returning 500 across seven unrelated scenarios. The same shard passed on #1455's refreshed head. The PR itself remains conflict-free against |
Problem
runSqliteDataMigrationsreads the completion ledger once at startup. When multiple hosts boot against the same database, they can all observe a migration as pending, complete its idempotent body, and then race to insert the same ledger stamp. The first insert wins; every other host currently fails startup on the duplicate primary key.This failure pattern appeared on the Cloudflare/D1 host during concurrent MCP initialization immediately after deployment: requests failed while the shared ledger was converging, then passed once every migration was stamped.
Fix
Stamp completed migrations atomically with:
The conflict target is only the migration name, so a concurrent runner that loses the same-stamp race converges without a startup failure while unrelated database errors still surface. Resolving the conflict in the write itself also avoids a cross-session verification read, whose visibility could lag another D1 session's commit.
The migration-body contract remains unchanged: bodies must be idempotent because they may rerun after a crash or concurrent startup.
Tests
bun run format:check;bun run lint;bun run typecheck(43/43 tasks);HOME=$(mktemp -d) bun run test(37/37 tasks).