Skip to content
TypeScript

fix(shared): opening a database waits for another process's lock (#28288)

yaacovcorcos67ff912ccdb1
diff --git a/packages/shared/src/nodeSqliteClient.test.ts b/packages/shared/src/nodeSqliteClient.test.ts
index ac786993fa0..32b6e8fdf15 100644
--- a/packages/shared/src/nodeSqliteClient.test.ts
+++ b/packages/shared/src/nodeSqliteClient.test.ts
@@ -9,6 +9,8 @@ import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as SqlClient from "effect/sql/SqlClient";
import * as Statement from "effect/sql/Statement";
+import { ChildProcess, ChildProcessSpawner } from "effect/process";
+import * as Stream from "effect/Stream";
import type * as Tracer from "effect/Tracer";
import { vi } from "vite-plus/test";
@@ -192,6 +194,34 @@ it.effect("keeps another connection from committing between a transaction's read
}).pipe(Effect.provide(NodeServices.layer)),
);
+it.effect("waits for another process's lock while the database opens", () =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-open-" });
+ const filename = path.join(directory, "state.sqlite");
+ const seed = new NodeSqlite.DatabaseSync(filename);
+ seed.exec("PRAGMA journal_mode = WAL; CREATE TABLE entries(name TEXT NOT NULL)");
+ seed.close();
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
+ const holder = yield* spawner.spawn(
+ ChildProcess.make(process.execPath, [
+ "-e",
+ `const db = new (require("node:sqlite").DatabaseSync)(process.argv[1]);
+ db.exec("PRAGMA locking_mode = EXCLUSIVE; BEGIN EXCLUSIVE");
+ console.log("held");
+ setTimeout(() => db.close(), 300);`,
+ filename,
+ ]),
+ );
+ yield* Stream.runHead(holder.stdout);
+ yield* Effect.gen(function* () {
+ const sql = yield* SqlClient.SqlClient;
+ assert.deepEqual(yield* sql`SELECT COUNT(*) FROM entries`.values, [[0]]);
+ }).pipe(Effect.provide(SqliteClient.layer({ filename, checkpointWorker: true })));
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
+);
+
it.effect("reports a transaction blocked by another writer as a lock timeout", () =>
Effect.gen(function* () {
const { filename, other } = yield* makeTempDatabase;
diff --git a/packages/shared/src/nodeSqliteClient.ts b/packages/shared/src/nodeSqliteClient.ts
index c0edfdb868d..3190afbc792 100644
--- a/packages/shared/src/nodeSqliteClient.ts
+++ b/packages/shared/src/nodeSqliteClient.ts
@@ -629,6 +629,8 @@ const startCheckpointWorker = Effect.fnUntraced(function* (
yield* warn("Node SQLite lacks transaction-state or statement-authorizer support");
return undefined;
}
+ // These are the writer's first statements, before callers set their own wait.
+ writer.exec("PRAGMA busy_timeout = 5000;");
const version = String(writer.prepare("SELECT sqlite_version() AS version").get()?.version);
const [major = 0, minor = 0] = version.split(".").map(Number);
// Older SQLite treats NOOP as PASSIVE, which would checkpoint on the writer.