Skip to content

Self-host cluster: instances starting together race on migrations ("duplicate column name", uncaught) #309

Done

Description

@Madraka

What happened

In the Swarm smoke test of #308 (run 46188727714, the Build edge job's "Swarm smoke test" step), the two cloudhal instances of a fresh installation start together on one store. One of them logs:

chsmoke6430_cloudhal.1 | workerd/server/server/listen/mod.rs:393: error: Uncaught exception: workerd/jsg/value.h:1456: failed: remote.jsg.Error: StorageError: migration "0004_audit_actor_name" failed: batch failed: D1_ERROR: duplicate column name: actor_name: SQLITE_ERROR

The other instance applied 0004 first. The request that hit the loser failed with an uncaught exception. The installation then came up, because later requests see the migrations as done, so the smoke test was not blocked by this. But a first request can fail on any fresh multi-instance start, and the outcome depends on timing.

Why (code read on main)

  • packages/storage/src/migrate.ts, migrate():
    • It reads the applied set from _migrations once.
    • Then it applies every pending migration as one atomic batch() (its statements plus INSERT INTO _migrations (name, …), whose name is the primary key). On any batch error it throws StorageError: migration "<name>" failed.
    • Two instances that read the set at the same time both try the same migration. The winner commits. The loser's batch fails, rolls back as a whole, and is thrown, although the migration is now applied.
  • packages/cloudflare/src/admin-worker.ts:
    • ensureMigrated (the admin Worker's per-isolate gate) and SchedulerClock catch once and call migrate() a second time ("a concurrent request or isolate applied them first").
    • With more than one migration still pending, the second pass can lose the next race (0005 after 0004) and throw. That is the uncaught exception above.
  • packages/core-ts/src/store.ts, SiteGraphStore.init(): it calls migrate() with no retry at all.

Nothing is applied twice today. The primary-key row in the same atomic batch guarantees at most once. The bug is that losing the race surfaces as a failure.

Done when

  • N instances converge: N instances (or N concurrent migrate() calls) starting together on one empty store all finish with the same schema and _migrations. None throws, nothing crashes, and there is no restart loop.
  • Applied migrations are detected: a migration already applied, by an earlier run or by a peer during this one, is detected and not re-run. When a migration's batch fails, migrate() re-reads _migrations. If the name is there, a peer applied it, and it moves on. Otherwise it throws as today.
  • Only batch(): no interactive transactions (hard rule 4). At most once still rests on the _migrations primary key inside the atomic batch.
  • Tests:
    • The storage conformance suite (better-sqlite3 and Miniflare D1) runs several migrate() calls concurrently on one fresh database and checks they converge.
    • A test shows an already-applied migration is never re-run.
    • CI's cluster smoke (the two-instance Swarm and Helm smoke tests) logs no migration error.
  • Simplified callers: the retry-once wrappers in admin-worker.ts are no longer needed.

Found in: #308 (#306's fix), run 46188727714.

🤖 Generated with Claude Code

Activity

  1. added
    bug
    on Nov 28, 2026
  2. added a commit that references this issue on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions