Skip to content

Commit 67ff912

Browse files
yaacovcorcosclaude
andauthored
fix(shared): opening a database waits for another process's lock (#28288)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 742a3bc commit 67ff912

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎packages/shared/src/nodeSqliteClient.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import * as FileSystem from "effect/FileSystem";
99
import * as Path from "effect/Path";
1010
import * as SqlClient from "effect/sql/SqlClient";
1111
import * as Statement from "effect/sql/Statement";
12+
import { ChildProcess, ChildProcessSpawner } from "effect/process";
13+
import * as Stream from "effect/Stream";
1214
import type * as Tracer from "effect/Tracer";
1315
import { vi } from "vite-plus/test";
1416

@@ -192,6 +194,34 @@ it.effect("keeps another connection from committing between a transaction's read
192194
}).pipe(Effect.provide(NodeServices.layer)),
193195
);
194196

197+
it.effect("waits for another process's lock while the database opens", () =>
198+
Effect.gen(function* () {
199+
const fs = yield* FileSystem.FileSystem;
200+
const path = yield* Path.Path;
201+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-open-" });
202+
const filename = path.join(directory, "state.sqlite");
203+
const seed = new NodeSqlite.DatabaseSync(filename);
204+
seed.exec("PRAGMA journal_mode = WAL; CREATE TABLE entries(name TEXT NOT NULL)");
205+
seed.close();
206+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
207+
const holder = yield* spawner.spawn(
208+
ChildProcess.make(process.execPath, [
209+
"-e",
210+
`const db = new (require("node:sqlite").DatabaseSync)(process.argv[1]);
211+
db.exec("PRAGMA locking_mode = EXCLUSIVE; BEGIN EXCLUSIVE");
212+
console.log("held");
213+
setTimeout(() => db.close(), 300);`,
214+
filename,
215+
]),
216+
);
217+
yield* Stream.runHead(holder.stdout);
218+
yield* Effect.gen(function* () {
219+
const sql = yield* SqlClient.SqlClient;
220+
assert.deepEqual(yield* sql`SELECT COUNT(*) FROM entries`.values, [[0]]);
221+
}).pipe(Effect.provide(SqliteClient.layer({ filename, checkpointWorker: true })));
222+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
223+
);
224+
195225
it.effect("reports a transaction blocked by another writer as a lock timeout", () =>
196226
Effect.gen(function* () {
197227
const { filename, other } = yield* makeTempDatabase;

‎packages/shared/src/nodeSqliteClient.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ const startCheckpointWorker = Effect.fnUntraced(function* (
629629
yield* warn("Node SQLite lacks transaction-state or statement-authorizer support");
630630
return undefined;
631631
}
632+
// These are the writer's first statements, before callers set their own wait.
633+
writer.exec("PRAGMA busy_timeout = 5000;");
632634
const version = String(writer.prepare("SELECT sqlite_version() AS version").get()?.version);
633635
const [major = 0, minor = 0] = version.split(".").map(Number);
634636
// Older SQLite treats NOOP as PASSIVE, which would checkpoint on the writer.

0 commit comments

Comments
 (0)