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

"asks ledger failed: database is locked" two minutes after a release (v0.7.733) #18169

opened this in answer to

Merged 16h 42m ago, live in production

torque/forge:torque/18157-asks-ledger-failed-database-is-locked-tw → main 0 commits fork

  • Merged. torque/18157-asks-ledger-failed-database-is-locked-tw was merged into main as 009df15.

Review

Sign in to review this change.
4 files changed +77 −13
src/engine/duties.ts +1 −1
@@ -174,7 +174,7 @@ function startReaders(app: App, adopt: () => void): () => void {
174174 try {
175175 recordAskLedger(app.db);
176176 } catch (error) {
177 app.log.error("asks ledger failed", { error: String(error) });
177 app.log.error("asks ledger failed", { step: "engine tick: recordAskLedger", error: String(error) });
178178 }
179179 try {
180180 noteDbSize(app.db, app.config.dataDir);
src/main.ts +1 −1
@@ -1001,7 +1001,7 @@ const tickAsks = async (): Promise<void> => {
10011001 try {
10021002 (await import("./web/studio/asks.ts")).recordAskLedger(app.db);
10031003 } catch (error) {
1004 app.log.error("asks ledger failed", { error: String(error) });
1004 app.log.error("asks ledger failed", { step: "serving tick: recordAskLedger", error: String(error) });
10051005 }
10061006 try {
10071007 noteDbSize(app.db, app.config.dataDir);
src/web/studio/asks.ts +30 −11
@@ -9,6 +9,7 @@
99
1010 import { askedAsleep, asleepLine } from "../../actions/needs_asleep.ts";
1111 import type { Database } from "bun:sqlite";
12 import { isBusy, withoutWaiting } from "../../kernel/db.ts";
1213 import { crewHandsFor } from "../../actions/give_back.ts";
1314 import { forgeNeedList, type ForgeNeed } from "../../actions/health.ts";
1415 import { reviewSet } from "./review_set.ts";
@@ -1023,9 +1024,10 @@ export function endOf(
10231024 /**
10241025 * Bring the asks ledger (`core/ask_ledger.ts`) in line with what asks each
10251026 * person who came to the forge lately — a signed-in session that has not
1026 * expired, or a phone to tell. A tick's work, never a request's.
1027 * expired, or a phone to tell. A tick's work, never a request's. Behind another
1028 * process's write lock it stops at once and returns how many people it left for the next tick.
10271029 */
1028 export function recordAskLedger(db: Database, now = new Date()): void {
1030 export function recordAskLedger(db: Database, now = new Date()): number {
10291031 const at = now.toISOString();
10301032 // Bounded, and in an order that keeps the people asks are for: whoever
10311033 // has a crew working (on their own repository or their organisation's),
@@ -1048,22 +1050,39 @@ export function recordAskLedger(db: Database, now = new Date()): void {
10481050 LIMIT 200`,
10491051 )
10501052 .all(at);
1053 let done = 0;
10511054 for (const { id } of people) {
10521055 const user = getUserById(db, id);
1053 if (!user) continue;
1056 if (!user) {
1057 done++;
1058 continue;
1059 }
10541060 const repos = listReposVisibleTo(db, user, { limit: 200, withoutArchived: true });
10551061 const all = scoredFor(db, user, repos, now.getTime());
10561062 const shown = splitByFilters(db, user, notDismissed(db, user, all)).shown;
10571063 const brief = (a: Scored) => ({ id: a.id, repoId: a.repo.id, kind: a.kind });
1058 syncAskLedger(
1059 db,
1060 user.id,
1061 all.map(brief),
1062 shown.map(brief),
1063 (askId, raisedAt) => endOf(db, user.id, askId, raisedAt),
1064 at,
1065 );
1064 try {
1065 // Never waits on another process's write lock: a draining forge on this database must not
1066 // hold the serving thread for the busy timeout (#18157, #6957).
1067 withoutWaiting(db, () =>
1068 syncAskLedger(
1069 db,
1070 user.id,
1071 all.map(brief),
1072 shown.map(brief),
1073 (askId, raisedAt) => endOf(db, user.id, askId, raisedAt),
1074 at,
1075 ),
1076 );
1077 } catch (error) {
1078 if (!isBusy(error)) throw error;
1079 // The ledger is derived from what asks people now: the next tick reconciles what this one
1080 // left.
1081 return people.length - done;
1082 }
1083 done++;
10661084 }
1085 return 0;
10671086 }
10681087
10691088 /** One ask in a line, without its repository's name (the row adds that). */
test/ask-ledger-lock.test.ts +45
@@ -84,3 +84,48 @@ test("a ledger write that another writer commits beside lands whole; the other w
8484 { ask_id: "z", ended_how: null },
8585 ]);
8686 });
87
88 test("the tick behind another process's write lock logs nothing, returns at once, and lands its rows once the lock is released (#18157)", async () => {
89 const { recordAskLedger } = await import("../src/web/studio/asks.ts");
90 const { createSession } = await import("../src/core/sessions.ts");
91 const { createAgent, addToCrew } = await import("../src/actions/crew.ts");
92 const { commitToBranch } = await import("../src/actions/repos.ts");
93 const { openPull } = await import("../src/actions/pulls.ts");
94 const { claimRun, createRun, finishRun } = await import("../src/core/runs.ts");
95 const { getUserByName } = await import("../src/core/users.ts");
96 const { getRepoById } = await import("../src/core/repos.ts");
97 const repo = getRepoById(app.db, repoId)!;
98 await commitToBranch(app, alice, repo, { message: "seed", files: [{ path: "README.md", content: "# lib\n" }] });
99 await createAgent(app, alice, { username: "bot" });
100 addToCrew(app, alice, repo, "bot");
101 await commitToBranch(app, alice, repo, { branch: "bot/1", base: "main", message: "t", files: [{ path: "F.md", content: "1\n" }] });
102 const opened = await openPull(app, getUserByName(app.db, "bot")!, repo, { title: "A change", body: "", baseBranch: "main", headBranch: "bot/1" });
103 if (!opened.ok) throw new Error(String(opened.error));
104 const run = createRun(app.db, { repoId, kind: "check", issueId: opened.value.id, env: "pull-1", commitOid: opened.value.headOid, reason: "pull" });
105 claimRun(app.db, run.id);
106 finishRun(app.db, run.id, { state: "ok", summary: "ok" });
107 app.db.query("DELETE FROM ask_ledger").run();
108 app.db.exec("PRAGMA busy_timeout = 5000");
109 createSession(app.db, alice.id);
110 const now = new Date();
111 const wanted = () => app.db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM ask_ledger").get()!.n;
112 // What asks alice now, learnt on a quiet database.
113 recordAskLedger(app.db, now);
114 const asks = wanted();
115 expect(asks).toBeGreaterThan(0);
116 app.db.query("DELETE FROM ask_ledger").run();
117 const other = new Database(join(dir, "forge.db"));
118 other.exec("BEGIN IMMEDIATE");
119 try {
120 const t0 = performance.now();
121 const left = recordAskLedger(app.db, now);
122 expect(performance.now() - t0).toBeLessThan(1000); // not the 5 s busy timeout
123 expect(left).toBeGreaterThan(0);
124 expect(wanted()).toBe(0);
125 } finally {
126 other.exec("ROLLBACK");
127 other.close();
128 }
129 expect(recordAskLedger(app.db, now)).toBe(0);
130 expect(wanted()).toBe(asks);
131 });

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

Keyboard shortcuts

Docs