Skip to content

fix(core): scope the migration lock to each database - #46866

Merged
kitlangton merged 1 commit into
v2from
migration-lock
Sep 2, 2026
Merged

kitlangton merged 1 commit into
v2from
migration-lock

Conversation

@kitlangton

Copy link
Copy Markdown
Contributor

Why

DatabaseMigration.apply serialized schema bootstrap behind a Semaphore created at module scope. That is one lock per JS isolate, not per database. On Cloudflare workerd many Durable Objects share an isolate, so every embedded OpenCode instance (Console's SlackSession, opencode-slack's SessionDO) waited on the same semaphore. When object A releases the permit, the waiter's fiber resumes inside A's dispatcher task, so object B's very next statement runs on B's own storage while the current actor is still A, and workerd rejects it with Cannot perform I/O on behalf of a different Durable Object (ActorCacheInterface). Reproduced downstream with 4 concurrent bootstraps: 3 fail; sequential bootstraps never contend. The shared lock also needlessly serialized bootstrap across unrelated databases everywhere.

What Changes

The lock moves out of DatabaseMigration.apply and into the Database layer, which is the only production caller (database.ts) and the only place that knows which database it is building. apply itself is no longer locked.

Before

DO A  build ─ pragmas ─ lock.acquire ─ SELECT sqlite_master ─ … ─ lock.release ─┐
DO B  build ─ pragmas ─ lock.acquire ─────────── waits ────────────────────────┘─ resumes in A's task
                                                                                  └─ native.sql.exec on B's storage
                                                                                     ✗ Cannot perform I/O on behalf of a different Durable Object

After

DO A  build ─ pragmas ─ lockA.acquire ─ SELECT sqlite_master ─ … ─ lockA.release   (lockA created in this build)
DO B  build ─ pragmas ─ lockB.acquire ─ SELECT sqlite_master ─ … ─ lockB.release   (lockB created in this build)
                                                                                    ✓ no cross-object waiter

Scope chosen, based on what the callers showed:

  • apply runs exactly once per Database.Service build, so the lock is created per build for layerFromClient (the workerd path: Database.configuredClient(sqliteLayer({ storage }))) and for :memory: databases, which are one database per connection.
  • Per-instance alone was not enough for file databases. Database.layer({ path }) can legitimately be built twice over the same file in one process (the existing serializes concurrent embedded initialization for one database path test), and without a shared lock both instances see an empty sqlite_master, both start bootstrap, and the second blocks on busy_timeout and then collides on the schema. File databases therefore share a lock keyed by the resolved filename. Different Durable Objects never take this path (the workerd adapter dies on filenames), so they can never share a lock.

No config option is added.

Scope

Embedded workerd hosts (Console agent, opencode-slack) are the affected consumers. Node/Bun behavior is unchanged apart from unrelated databases no longer serializing against each other. No config or public API change; DatabaseMigration.apply(db) keeps its signature.

Verification

cd packages/core
bun typecheck
bun test test/database-migration.test.ts test/v1-migration.test.ts test/session-store.test.ts
cd ../..
bunx oxlint packages/core/src/database/database.ts packages/core/src/database/migration.ts packages/core/test/database-migration.test.ts
bunx ast-grep scan -c script/ast-grep/sgconfig.yml packages/core/src/database/database.ts packages/core/src/database/migration.ts
bun run lint:effect-simplifications
bunx prettier --check packages/core/src/database/database.ts packages/core/src/database/migration.ts packages/core/test/database-migration.test.ts
  • bun typecheck: clean.
  • Tests: 44 pass, 0 fail across the three files.
  • New test bootstraps distinct databases without waiting on each other's lock: builds one Database.layerFromClient over a real in-memory SqlClient whose sqlite_master query parks on a Deferred (so it holds its lock), then builds a second layerFromClient over another in-memory client and asserts it completes while the first is still parked. Verified it fails against the previous module-scoped lock (the second build times out) and passes with this change.
  • Existing serializes concurrent embedded initialization for one database path still passes; removing the lock entirely was confirmed to make it fail, which is why file databases keep a shared per-path lock.
  • oxlint: 0 errors, 4 warnings, all pre-existing on v2. lint:effect-patterns has 27 pre-existing errors on v2, none in the changed files. Prettier clean.

The workerd cross-object hand-off itself cannot be asserted in this suite: it needs multiple Durable Objects in one workerd isolate. That case is reproduced downstream in the Console repository (SDK 0.0.0-dev-18852, Effect 4.0.0-rc.112, workerd 1.20260828.1).

@kitlangton
kitlangton merged commit bbc4431 into v2 Sep 2, 2026
14 of 15 checks passed
@kitlangton
kitlangton deleted the migration-lock branch September 2, 2026 17:49
jinhuang712 pushed a commit to jinhuang712/opencode that referenced this pull request Sep 26, 2026
@opencodejr opencodejr Bot mentioned this pull request Nov 1, 2026
3 of 6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant