Skip to content

Commit 741e197

Browse files
anandgupta42claude
andcommitted
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]>
1 parent 19f909c commit 741e197

7 files changed

Lines changed: 232 additions & 19 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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
yield* Effect.sleep(POLL_MS)
73+
}
74+
})
75+
const release = (lock: { exec: (sql: string) => void; close: () => void }) =>
76+
Effect.sync(() => {
77+
active.delete(filename)
78+
try {
79+
lock.exec("ROLLBACK")
80+
} finally {
81+
lock.close()
82+
}
83+
})
84+
return semaphore.withPermits(1)(Effect.acquireUseRelease(acquire, () => body, release))
85+
}

‎packages/core/src/database/sqlite.bun.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

@@ -161,7 +164,18 @@ const nativeLayer = (config: Config) =>
161164
create: config.create ?? true,
162165
})
163166
yield* Effect.addFinalizer(() => Effect.sync(() => native.close()))
164-
if (config.disableWAL !== true) native.run("PRAGMA journal_mode = WAL;")
167+
// altimate_change start — upstream_fix: the WAL switch needs a write lock and can fail at once with
168+
// SQLITE_BUSY, so install the busy handler first and serialize the switch across processes.
169+
native.run("PRAGMA busy_timeout = 5000;")
170+
if (config.disableWAL !== true) {
171+
const release = SetupLock.acquireSync(config.filename)
172+
try {
173+
native.run("PRAGMA journal_mode = WAL;")
174+
} finally {
175+
release()
176+
}
177+
}
178+
// altimate_change end
165179
return native
166180
}),
167181
)
@@ -181,3 +195,11 @@ export const layer = (config: Config) => {
181195
Layer.provide(Reactivity.layer),
182196
)
183197
}
198+
199+
// altimate_change start — upstream_fix: sentinel database used as a cross-process mutex (see setup-lock.ts)
200+
export function openSetupLock(filename: string, timeoutMs: number) {
201+
const lock = new Database(filename, { create: true })
202+
lock.run(`PRAGMA busy_timeout = ${timeoutMs}`)
203+
return { exec: (sql: string) => void lock.run(sql), close: () => lock.close() }
204+
}
205+
// altimate_change end

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

Lines changed: 22 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 = 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,10 @@ 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+
return { exec: (sql: string) => void lock.exec(sql), close: () => lock.close() }
198+
}
199+
// altimate_change end

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

Lines changed: 19 additions & 2 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,27 @@ 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+
function open() {
398411
const sqlite = new BunDatabase(Path, { create: true })
399412
state.sqlite = sqlite
400413

414+
// altimate_change start — upstream_fix: install the busy handler before the first statement that
415+
// takes a lock. Switching a fresh file to WAL needs an exclusive lock, so concurrent first-run
416+
// processes failed immediately with "database is locked" when this came after journal_mode.
417+
sqlite.run("PRAGMA busy_timeout = 5000")
418+
// altimate_change end
401419
sqlite.run("PRAGMA journal_mode = WAL")
402420
sqlite.run("PRAGMA synchronous = NORMAL")
403-
sqlite.run("PRAGMA busy_timeout = 5000")
404421
sqlite.run("PRAGMA cache_size = -64000")
405422
sqlite.run("PRAGMA foreign_keys = ON")
406423
sqlite.run("PRAGMA wal_checkpoint(PASSIVE)")
@@ -446,7 +463,7 @@ export namespace Database {
446463
}
447464

448465
return db
449-
})
466+
}
450467

451468
export function close() {
452469
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(["bun", "run", fixture], {
10+
cwd: path.join(import.meta.dir, "..", ".."),
11+
env: { ...process.env, XDG_DATA_HOME: data, OPENCODE_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+
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Child process for concurrent-open.test.ts: opens both database layers against the shared data directory.
2+
import { Effect } from "effect"
3+
import { Database } from "../../../src/storage/db"
4+
import { Database as CoreDatabase } from "@opencode-ai/core/database/database"
5+
6+
try {
7+
Database.Client()
8+
await Effect.runPromise(
9+
Effect.scoped(
10+
Effect.gen(function* () {
11+
yield* CoreDatabase.Service
12+
}).pipe(Effect.provide(CoreDatabase.layerFromPath(Database.Path))),
13+
),
14+
)
15+
console.log("opened")
16+
} catch (error) {
17+
console.log("FAILED: " + (error instanceof Error ? error.message : String(error)))
18+
process.exitCode = 1
19+
}

0 commit comments

Comments
 (0)