fix(server): sqlite transactions wait for the write lock instead of failing (#15488)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Opus 5.5 authored and GitHub committed 2026-10-03 23:35:32 -07:00
1 parent 40596072eb
commit 0fe4fa40fe
2 files changed
+114 -24

No files matched your search

@@ -61,8 +61,88 @@ layer("NodeSqliteClient", (it) => {
assert.equal(error.reason.operation, "prepare");
}),
);
it.effect("classifies constraint failures by their SQLite result code", () =>
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`CREATE TABLE constrained(name TEXT NOT NULL UNIQUE)`;
yield* sql`INSERT INTO constrained VALUES ('taken')`;
const duplicate = yield* sql`INSERT INTO constrained VALUES ('taken')`.pipe(Effect.flip);
assert(duplicate.reason._tag === "UniqueViolation");
assert.equal(duplicate.reason.constraint, "constrained.name");
const missing = yield* sql`INSERT INTO constrained VALUES (NULL)`.pipe(Effect.flip);
assert.equal(missing.reason._tag, "ConstraintError");
}),
);
});
const makeTempDatabase = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-transaction-" });
const filename = path.join(directory, "state.sqlite");
// node:sqlite connections fail a busy statement at once unless given a timeout.
const other = yield* Effect.acquireRelease(
Effect.sync(() => new NodeSqlite.DatabaseSync(filename)),
(database) => Effect.sync(() => database.close()),
);
yield* Effect.sync(() => {
other.exec(`
PRAGMA journal_mode = WAL;
CREATE TABLE counters(id INTEGER PRIMARY KEY, value INTEGER NOT NULL);
INSERT INTO counters VALUES (1, 0);
`);
});
return { filename, other };
});
it.effect("keeps another connection from committing between a transaction's read and write", () =>
Effect.gen(function* () {
const { filename, other } = yield* makeTempDatabase;
yield* Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql.withTransaction(
Effect.gen(function* () {
const [row] = yield* sql<{ readonly value: number }>`
SELECT value FROM counters WHERE id = 1
`;
// With a deferred BEGIN this commit lands and the write below fails
// with SQLITE_BUSY_SNAPSHOT, which no busy timeout can wait out.
yield* Effect.sync(() =>
assert.throws(
() => other.exec("UPDATE counters SET value = value + 1 WHERE id = 1"),
/database is locked/,
),
);
yield* sql`UPDATE counters SET value = ${(row?.value ?? 0) + 10} WHERE id = 1`;
}),
);
yield* Effect.sync(() => other.exec("UPDATE counters SET value = value + 1 WHERE id = 1"));
assert.deepEqual(yield* sql`SELECT value FROM counters WHERE id = 1`.values, [[11]]);
}).pipe(Effect.provide(SqliteClient.layer({ filename })));
}).pipe(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;
yield* Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`PRAGMA busy_timeout = 0`;
const read = sql.withTransaction(sql`SELECT value FROM counters WHERE id = 1`.values);
yield* Effect.sync(() => other.exec("BEGIN IMMEDIATE"));
const error = yield* read.pipe(Effect.flip);
assert.equal(error.reason._tag, "LockTimeoutError");
yield* Effect.sync(() => other.exec("COMMIT"));
assert.deepEqual(yield* read, [[0]]);
}).pipe(Effect.provide(SqliteClient.layer({ filename })));
}).pipe(Effect.provide(NodeServices.layer)),
);
it.effect("returns a typed failure when the database cannot be opened", () =>
Effect.gen(function* () {
const error = yield* Effect.flip(