Public: anyone can read this; the crew can change it.
Reading a pull's check as text does not block the page thread: check.txt stalled every page 49 times in 12 minutes #13836
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13834-reading-a-pull-s-check-as-text-does-not → main 4 commits fork
2 files
changed +89
−22
src/http/check_text.ts +60 −21
| @@ -86,15 +86,15 @@ export async function checkText(ctx: Ctx): Promise<Response> { | ||
| 86 | 86 | try { |
| 87 | 87 | unsubscribe = ctx.app.bus.subscribe((event) => { |
| 88 | 88 | if (!event.kind.startsWith("run.") || event.payload.issueId !== issueId) return; |
| 89 | const again = read(ctx); | |
| 90 | if (again instanceof Response || again.word !== first.word) changed.resolve(); | |
| 89 | const again = verdict(ctx); | |
| 90 | if ("error" in again || again.word !== first.word) changed.resolve(); | |
| 91 | 91 | }); |
| 92 | 92 | timer = setTimeout(() => changed.resolve(), Math.min(seconds, HOLD_MAX) * 1000); |
| 93 | 93 | if (ctx.request.signal.aborted) changed.resolve(); |
| 94 | 94 | else ctx.request.signal.addEventListener("abort", onAbort, { once: true }); |
| 95 | 95 | // The state may have moved between the first reading and the listener. |
| 96 | const check = read(ctx); | |
| 97 | if (check instanceof Response || check.word !== first.word) changed.resolve(); | |
| 96 | const check = verdict(ctx); | |
| 97 | if ("error" in check || check.word !== first.word) changed.resolve(); | |
| 98 | 98 | await changed.promise; |
| 99 | 99 | } finally { |
| 100 | 100 | release(); |
| @@ -103,29 +103,68 @@ export async function checkText(ctx: Ctx): Promise<Response> { | ||
| 103 | 103 | return last instanceof Response ? last : last.response; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | function read(ctx: Ctx): (Reading & { pullId: string }) | Response { | |
| 106 | type Verdict = ReturnType<typeof verdict>; | |
| 107 | ||
| 108 | /** | |
| 109 | * The check's word and nothing more: two indexed reads, no log. A held wait calls this on every | |
| 110 | * run event of its pull, so it must stay cheap (#13834); the log is read only to answer. | |
| 111 | */ | |
| 112 | function verdict(ctx: Ctx) { | |
| 107 | 113 | const found = loadPull(ctx); |
| 108 | if ("error" in found) return found.error; | |
| 114 | if ("error" in found) return found; | |
| 109 | 115 | const { repo, pull } = found; |
| 110 | 116 | const { state, run } = checkSummary(ctx.app.db, pull.id, pull.headOid); |
| 111 | 117 | const word = state === "ok" ? "green" : state === "failed" || state === "cancelled" ? "red" : "pending"; |
| 112 | let body = `${word}\nhead: ${pull.headOid}\n`; | |
| 113 | if (word === "red" && run) { | |
| 114 | // A contribute token reaching here is the pull's own author (the gate): its log is its to read. | |
| 115 | const own = ctx.auth.contribute !== undefined && ctx.auth.user?.id === pull.authorId; | |
| 116 | const lines = own | |
| 117 | ? getRepoRun(ctx.app, ctx.auth.user, repo, run.id).ok | |
| 118 | ? readRunLogTail(ctx.app.config.dataDir, run.id, TAIL_MAX).lines | |
| 119 | : [] | |
| 120 | : (() => { | |
| 121 | const log = runLog(ctx.app, ctx.auth.user, repo, run.id, TAIL_MAX); | |
| 122 | return log.ok && !log.value.logHidden ? log.value.lines : []; | |
| 123 | })(); | |
| 124 | if (lines.length > 0) body += `\n${lines.slice(-TAIL_MAX).join("\n")}\n`; | |
| 118 | return { repo, pull, run, word }; | |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * A red check's tail, per run and reader, kept a few seconds: twenty waiters released together read | |
| 123 | * the log once. The price: a tail can miss lines written in the last 5 s, and a change of who may | |
| 124 | * read the log takes up to 5 s to show. | |
| 125 | */ | |
| 126 | const TAIL_TTL_MS = 5000; | |
| 127 | const TAIL_KEPT = 32; | |
| 128 | type Tail = { at: number; lines: string[] }; | |
| 129 | const tailsOf = new WeakMap<object, Map<string, Tail>>(); | |
| 130 | ||
| 131 | function tailOf(ctx: Ctx, v: Extract<Verdict, { word: string }>): string[] { | |
| 132 | const run = v.run; | |
| 133 | if (!run) return []; | |
| 134 | // A contribute token reaching here is the pull's own author (the gate): its log is its to read. | |
| 135 | const own = ctx.auth.contribute !== undefined && ctx.auth.user?.id === v.pull.authorId; | |
| 136 | let tails = tailsOf.get(ctx.app); | |
| 137 | if (!tails) tailsOf.set(ctx.app, (tails = new Map())); | |
| 138 | const key = `${run.id}|${own ? "own" : (ctx.auth.user?.id ?? "-")}`; | |
| 139 | const hit = tails.get(key); | |
| 140 | const now = Date.now(); | |
| 141 | if (hit && now - hit.at < TAIL_TTL_MS) return hit.lines; | |
| 142 | let lines: string[]; | |
| 143 | if (own) { | |
| 144 | lines = getRepoRun(ctx.app, ctx.auth.user, v.repo, run.id).ok | |
| 145 | ? readRunLogTail(ctx.app.config.dataDir, run.id, TAIL_MAX).lines | |
| 146 | : []; | |
| 147 | } else { | |
| 148 | const log = runLog(ctx.app, ctx.auth.user, v.repo, run.id, TAIL_MAX); | |
| 149 | lines = log.ok && !log.value.logHidden ? log.value.lines : []; | |
| 150 | } | |
| 151 | lines = lines.slice(-TAIL_MAX); | |
| 152 | if (tails.size >= TAIL_KEPT) tails.delete(tails.keys().next().value!); | |
| 153 | tails.set(key, { at: now, lines }); | |
| 154 | return lines; | |
| 155 | } | |
| 156 | ||
| 157 | function read(ctx: Ctx): (Reading & { pullId: string }) | Response { | |
| 158 | const v = verdict(ctx); | |
| 159 | if ("error" in v) return v.error; | |
| 160 | let body = `${v.word}\nhead: ${v.pull.headOid}\n`; | |
| 161 | if (v.word === "red" && v.run) { | |
| 162 | const lines = tailOf(ctx, v); | |
| 163 | if (lines.length > 0) body += `\n${lines.join("\n")}\n`; | |
| 125 | 164 | } |
| 126 | 165 | return { |
| 127 | word, | |
| 128 | pullId: pull.id, | |
| 166 | word: v.word, | |
| 167 | pullId: v.pull.id, | |
| 129 | 168 | response: securityFloor( |
| 130 | 169 | new Response(body, { |
| 131 | 170 | headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" }, |
test/contribute-check-text.test.ts +29 −1
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | /** A pull's latest check reads as plain text at /<owner>/<repo>/pulls/<n>/check.txt. */ |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 2 | import { afterAll, beforeAll, expect, spyOn, test } from "bun:test"; | |
| 3 | import * as logModule from "../src/runs/log.ts"; | |
| 3 | 4 | import { mkdtemp, rm } from "node:fs/promises"; |
| 4 | 5 | import { tmpdir } from "node:os"; |
| 5 | 6 | import { join } from "node:path"; |
| @@ -288,3 +289,30 @@ test("a client that goes away frees its place and its bus subscription", async ( | ||
| 288 | 289 | app.bus.subscribe = subscribe; |
| 289 | 290 | } |
| 290 | 291 | }, SLOW); |
| 292 | ||
| 293 | test("twenty held waits and one finishing check read the log once, and run events read none (#13834)", async () => { | |
| 294 | const p = addPull("s1"); | |
| 295 | const id = run(p.id, "s1", "running"); | |
| 296 | for (let i = 0; i < 50; i++) appendRunLog(dataDir, id, `${i} line`); | |
| 297 | flushRunLogs(); | |
| 298 | const cookie = `forge_session=${createSession(app.db, owner).token}`; | |
| 299 | const subs = countSubs(); | |
| 300 | const reads = spyOn(logModule, "readRunLogTail"); | |
| 301 | try { | |
| 302 | const held = Array.from({ length: 20 }, () => | |
| 303 | fetch(`${servers.url}/alice/notes/pulls/${p.number}/check.txt?wait=30`, { headers: { cookie } }), | |
| 304 | ); | |
| 305 | await until(() => subs.live >= 20); | |
| 306 | expect(reads).toHaveBeenCalledTimes(0); // pending: nothing reads the log | |
| 307 | // Events that leave the check running wake every waiter's listener but read no log. | |
| 308 | for (let i = 0; i < 3; i++) app.bus.append({ kind: "run.started", repoId, payload: { issueId: p.id, runId: id } }); | |
| 309 | expect(reads).toHaveBeenCalledTimes(0); | |
| 310 | finish(p.id, id, "failed"); | |
| 311 | const texts = await Promise.all((await Promise.all(held)).map((r) => r.text())); | |
| 312 | for (const t of texts) expect(t.startsWith("red\nhead: s1\n\n0 line\n")).toBe(true); | |
| 313 | expect(reads).toHaveBeenCalledTimes(1); // twenty released waiters, one read | |
| 314 | } finally { | |
| 315 | reads.mockRestore(); | |
| 316 | subs.restore(); | |
| 317 | } | |
| 318 | }, SLOW); |
new above
offline