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
Merged 16h 42m ago, live in production
torque/forge:torque/18157-asks-ledger-failed-database-is-locked-tw → main 0 commits fork
4 files
changed +77
−13
src/engine/duties.ts +1 −1
| @@ -174,7 +174,7 @@ function startReaders(app: App, adopt: () => void): () => void { | ||
| 174 | 174 | try { |
| 175 | 175 | recordAskLedger(app.db); |
| 176 | 176 | } 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) }); | |
| 178 | 178 | } |
| 179 | 179 | try { |
| 180 | 180 | noteDbSize(app.db, app.config.dataDir); |
src/main.ts +1 −1
| @@ -1001,7 +1001,7 @@ const tickAsks = async (): Promise<void> => { | ||
| 1001 | 1001 | try { |
| 1002 | 1002 | (await import("./web/studio/asks.ts")).recordAskLedger(app.db); |
| 1003 | 1003 | } 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) }); | |
| 1005 | 1005 | } |
| 1006 | 1006 | try { |
| 1007 | 1007 | noteDbSize(app.db, app.config.dataDir); |
src/web/studio/asks.ts +30 −11
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | |
| 10 | 10 | import { askedAsleep, asleepLine } from "../../actions/needs_asleep.ts"; |
| 11 | 11 | import type { Database } from "bun:sqlite"; |
| 12 | import { isBusy, withoutWaiting } from "../../kernel/db.ts"; | |
| 12 | 13 | import { crewHandsFor } from "../../actions/give_back.ts"; |
| 13 | 14 | import { forgeNeedList, type ForgeNeed } from "../../actions/health.ts"; |
| 14 | 15 | import { reviewSet } from "./review_set.ts"; |
| @@ -1023,9 +1024,10 @@ export function endOf( | ||
| 1023 | 1024 | /** |
| 1024 | 1025 | * Bring the asks ledger (`core/ask_ledger.ts`) in line with what asks each |
| 1025 | 1026 | * 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. | |
| 1027 | 1029 | */ |
| 1028 | export function recordAskLedger(db: Database, now = new Date()): void { | |
| 1030 | export function recordAskLedger(db: Database, now = new Date()): number { | |
| 1029 | 1031 | const at = now.toISOString(); |
| 1030 | 1032 | // Bounded, and in an order that keeps the people asks are for: whoever |
| 1031 | 1033 | // 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 { | ||
| 1048 | 1050 | LIMIT 200`, |
| 1049 | 1051 | ) |
| 1050 | 1052 | .all(at); |
| 1053 | let done = 0; | |
| 1051 | 1054 | for (const { id } of people) { |
| 1052 | 1055 | const user = getUserById(db, id); |
| 1053 | if (!user) continue; | |
| 1056 | if (!user) { | |
| 1057 | done++; | |
| 1058 | continue; | |
| 1059 | } | |
| 1054 | 1060 | const repos = listReposVisibleTo(db, user, { limit: 200, withoutArchived: true }); |
| 1055 | 1061 | const all = scoredFor(db, user, repos, now.getTime()); |
| 1056 | 1062 | const shown = splitByFilters(db, user, notDismissed(db, user, all)).shown; |
| 1057 | 1063 | 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++; | |
| 1066 | 1084 | } |
| 1085 | return 0; | |
| 1067 | 1086 | } |
| 1068 | 1087 | |
| 1069 | 1088 | /** 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 | ||
| 84 | 84 | { ask_id: "z", ended_how: null }, |
| 85 | 85 | ]); |
| 86 | 86 | }); |
| 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 | }); |
new above
offline