Public: anyone can read this; the crew can change it.
A new head cancels its pull's running check, not only the queued one #1834
mason/forge:mason/1793-a-new-head-cancels-its-pull-s-running-ch → main 2 commits fork
2 files
changed +51
−0
src/checks/service.ts +9
| @@ -112,6 +112,9 @@ export interface ChecksHandle { | ||
| 112 | 112 | /** How often queued checks another forge left are looked for. */ |
| 113 | 113 | const TICK_MS = 5_000; |
| 114 | 114 | |
| 115 | /** How often a running check looks whether it was cancelled under it. */ | |
| 116 | const WATCH_MS = 2_000; | |
| 117 | ||
| 115 | 118 | /** |
| 116 | 119 | * How long a queued check may wait before it outranks a newer one however |
| 117 | 120 | * fresh: the bound that keeps `pickNext` from starving a pull nobody has |
| @@ -386,9 +389,15 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle { | ||
| 386 | 389 | overran = true; |
| 387 | 390 | budget.abort(); |
| 388 | 391 | }, limit); |
| 392 | // A row cancelled by another forge over the same database (a roll): | |
| 393 | // nothing aborts this one in memory, so it looks (#1793). | |
| 394 | const watch = setInterval(() => { | |
| 395 | if (getRun(app.db, run.id)?.state === "cancelled") budget.abort(); | |
| 396 | }, WATCH_MS); | |
| 389 | 397 | const outcome = await check(claimed, repo, write, budget.signal).finally( |
| 390 | 398 | () => { |
| 391 | 399 | clearTimeout(clock); |
| 400 | clearInterval(watch); | |
| 392 | 401 | signal.removeEventListener("abort", forward); |
| 393 | 402 | lease(); |
| 394 | 403 | }, |
test/checks.test.ts +42
| @@ -876,6 +876,48 @@ describe("checks", () => { | ||
| 876 | 876 | expect(done.reason).toBe("boot"); |
| 877 | 877 | expect(done.state).toBe("ok"); |
| 878 | 878 | }, 60_000); |
| 879 | ||
| 880 | /** Wait until `find` answers a run; the run, or undefined at the deadline. */ | |
| 881 | async function until( | |
| 882 | find: () => Run | undefined, | |
| 883 | timeoutMs = 20_000, | |
| 884 | ): Promise<Run | undefined> { | |
| 885 | const deadline = Date.now() + timeoutMs; | |
| 886 | while (Date.now() < deadline) { | |
| 887 | const run = find(); | |
| 888 | if (run) return run; | |
| 889 | await Bun.sleep(50); | |
| 890 | } | |
| 891 | return undefined; | |
| 892 | } | |
| 893 | test("a running check cancelled by another forge over the same database stops within seconds", async () => { | |
| 894 | const slow = await commitToBranch(app, alice, repo, { | |
| 895 | message: "A check that sleeps", | |
| 896 | files: [{ path: "forge.json", content: checkJson("sleep 30") }], | |
| 897 | }); | |
| 898 | if (!slow.ok) throw new Error(JSON.stringify(slow.error)); | |
| 899 | const running = await until(() => | |
| 900 | listRuns(app.db, repo.id, { kind: "check", limit: 100 }).find( | |
| 901 | (r) => r.commitOid === slow.value.commitOid && r.state === "running", | |
| 902 | ), | |
| 903 | ); | |
| 904 | expect(running).toBeDefined(); | |
| 905 | // What the other forge's `request` writes: the row, nothing in memory here. | |
| 906 | cancelRun(app.db, running!.id, "superseded by 1234567"); | |
| 907 | // The slot this forge held comes free: the next check runs in it. | |
| 908 | const next = await commitToBranch(app, alice, repo, { | |
| 909 | message: "A quick check", | |
| 910 | files: [{ path: "forge.json", content: checkJson("true") }], | |
| 911 | }); | |
| 912 | if (!next.ok) throw new Error(JSON.stringify(next.error)); | |
| 913 | const done = await settled( | |
| 914 | (r) => r.commitOid === next.value.commitOid, | |
| 915 | 10_000, | |
| 916 | ); | |
| 917 | expect(done.state).toBe("ok"); | |
| 918 | expect(getRun(app.db, running!.id)?.state).toBe("cancelled"); | |
| 919 | expect(getRun(app.db, running!.id)?.summary).toBe("superseded by 1234567"); | |
| 920 | }, 60_000); | |
| 879 | 921 | }); |
| 880 | 922 | |
| 881 | 923 | describe("a check its forge died under", () => { |
new above
offline