Public: anyone can read this; the crew can change it.
Stall log: a long-polled runner claim is resting, not in flight #16712
Check · Review · Land · Live done — Landed and live in production.
claim-perf → main 0 commits
3 files
changed +117
−2
src/http/api/runners.ts +4 −1
| @@ -26,6 +26,7 @@ import { | ||
| 26 | 26 | type RunnerResult, |
| 27 | 27 | } from "../../checks/runner_jobs.ts"; |
| 28 | 28 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| 29 | import { inFlight } from "../../perf/live.ts"; | |
| 29 | 30 | import { advertiseRefs, serviceRpc } from "../../git/smart-http.ts"; |
| 30 | 31 | import type { Ctx, Handler } from "../context.ts"; |
| 31 | 32 | import { STRANGER_MAX } from "../limits.ts"; |
| @@ -141,7 +142,9 @@ export function runnerRoutes(router: Router<Handler>): void { | ||
| 141 | 142 | } |
| 142 | 143 | const left = until - Date.now(); |
| 143 | 144 | if (left <= 0 || stop.aborted) return new Response(null, { status: 204 }); |
| 144 | await nextOffer(ctx.app, left, stop); | |
| 145 | // Resting, the claim holds nothing: a stall while it waits is not its own | |
| 146 | // (`perf/in_flight.ts`). | |
| 147 | await inFlight.rest(ctx.request, () => nextOffer(ctx.app, left, stop)); | |
| 145 | 148 | } |
| 146 | 149 | }); |
| 147 | 150 |
src/perf/in_flight.ts +22 −1
| @@ -9,6 +9,13 @@ | ||
| 9 | 9 | * read only at a wake (`nameOf`), since the router names a request after it |
| 10 | 10 | * starts; a stall is rare, so the walk over the open set costs nothing on |
| 11 | 11 | * the common request. |
| 12 | * | |
| 13 | * A request resting on a wake (`rest`) is not open while it rests. A long poll held for 25 s | |
| 14 | * runs a few microseconds of it: open throughout, it was named "in flight" for every stall in | |
| 15 | * those 25 s, and its name hid the real cause, since a stall with a request in flight reads no | |
| 16 | * job (on production 2026-11-30, 320 of 347 stalls in an hour named | |
| 17 | * `/api/v1/runner/claim (in flight)`). Woken, it is open again from the wake, so a stall it causes | |
| 18 | * after waking is still charged to it. | |
| 12 | 19 | */ |
| 13 | 20 | |
| 14 | 21 | export interface InFlight<K> { |
| @@ -18,13 +25,18 @@ export interface InFlight<K> { | ||
| 18 | 25 | end(key: K): void; |
| 19 | 26 | /** Run `work` with `key` open from `at` until it settles, however it settles. */ |
| 20 | 27 | during<T>(key: K, at: number, work: () => Promise<T>): Promise<T>; |
| 28 | /** | |
| 29 | * Run `work`, a wait for a wake, with `key` out of the open set; on its settling `key` is open | |
| 30 | * again from that moment. A key not open runs `work` and stays closed. | |
| 31 | */ | |
| 32 | rest<T>(key: K, work: () => Promise<T>): Promise<T>; | |
| 21 | 33 | /** The patterns of the requests begun at or before `from` and still open, oldest first, each once. */ |
| 22 | 34 | since(from: number): string[]; |
| 23 | 35 | /** How many are open now. */ |
| 24 | 36 | size(): number; |
| 25 | 37 | } |
| 26 | 38 | |
| 27 | export function createInFlight<K>(nameOf: (key: K) => string): InFlight<K> { | |
| 39 | export function createInFlight<K>(nameOf: (key: K) => string, now: () => number = () => performance.now()): InFlight<K> { | |
| 28 | 40 | const open = new Map<K, number>(); |
| 29 | 41 | return { |
| 30 | 42 | begin(key, at) { |
| @@ -41,6 +53,15 @@ export function createInFlight<K>(nameOf: (key: K) => string): InFlight<K> { | ||
| 41 | 53 | open.delete(key); |
| 42 | 54 | } |
| 43 | 55 | }, |
| 56 | async rest(key, work) { | |
| 57 | if (!open.has(key)) return work(); | |
| 58 | open.delete(key); | |
| 59 | try { | |
| 60 | return await work(); | |
| 61 | } finally { | |
| 62 | open.set(key, now()); | |
| 63 | } | |
| 64 | }, | |
| 44 | 65 | since(from) { |
| 45 | 66 | const old: [number, string][] = []; |
| 46 | 67 | for (const [key, at] of open) if (at <= from) old.push([at, nameOf(key)]); |
test/claim-rest.test.ts added +91
| @@ -0,0 +1,91 @@ | ||
| 1 | /** | |
| 2 | * A runner's claim resting in its long poll is not "in flight" for a stall (`perf/in_flight.ts` | |
| 3 | * `rest`). Every check runner holds a claim open for up to 25 s, so a claim open throughout was | |
| 4 | * named for nearly every stall on production (320 of 347 in an hour) while its own work was a | |
| 5 | * 0.2 ms indexed look; and a stall with a request in flight reads no job, so the cause was hidden. | |
| 6 | */ | |
| 7 | import { expect, test } from "bun:test"; | |
| 8 | import { deskOf, enrolRunner, offerJob, wakeClaimers, type RunnerJob } from "../src/checks/runner_jobs.ts"; | |
| 9 | import { createInFlight } from "../src/perf/in_flight.ts"; | |
| 10 | import { inFlight, liveOf } from "../src/perf/live.ts"; | |
| 11 | import { blamed, createLoopWatch, LIMIT_MS } from "../src/perf/loop.ts"; | |
| 12 | import { checksForge } from "./helpers/checks-forge.ts"; | |
| 13 | import { quietLogs } from "./helpers/globals.ts"; | |
| 14 | ||
| 15 | quietLogs(); | |
| 16 | const f = checksForge("forge-claim-rest-"); | |
| 17 | const CLAIM = "/api/v1/runner/claim"; | |
| 18 | ||
| 19 | test("a resting request is out of the open set, and open again from its wake", async () => { | |
| 20 | let clock = 1_000; | |
| 21 | const open = createInFlight<string>( | |
| 22 | (key) => key.split("#")[0]!, | |
| 23 | () => clock, | |
| 24 | ); | |
| 25 | const watch = createLoopWatch( | |
| 26 | 250, | |
| 27 | () => [], | |
| 28 | open.since, | |
| 29 | () => [], | |
| 30 | () => ["sampler"], | |
| 31 | ); | |
| 32 | open.begin("/poll#1", 1_000); | |
| 33 | let wake = (): void => {}; | |
| 34 | const resting = open.rest("/poll#1", () => new Promise<void>((r) => (wake = r))); | |
| 35 | // Held 400 ms while the poll rested: the suspect is read, not the poll. | |
| 36 | expect(blamed(watch.stalled(400, 2_000)!)).toEqual(["awaiting:sampler"]); | |
| 37 | clock = 2_100; | |
| 38 | wake(); | |
| 39 | await resting; | |
| 40 | // Woken at 2_100: a stall from 2_200 on is its own; one reaching back before the wake is not. | |
| 41 | expect(blamed(watch.stalled(400, 2_600)!)).toEqual(["/poll (in flight)"]); | |
| 42 | expect(open.since(2_099)).toEqual([]); | |
| 43 | // Resting a key not open runs the work and leaves it closed. | |
| 44 | expect(await open.rest("/never#1", async () => 7)).toBe(7); | |
| 45 | expect(open.size()).toBe(1); | |
| 46 | // A rest that throws still reopens. | |
| 47 | await expect( | |
| 48 | open.rest("/poll#1", async () => { | |
| 49 | throw new Error("gone"); | |
| 50 | }), | |
| 51 | ).rejects.toThrow("gone"); | |
| 52 | expect(open.since(clock)).toEqual(["/poll"]); | |
| 53 | }); | |
| 54 | ||
| 55 | const job: Omit<RunnerJob, "id"> = { | |
| 56 | run: "run1", | |
| 57 | repo: "alice/lib", | |
| 58 | commit: "a".repeat(40), | |
| 59 | depth: 1, | |
| 60 | steps: [{ name: "check", command: "true" }], | |
| 61 | env: { CI: "1" }, | |
| 62 | limits: {}, | |
| 63 | timeoutS: 60, | |
| 64 | egress: [], | |
| 65 | egressMode: "enforce", | |
| 66 | cell: "preferred", | |
| 67 | cache: { repo: "r", tree: "", base: "" }, | |
| 68 | }; | |
| 69 | ||
| 70 | test("a real claim waiting on the desk is not named for a stall, and still takes the job it is woken for", async () => { | |
| 71 | const { token } = enrolRunner(f.app.db, "rest", f.alice.id); | |
| 72 | const desk = deskOf(f.app); | |
| 73 | const before = desk.claimers.size; | |
| 74 | const claim = fetch(`${f.servers.url}${CLAIM}`, { | |
| 75 | method: "POST", | |
| 76 | headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, | |
| 77 | body: JSON.stringify({ wait: 25 }), | |
| 78 | }); | |
| 79 | // The signal: the claim has looked, found nothing, and rests on the desk. | |
| 80 | while (desk.claimers.size === before) await new Promise((r) => setImmediate(r)); | |
| 81 | const now = performance.now(); | |
| 82 | expect(inFlight.since(now)).not.toContain(CLAIM); | |
| 83 | const stall = liveOf(f.app).loopWatch.stalled(LIMIT_MS + 10, now)!; | |
| 84 | expect(blamed(stall).some((name) => name.startsWith(CLAIM))).toBe(false); | |
| 85 | ||
| 86 | offerJob(f.app.db, { runId: "run1", repoId: "r", dir: "/nowhere", job }); | |
| 87 | wakeClaimers(f.app); | |
| 88 | const res = await claim; | |
| 89 | expect(res.status).toBe(200); | |
| 90 | expect(((await res.json()) as { job: { run: string } }).job.run).toBe("run1"); | |
| 91 | }); |
new above
offline