Skip to content

Prevent duplicate migration-stamp failures during concurrent startup#1454

Closed
jadch wants to merge 2 commits into
UsefulSoftwareCo:mainfrom
jadch:fix/concurrent-data-migration-stamps
Closed

Prevent duplicate migration-stamp failures during concurrent startup#1454
jadch wants to merge 2 commits into
UsefulSoftwareCo:mainfrom
jadch:fix/concurrent-data-migration-stamps

Conversation

@jadch

@jadch jadch commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Problem

runSqliteDataMigrations reads 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:

INSERT INTO data_migration (name, time_completed)
VALUES (?, ?)
ON CONFLICT(name) DO NOTHING

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

  • deterministic concurrent-runner regression against real in-memory libSQL: both runners read an empty ledger, both bodies complete, and exactly one stamp remains;
  • non-conflict stamp failures still surface with their original cause;
  • concurrent regression passed 100/100 repeated runs;
  • bun run format:check;
  • bun run lint;
  • bun run typecheck (43/43 tasks);
  • full non-E2E suite via isolated HOME=$(mktemp -d) bun run test (37/37 tasks).

jadch added 2 commits July 23, 2026 08:16
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.
@jadch jadch changed the title Converge concurrent data migration stamps Prevent duplicate migration-stamp failures during concurrent startup Jul 23, 2026
@jadch jadch closed this Jul 23, 2026
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