Skip to content

Commit 268cbd3

Browse files
anandgupta42claude
andauthored
fix: avoid "database is locked" when several CLI processes start on a fresh data dir (#1541)
* fix(storage): serialize first-run database setup across concurrent processes Concurrent fresh starts against one data directory failed with "database is locked". The core `sqlite.bun.ts` opener ran `PRAGMA journal_mode = WAL` before any busy handler existed, and both the legacy `Database.Client` and the core `Database` layer ran migrations whose deferred read-to-write upgrade fails immediately with `SQLITE_BUSY` instead of waiting. - Install `busy_timeout` before the WAL switch in both openers. - Add `setup-lock.ts`: an exclusive transaction on a sentinel database is a cross-process mutex that the OS releases if the holder dies. Synchronous sections block on it; the effect-based migration polls so in-process peers keep progressing. - Add a regression test that opens both database layers from 4 simultaneous processes against a fresh data directory. Co-Authored-By: Claude Opus 5.5 <[email protected]> * 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]> * fix(storage): mark the extracted open() as a fork change and isolate the alias env in the concurrent-open test Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(storage): make read-only Bun opens actually read-only Bun's `Database` honours `readwrite`/`create` over `readonly`, so the default `readwrite: true, create: true` let a `readonly: true` open write and create a missing file. Disable both when `readonly` is set; the WAL/setup-lock skip for read-only opens now matches what the connection can do. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 4fc9fb0 commit 268cbd3

8 files changed

Lines changed: 300 additions & 23 deletions

File tree

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ export * as Database from "./database"
22

33
import { EffectDrizzleSqlite } from "@opencode-ai/effect-drizzle-sqlite"
44
import { layer as sqliteLayer } from "#sqlite"
5+
// altimate_change start — upstream_fix: cross-process first-run setup lock
6+
import * as SetupLock from "./setup-lock"
7+
// altimate_change end
58
import { Context, Effect, Layer } from "effect"
69
import { Global } from "../global"
710
import { Flag } from "../flag/flag"
@@ -19,26 +22,32 @@ export interface Interface {
1922

2023
export class Service extends Context.Service<Service, Interface>()("@opencode/v2/storage/Database") {}
2124

22-
export const layer = Layer.effect(
23-
Service,
24-
Effect.gen(function* () {
25-
const db = yield* makeDatabase
25+
// altimate_change start — upstream_fix: `layer` takes the database filename so first-run setup can be serialized
26+
export const layer = (filename?: string) =>
27+
Layer.effect(
28+
Service,
29+
Effect.gen(function* () {
30+
const db = yield* makeDatabase
2631

27-
yield* db.run("PRAGMA journal_mode = WAL")
28-
yield* db.run("PRAGMA synchronous = NORMAL")
29-
yield* db.run("PRAGMA busy_timeout = 5000")
30-
yield* db.run("PRAGMA cache_size = -64000")
31-
yield* db.run("PRAGMA foreign_keys = ON")
32-
yield* db.run("PRAGMA wal_checkpoint(PASSIVE)")
33-
yield* DatabaseMigration.apply(db)
32+
yield* db.run("PRAGMA busy_timeout = 5000")
33+
const setup = Effect.gen(function* () {
34+
yield* db.run("PRAGMA journal_mode = WAL")
35+
yield* db.run("PRAGMA synchronous = NORMAL")
36+
yield* db.run("PRAGMA cache_size = -64000")
37+
yield* db.run("PRAGMA foreign_keys = ON")
38+
yield* db.run("PRAGMA wal_checkpoint(PASSIVE)")
39+
yield* DatabaseMigration.apply(db)
40+
})
41+
yield* filename ? SetupLock.hold(filename, setup) : setup
3442

35-
return { db }
36-
}).pipe(Effect.orDie),
37-
)
43+
return { db }
44+
}).pipe(Effect.orDie),
45+
)
3846

3947
export function layerFromPath(filename: string) {
40-
return layer.pipe(Layer.provide(sqliteLayer({ filename })))
48+
return layer(filename).pipe(Layer.provide(sqliteLayer({ filename })))
4149
}
50+
// altimate_change end
4251

4352
export function path() {
4453
if (Flag.OPENCODE_DB) {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { Effect, Semaphore } from "effect"
2+
import { openSetupLock } from "#sqlite"
3+
4+
/**
5+
* Cross-process mutex for first-run database setup (WAL switch and migrations).
6+
*
7+
* SQLite cannot queue a deferred read-to-write upgrade, so concurrent fresh starts failed at once with
8+
* "database is locked". An exclusive transaction on a sentinel database next to the real one serializes
9+
* them, and the OS releases it if the holder dies.
10+
*
11+
* `acquireSync` blocks the thread, so it is only for sections that never yield. A section that yields
12+
* (the effect-based migration) uses `hold`, which polls without blocking. A synchronous caller in the
13+
* same process as an active `hold` skips the lock: blocking would deadlock the holder, and connections
14+
* in one process already queue through their own busy handlers.
15+
*/
16+
const semaphores = new Map<string, Semaphore.Semaphore>()
17+
const active = new Set<string>()
18+
19+
const sentinel = (filename: string) => filename + ".setup-lock"
20+
const SYNC_TIMEOUT_MS = 60_000
21+
const POLL_MS = 25
22+
const HOLD_TIMEOUT_MS = 60_000
23+
24+
export function acquireSync(filename: string): () => void {
25+
if (filename === ":memory:" || active.has(filename)) return () => {}
26+
const lock = openSetupLock(sentinel(filename), SYNC_TIMEOUT_MS)
27+
try {
28+
lock.exec("BEGIN EXCLUSIVE")
29+
} catch (error) {
30+
lock.close()
31+
throw error
32+
}
33+
return () => {
34+
try {
35+
lock.exec("ROLLBACK")
36+
} finally {
37+
lock.close()
38+
}
39+
}
40+
}
41+
42+
function isBusy(error: unknown) {
43+
return /locked|busy/i.test(error instanceof Error ? error.message : String(error))
44+
}
45+
46+
/** Runs `body` holding the lock; waiting is async so in-process peers keep making progress. */
47+
export function hold<A, E, R>(filename: string, body: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> {
48+
if (filename === ":memory:") return body
49+
let semaphore = semaphores.get(filename)
50+
if (!semaphore) {
51+
semaphore = Semaphore.makeUnsafe(1)
52+
semaphores.set(filename, semaphore)
53+
}
54+
const acquire = Effect.gen(function* () {
55+
const started = Date.now()
56+
while (true) {
57+
const lock = openSetupLock(sentinel(filename), 0)
58+
const result = yield* Effect.try({ try: () => lock.exec("BEGIN EXCLUSIVE"), catch: (error) => error }).pipe(
59+
Effect.map(() => lock),
60+
Effect.catch((error) =>
61+
Effect.sync(() => lock.close()).pipe(
62+
Effect.flatMap(() =>
63+
isBusy(error) && Date.now() - started < HOLD_TIMEOUT_MS ? Effect.succeed(undefined) : Effect.die(error),
64+
),
65+
),
66+
),
67+
)
68+
if (result) {
69+
active.add(filename)
70+
return result
71+
}
72+
// `acquireUseRelease` makes `acquire` uninterruptible; no handle is open while sleeping, so cancel here.
73+
yield* Effect.interruptible(Effect.sleep(POLL_MS))
74+
}
75+
})
76+
const release = (lock: { exec: (sql: string) => void; close: () => void }) =>
77+
Effect.sync(() => {
78+
active.delete(filename)
79+
try {
80+
lock.exec("ROLLBACK")
81+
} finally {
82+
lock.close()
83+
}
84+
})
85+
return semaphore.withPermits(1)(Effect.acquireUseRelease(acquire, () => body, release))
86+
}

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import type { Connection } from "effect/unstable/sql/SqlConnection"
1414
import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError"
1515
import * as Statement from "effect/unstable/sql/Statement"
1616
import { Sqlite } from "./sqlite"
17+
// altimate_change start — upstream_fix: cross-process first-run setup lock
18+
import * as SetupLock from "./setup-lock"
19+
// altimate_change end
1720

1821
const ATTR_DB_SYSTEM_NAME = "db.system.name"
1922

@@ -155,13 +158,27 @@ const nativeLayer = (config: Config) =>
155158
Layer.effect(
156159
Sqlite.Native,
157160
Effect.gen(function* () {
161+
// altimate_change start — upstream_fix: Bun honours readwrite/create over readonly, so a read-only open
162+
// with the defaults could still write; read-only now disables both.
158163
const native = new Database(config.filename, {
159164
readonly: config.readonly,
160-
readwrite: config.readwrite ?? true,
161-
create: config.create ?? true,
165+
readwrite: config.readonly === true ? false : (config.readwrite ?? true),
166+
create: config.readonly === true ? false : (config.create ?? true),
162167
})
168+
// altimate_change end
163169
yield* Effect.addFinalizer(() => Effect.sync(() => native.close()))
164-
if (config.disableWAL !== true) native.run("PRAGMA journal_mode = WAL;")
170+
// altimate_change start — upstream_fix: the WAL switch needs a write lock and can fail at once with
171+
// SQLITE_BUSY, so install the busy handler first and serialize the switch across processes.
172+
native.run("PRAGMA busy_timeout = 5000;")
173+
if (config.disableWAL !== true && config.readonly !== true) {
174+
const release = SetupLock.acquireSync(config.filename)
175+
try {
176+
native.run("PRAGMA journal_mode = WAL;")
177+
} finally {
178+
release()
179+
}
180+
}
181+
// altimate_change end
165182
return native
166183
}),
167184
)
@@ -181,3 +198,11 @@ export const layer = (config: Config) => {
181198
Layer.provide(Reactivity.layer),
182199
)
183200
}
201+
202+
// altimate_change start — upstream_fix: sentinel database used as a cross-process mutex (see setup-lock.ts)
203+
export function openSetupLock(filename: string, timeoutMs: number) {
204+
const lock = new Database(filename, { create: true })
205+
lock.run(`PRAGMA busy_timeout = ${timeoutMs}`)
206+
return { exec: (sql: string) => void lock.run(sql), close: () => lock.close() }
207+
}
208+
// altimate_change end

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import type { Connection } from "effect/unstable/sql/SqlConnection"
1414
import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError"
1515
import * as Statement from "effect/unstable/sql/Statement"
1616
import { Sqlite } from "./sqlite"
17+
// altimate_change start — upstream_fix: cross-process first-run setup lock
18+
import * as SetupLock from "./setup-lock"
19+
// altimate_change end
1720

1821
const ATTR_DB_SYSTEM_NAME = "db.system.name"
1922

@@ -156,7 +159,18 @@ const nativeLayer = (config: Config) =>
156159
open: true,
157160
})
158161
yield* Effect.addFinalizer(() => Effect.sync(() => native.close()))
159-
if (config.disableWAL !== true && config.readonly !== true) native.exec("PRAGMA journal_mode = WAL;")
162+
// altimate_change start — upstream_fix: the WAL switch needs a write lock and can fail at once with
163+
// SQLITE_BUSY, so install the busy handler first and serialize the switch across processes.
164+
native.exec(`PRAGMA busy_timeout = ${config.timeout ?? 5000};`)
165+
if (config.disableWAL !== true && config.readonly !== true) {
166+
const release = SetupLock.acquireSync(config.filename)
167+
try {
168+
native.exec("PRAGMA journal_mode = WAL;")
169+
} finally {
170+
release()
171+
}
172+
}
173+
// altimate_change end
160174
return native
161175
}),
162176
)
@@ -176,3 +190,11 @@ export const layer = (config: Config) => {
176190
Layer.provide(Reactivity.layer),
177191
)
178192
}
193+
194+
// altimate_change start — upstream_fix: sentinel database used as a cross-process mutex (see setup-lock.ts)
195+
export function openSetupLock(filename: string, timeoutMs: number) {
196+
const lock = new DatabaseSync(filename, { timeout: timeoutMs })
197+
lock.exec(`PRAGMA busy_timeout = ${timeoutMs}`)
198+
return { exec: (sql: string) => void lock.exec(sql), close: () => lock.close() }
199+
}
200+
// altimate_change end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { Database as BunDatabase } from "bun:sqlite"
3+
import path from "path"
4+
import { Effect } from "effect"
5+
import { layer } from "../src/database/sqlite.bun"
6+
import { Sqlite } from "../src/database/sqlite"
7+
import { tmpdir } from "./fixture/tmpdir"
8+
9+
describe("sqlite.bun read-only open", () => {
10+
test("rejects writes and leaves the journal mode alone", async () => {
11+
await using tmp = await tmpdir()
12+
const filename = path.join(tmp.path, "ro.sqlite")
13+
const seed = new BunDatabase(filename, { create: true })
14+
seed.run("CREATE TABLE t (x INTEGER)")
15+
seed.close()
16+
17+
const result = await Effect.runPromise(
18+
Effect.gen(function* () {
19+
const native = (yield* Sqlite.Native) as BunDatabase
20+
const mode = native.query("PRAGMA journal_mode").get() as { journal_mode: string }
21+
const write = (() => {
22+
try {
23+
native.run("INSERT INTO t VALUES (1)")
24+
return "written"
25+
} catch (error) {
26+
return (error as Error).message
27+
}
28+
})()
29+
return { mode: mode.journal_mode, write }
30+
}).pipe(Effect.provide(layer({ filename, readonly: true })), Effect.scoped),
31+
)
32+
33+
expect(result.mode).toBe("delete")
34+
expect(result.write).toContain("readonly")
35+
})
36+
37+
test("does not create a missing file", async () => {
38+
await using tmp = await tmpdir()
39+
const filename = path.join(tmp.path, "missing.sqlite")
40+
const exit = await Effect.runPromiseExit(
41+
Effect.gen(function* () {
42+
yield* Sqlite.Native
43+
}).pipe(Effect.provide(layer({ filename, readonly: true })), Effect.scoped),
44+
)
45+
expect(exit._tag).toBe("Failure")
46+
expect(await Bun.file(filename).exists()).toBe(false)
47+
})
48+
})

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { readFileSync, readdirSync, existsSync } from "fs"
1414
import * as schema from "./schema"
1515
import { InstallationChannel } from "@opencode-ai/core/installation/version"
1616
import { Flag } from "../flag/flag"
17+
// altimate_change start — upstream_fix: cross-process first-run setup lock
18+
import * as SetupLock from "@opencode-ai/core/database/setup-lock"
19+
// altimate_change end
1720
import { iife } from "@/util/iife"
1821
import { Effect } from "effect"
1922
import freshSchema from "@opencode-ai/core/database/schema.gen"
@@ -394,13 +397,28 @@ export namespace Database {
394397

395398
export const Client = lazy(() => {
396399
log.info("opening database", { path: Path })
400+
// altimate_change start — upstream_fix: serialize first-run setup across processes (see core setup-lock.ts)
401+
const releaseSetupLock = SetupLock.acquireSync(Path)
402+
try {
403+
return open()
404+
} finally {
405+
releaseSetupLock()
406+
}
407+
// altimate_change end
408+
})
397409

410+
// altimate_change start — upstream_fix: setup extracted from the lazy Client so it runs under the setup lock
411+
function open() {
398412
const sqlite = new BunDatabase(Path, { create: true })
399413
state.sqlite = sqlite
400414

415+
// altimate_change start — upstream_fix: install the busy handler before the first statement that
416+
// takes a lock. Switching a fresh file to WAL needs an exclusive lock, so concurrent first-run
417+
// processes failed immediately with "database is locked" when this came after journal_mode.
418+
sqlite.run("PRAGMA busy_timeout = 5000")
419+
// altimate_change end
401420
sqlite.run("PRAGMA journal_mode = WAL")
402421
sqlite.run("PRAGMA synchronous = NORMAL")
403-
sqlite.run("PRAGMA busy_timeout = 5000")
404422
sqlite.run("PRAGMA cache_size = -64000")
405423
sqlite.run("PRAGMA foreign_keys = ON")
406424
sqlite.run("PRAGMA wal_checkpoint(PASSIVE)")
@@ -422,8 +440,17 @@ export namespace Database {
422440
item.sql = "select 1;"
423441
}
424442
}
425-
const adopted = adoptCoreOwnedDatabase(sqlite, entries)
426-
const initialized = adopted ? false : initializeFreshDatabase(sqlite, entries)
443+
// altimate_change start — upstream_fix: the schema-state checks share the write transaction, so a
444+
// same-process core migration cannot create the schema between a check and the CREATE TABLEs
445+
let adopted = false
446+
let initialized = false
447+
sqlite
448+
.transaction(() => {
449+
adopted = adoptCoreOwnedDatabase(sqlite, entries)
450+
initialized = adopted ? false : initializeFreshDatabase(sqlite, entries)
451+
})
452+
.immediate()
453+
// altimate_change end
427454
const repaired = adopted || initialized ? false : repairLegacyDatabase(sqlite, entries)
428455
// altimate_change start — backfill migration names before migrate
429456
backfillMigrationNames(sqlite, entries)
@@ -446,7 +473,8 @@ export namespace Database {
446473
}
447474

448475
return db
449-
})
476+
}
477+
// altimate_change end
450478

451479
export function close() {
452480
const sqlite = state.sqlite
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, expect, test } from "bun:test"
2+
import fs from "fs/promises"
3+
import os from "os"
4+
import path from "path"
5+
6+
const fixture = path.join(import.meta.dir, "fixture", "open-database.ts")
7+
8+
async function launch(data: string) {
9+
const proc = Bun.spawn([process.execPath, "run", fixture], {
10+
cwd: path.join(import.meta.dir, "..", ".."),
11+
env: { ...process.env, XDG_DATA_HOME: data, OPENCODE_DB: "", ALTIMATE_CLI_DB: "" },
12+
stdout: "pipe",
13+
stderr: "pipe",
14+
})
15+
const [stdout, stderr, code] = await Promise.all([
16+
new Response(proc.stdout).text(),
17+
new Response(proc.stderr).text(),
18+
proc.exited,
19+
])
20+
return { stdout, stderr, code }
21+
}
22+
23+
describe("concurrent first-run database setup", () => {
24+
test("simultaneous fresh processes never fail with SQLITE_BUSY", async () => {
25+
// Fresh data directories exercise the WAL switch and migrations, where concurrent starts raced.
26+
for (let round = 0; round < 3; round++) {
27+
const data = await fs.mkdtemp(path.join(os.tmpdir(), "altimate-concurrent-db-"))
28+
try {
29+
const results = await Promise.all(Array.from({ length: 4 }, () => launch(data)))
30+
for (const result of results) {
31+
expect(result.stdout + result.stderr).not.toMatch(/database is locked|SQLITE_BUSY/i)
32+
expect(result.code).toBe(0)
33+
expect(result.stdout).toContain("opened")
34+
}
35+
} finally {
36+
await fs.rm(data, { recursive: true, force: true })
37+
}
38+
}
39+
}, 120_000)
40+
})

0 commit comments

Comments
 (0)