Repository navigation
fix(core): scope the migration lock to each database - #46866
Merged
Merged
Conversation
jinhuang712
pushed a commit
to jinhuang712/opencode
that referenced
this pull request
Sep 26, 2026
3 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
DatabaseMigration.applyserialized schema bootstrap behind aSemaphorecreated 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'sSlackSession, opencode-slack'sSessionDO) 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 withCannot 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.applyand into theDatabaselayer, which is the only production caller (database.ts) and the only place that knows which database it is building.applyitself is no longer locked.Before
After
Scope chosen, based on what the callers showed:
applyruns exactly once perDatabase.Servicebuild, so the lock is created per build forlayerFromClient(the workerd path:Database.configuredClient(sqliteLayer({ storage }))) and for:memory:databases, which are one database per connection.Database.layer({ path })can legitimately be built twice over the same file in one process (the existingserializes concurrent embedded initialization for one database pathtest), and without a shared lock both instances see an emptysqlite_master, both start bootstrap, and the second blocks onbusy_timeoutand 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
bun typecheck: clean.bootstraps distinct databases without waiting on each other's lock: builds oneDatabase.layerFromClientover a real in-memorySqlClientwhosesqlite_masterquery parks on aDeferred(so it holds its lock), then builds a secondlayerFromClientover 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.serializes concurrent embedded initialization for one database pathstill passes; removing the lock entirely was confirmed to make it fail, which is why file databases keep a shared per-path lock.v2.lint:effect-patternshas 27 pre-existing errors onv2, 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, Effect4.0.0-rc.112, workerd1.20260828.1).