Skip to content

Commit 31d9ea1

Browse files
anandgupta42claude
andcommitted
fix(storage): address review findings on the first-run setup lock
- Run the legacy adopt/initialize schema checks inside one `BEGIN IMMEDIATE` transaction so a same-process core migration cannot create the schema between a check and the `CREATE TABLE`s. - Make the setup-lock polling interruptible so cancelling startup does not wait for the sentinel lock. - Use the configured `timeout` for `PRAGMA busy_timeout` in the Node opener and skip the WAL switch and lock for read-only Bun opens. - Spawn the regression fixture with `process.execPath`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 741e197 commit 31d9ea1

5 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎packages/core/src/database/setup-lock.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ export function hold<A, E, R>(filename: string, body: Effect.Effect<A, E, R>): E
6969
active.add(filename)
7070
return result
7171
}
72-
yield* Effect.sleep(POLL_MS)
72+
// `acquireUseRelease` makes `acquire` uninterruptible; no handle is open while sleeping, so cancel here.
73+
yield* Effect.interruptible(Effect.sleep(POLL_MS))
7374
}
7475
})
7576
const release = (lock: { exec: (sql: string) => void; close: () => void }) =>

‎packages/core/src/database/sqlite.bun.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ const nativeLayer = (config: Config) =>
167167
// altimate_change start — upstream_fix: the WAL switch needs a write lock and can fail at once with
168168
// SQLITE_BUSY, so install the busy handler first and serialize the switch across processes.
169169
native.run("PRAGMA busy_timeout = 5000;")
170-
if (config.disableWAL !== true) {
170+
if (config.disableWAL !== true && config.readonly !== true) {
171171
const release = SetupLock.acquireSync(config.filename)
172172
try {
173173
native.run("PRAGMA journal_mode = WAL;")

‎packages/core/src/database/sqlite.node.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ const nativeLayer = (config: Config) =>
161161
yield* Effect.addFinalizer(() => Effect.sync(() => native.close()))
162162
// altimate_change start — upstream_fix: the WAL switch needs a write lock and can fail at once with
163163
// SQLITE_BUSY, so install the busy handler first and serialize the switch across processes.
164-
native.exec("PRAGMA busy_timeout = 5000;")
164+
native.exec(`PRAGMA busy_timeout = ${config.timeout ?? 5000};`)
165165
if (config.disableWAL !== true && config.readonly !== true) {
166166
const release = SetupLock.acquireSync(config.filename)
167167
try {
@@ -194,6 +194,7 @@ export const layer = (config: Config) => {
194194
// altimate_change start — upstream_fix: sentinel database used as a cross-process mutex (see setup-lock.ts)
195195
export function openSetupLock(filename: string, timeoutMs: number) {
196196
const lock = new DatabaseSync(filename, { timeout: timeoutMs })
197+
lock.exec(`PRAGMA busy_timeout = ${timeoutMs}`)
197198
return { exec: (sql: string) => void lock.exec(sql), close: () => lock.close() }
198199
}
199200
// altimate_change end

‎packages/opencode/src/storage/db.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,17 @@ export namespace Database {
439439
item.sql = "select 1;"
440440
}
441441
}
442-
const adopted = adoptCoreOwnedDatabase(sqlite, entries)
443-
const initialized = adopted ? false : initializeFreshDatabase(sqlite, entries)
442+
// altimate_change start — upstream_fix: the schema-state checks share the write transaction, so a
443+
// same-process core migration cannot create the schema between a check and the CREATE TABLEs
444+
let adopted = false
445+
let initialized = false
446+
sqlite
447+
.transaction(() => {
448+
adopted = adoptCoreOwnedDatabase(sqlite, entries)
449+
initialized = adopted ? false : initializeFreshDatabase(sqlite, entries)
450+
})
451+
.immediate()
452+
// altimate_change end
444453
const repaired = adopted || initialized ? false : repairLegacyDatabase(sqlite, entries)
445454
// altimate_change start — backfill migration names before migrate
446455
backfillMigrationNames(sqlite, entries)

‎packages/opencode/test/storage/concurrent-open.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import path from "path"
66
const fixture = path.join(import.meta.dir, "fixture", "open-database.ts")
77

88
async function launch(data: string) {
9-
const proc = Bun.spawn(["bun", "run", fixture], {
9+
const proc = Bun.spawn([process.execPath, "run", fixture], {
1010
cwd: path.join(import.meta.dir, "..", ".."),
1111
env: { ...process.env, XDG_DATA_HOME: data, OPENCODE_DB: "" },
1212
stdout: "pipe",

0 commit comments

Comments
 (0)