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

Whether reports are closed is read from the database, so every process agrees #15053

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15039-whether-reports-are-closed-is-read-from → main 1 commit fork

  • Merged. scout/15039-whether-reports-are-closed-is-read-from was merged into main as 4b0992f.

Review

Sign in to review this change.
2 files changed +62 −19
src/core/reports.ts +6 −19
@@ -212,30 +212,17 @@ export function outsideTitle(
212212 }
213213
214214 /**
215 * Which repositories have closed the door, held per database after the first
216 * read (every repository page asks): `setReportsClosed` is the only writer, so
217 * it keeps the set. At most one id per closed repository, gone on restart.
215 * Whether the repository's owner has closed the door for reports from outside.
216 * Read from the row every time (a primary-key lookup), never from memory: a
217 * close made on another connection or by another process holds here at once
218 * (#15039).
218219 */
219 const closedSets = new WeakMap<Database, Set<string>>();
220 function closedSet(db: Database): Set<string> {
221 let set = closedSets.get(db);
222 if (set === undefined) {
223 set = new Set(
224 (db.query("SELECT repo_id AS id FROM report_closed").all() as { id: string }[]).map((r) => r.id),
225 );
226 closedSets.set(db, set);
227 }
228 return set;
229 }
230
231 /** Whether the repository's owner has closed the door for reports from outside. */
232 export const reportsClosed = (db: Database, repoId: string): boolean => closedSet(db).has(repoId);
220 export const reportsClosed = (db: Database, repoId: string): boolean =>
221 db.query("SELECT 1 FROM report_closed WHERE repo_id = ?1").get(repoId) !== null;
233222
234223 export function setReportsClosed(db: Database, repoId: string, closed: boolean): void {
235224 if (closed) db.query("INSERT OR IGNORE INTO report_closed (repo_id) VALUES (?1)").run(repoId);
236225 else db.query("DELETE FROM report_closed WHERE repo_id = ?1").run(repoId);
237 if (closed) closedSet(db).add(repoId);
238 else closedSet(db).delete(repoId);
239226 }
240227
241228 export function countWaiting(db: Database, repoId: string): number {
test/reports-closed-shared.test.ts added +56
@@ -0,0 +1,56 @@
1 /**
2 * Whether reports are closed is read from the row (#15039): a close made on
3 * one connection to the database is seen by filing on another, as when the
4 * terminal and the web are different processes.
5 */
6 import { Database } from "bun:sqlite";
7 import { afterAll, beforeAll, expect, test } from "bun:test";
8 import { mkdtemp, rm } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { createRepo } from "../src/actions/repos.ts";
12 import { fileReport } from "../src/actions/reports.ts";
13 import { createApp, type App } from "../src/app.ts";
14 import { reportsClosed, setReportsClosed } from "../src/core/reports.ts";
15 import { createUser } from "../src/core/users.ts";
16 import type { Repo } from "../src/core/repos.ts";
17
18 let dataDir: string;
19 let app: App;
20 let other: Database;
21 let repo: Repo;
22
23 beforeAll(async () => {
24 dataDir = await mkdtemp(join(tmpdir(), "forge-reports-closed-shared-"));
25 app = createApp({ dataDir, port: 0 });
26 const made = await createUser(app.db, {
27 origin: "seed", trusted: true, username: "alice", email: "[email protected]",
28 password: "a-good-passphrase", adminIfFirst: true,
29 });
30 if (!made.ok) throw new Error(String(made.error));
31 const r = await createRepo(app, made.value, { name: "game", visibility: "public" });
32 if (!r.ok) throw new Error(String(r.error));
33 repo = r.value;
34 other = new Database(join(dataDir, "forge.db"), { strict: true });
35 other.exec("PRAGMA busy_timeout = 5000");
36 });
37
38 afterAll(async () => {
39 other.close();
40 app.close();
41 await rm(dataDir, { recursive: true, force: true });
42 });
43
44 test("a close on another connection refuses the next filing, and an open lets it through", () => {
45 // The first read happens before the other connection writes, as a page would.
46 expect(reportsClosed(app.db, repo.id)).toBe(false);
47 setReportsClosed(other, repo.id, true);
48 expect(reportsClosed(app.db, repo.id)).toBe(true);
49 const refused = fileReport(app, repo, { title: "Broken", body: "", page: "" }, "src-a");
50 expect(refused.ok).toBe(false);
51 if (!refused.ok) expect(refused.error.error).toBe("closed");
52
53 setReportsClosed(other, repo.id, false);
54 expect(reportsClosed(app.db, repo.id)).toBe(false);
55 expect(fileReport(app, repo, { title: "Broken", body: "", page: "" }, "src-b").ok).toBe(true);
56 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs