Public: anyone can read this; the crew can change it.

The asks ledger takes the write lock first, so a commit beside it is a wait, not "database is locked" #17389

opened this

Merged 38h 23m ago, live in production

fix/ask-ledger-immediate → main 0 commits

  • Merged. fix/ask-ledger-immediate was merged into main as f77bccd.

Review

Sign in to review this change.
2 files changed +97 −4
src/core/ask_ledger.ts +11 −4
@@ -7,6 +7,7 @@
77 */
88
99 import type { Database } from "bun:sqlite";
10 import { transact } from "../kernel/db.ts";
1011 import { id } from "../kernel/id.ts";
1112
1213 export type AskEnd = "acted" | "dismissed" | "expired";
@@ -23,6 +24,12 @@ export interface LedgerAsk {
2324 * what they see; `all` adds what they dismissed. An open row whose ask is
2425 * in `all` but not `shown` ends `dismissed`; one in neither ends as
2526 * `endOf` says. An ask in `shown` with no open row is raised.
27 *
28 * One immediate transaction (`transact`), the open rows read inside it: `endOf` reads before the
29 * first write, and a deferred transaction that read cannot wait to write. Another writer's commit
30 * in between made SQLite refuse the write at once ("database is locked", with no busy wait), as the
31 * merges right after a release did at 03:16 and 04:53 on 2026-12-02. Taking the lock first makes a
32 * busy forge a wait.
2633 */
2734 export function syncAskLedger(
2835 db: Database,
@@ -32,7 +39,7 @@ export function syncAskLedger(
3239 endOf: (askId: string, raisedAt: string) => Exclude<AskEnd, "dismissed">,
3340 now: string,
3441 ): void {
35 const open = db
42 const openRows = () => db
3643 .query<
3744 { id: string; ask_id: string; raised_at: string },
3845 [string]
@@ -49,9 +56,9 @@ export function syncAskLedger(
4956 >(
5057 "INSERT INTO ask_ledger (id, viewer_id, ask_id, repo_id, kind, raised_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
5158 );
52 db.transaction(() => {
59 transact(db, () => {
5360 const stillOpen = new Set<string>();
54 for (const row of open) {
61 for (const row of openRows()) {
5562 if (shownIds.has(row.ask_id)) stillOpen.add(row.ask_id);
5663 else if (allIds.has(row.ask_id)) end.run(row.id, now, "dismissed");
5764 else end.run(row.id, now, endOf(row.ask_id, row.raised_at));
@@ -59,7 +66,7 @@ export function syncAskLedger(
5966 for (const a of shown)
6067 if (!stillOpen.has(a.id))
6168 raise.run(id(), viewerId, a.id, a.repoId, a.kind, now);
62 })();
69 });
6370 }
6471
6572 export interface AskMeasure {
test/ask-ledger-lock.test.ts added +86
@@ -0,0 +1,86 @@
1 /**
2 * The asks ledger beside another writer (2026-12-02, "asks ledger failed: database is locked" after
3 * two releases): its transaction reads (`endOf`) before it writes, and another connection's commit
4 * in between made a deferred transaction's write fail at once, with no busy wait. The ledger now
5 * takes the write lock first (`transact`), so the other writer is the one that meets the lock: the
6 * ledger lands whole, and the other write lands after it.
7 */
8 import { Database } from "bun:sqlite";
9 import { afterAll, beforeAll, expect, test } from "bun:test";
10 import { mkdtemp, rm } from "node:fs/promises";
11 import { tmpdir } from "node:os";
12 import { join } from "node:path";
13 import { createRepo } from "../src/actions/repos.ts";
14 import { createApp, type App } from "../src/app.ts";
15 import { syncAskLedger } from "../src/core/ask_ledger.ts";
16 import { createUser, type User } from "../src/core/users.ts";
17 import { isBusy } from "../src/kernel/db.ts";
18 import { quietLogs } from "./helpers/globals.ts";
19
20 quietLogs();
21
22 let dir = "";
23 let app: App;
24 let alice: User;
25 let repoId = "";
26
27 beforeAll(async () => {
28 dir = await mkdtemp(join(tmpdir(), "ask-ledger-lock-"));
29 app = createApp({ dataDir: dir, port: 0, host: "forge.test" });
30 const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
31 if (!u.ok) throw new Error(u.error);
32 alice = u.value;
33 const r = await createRepo(app, alice, { name: "lib", visibility: "public" });
34 if (!r.ok) throw new Error(String(r.error));
35 repoId = r.value.id;
36 });
37 afterAll(async () => {
38 app.close();
39 await rm(dir, { recursive: true, force: true });
40 });
41
42 const ask = (id: string) => ({ id, repoId, kind: "review" });
43 const rows = () =>
44 app.db
45 .query<{ ask_id: string; ended_how: string | null }, [string]>("SELECT ask_id, ended_how FROM ask_ledger WHERE viewer_id = ?1 ORDER BY ask_id")
46 .all(alice.id);
47
48 test("a ledger write that another writer commits beside lands whole; the other writer waits for it", () => {
49 syncAskLedger(app.db, alice.id, [ask("a")], [ask("a")], () => "acted", "2026-12-02T04:51:00Z");
50 // A second connection to the same file, as the serving thread is to the engine's.
51 const other = new Database(join(dir, "forge.db"));
52 other.exec("PRAGMA busy_timeout = 0");
53 const write = () =>
54 other.query("INSERT INTO ask_ledger (id, viewer_id, ask_id, repo_id, kind, raised_at) VALUES ('other', ?1, 'z', ?2, 'review', '2026-12-02T04:53:17Z')").run(alice.id, repoId);
55 let met = false;
56 try {
57 // "a" is gone from what asks alice: the ledger asks `endOf`, which reads, and the other writer
58 // commits right then, between the ledger's read and its write.
59 syncAskLedger(
60 app.db,
61 alice.id,
62 [ask("b")],
63 [ask("b")],
64 () => {
65 app.db.query("SELECT COUNT(*) FROM ask_ledger").get();
66 try {
67 write();
68 } catch (error) {
69 if (!isBusy(error)) throw error;
70 met = true;
71 }
72 return "expired";
73 },
74 "2026-12-02T04:53:17Z",
75 );
76 expect(met).toBe(true);
77 write();
78 } finally {
79 other.close();
80 }
81 expect(rows()).toEqual([
82 { ask_id: "a", ended_how: "expired" },
83 { ask_id: "b", ended_how: null },
84 { ask_id: "z", ended_how: null },
85 ]);
86 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs