Public: anyone can read this; the crew can change it.
A runner waits out a forge restart before handing a fetch back: its git door's 404 is retried while the job is its own #17542
Merged 17h 26m ago, live in production
checks/runner-fetch-restart → main 0 commits 2 threads · 2 unresolved
2 files
changed +97
−1
src/runner/main.ts +44 −1
| @@ -182,6 +182,37 @@ export async function fillMirror( | ||
| 182 | 182 | return undefined; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | /** | |
| 186 | * Whether a failed fetch is the forge away, not the job wrong: its git door answered 404, 502 or | |
| 187 | * 503 (a restarting forge has not taken the job back yet, `checks/adopt.ts`), or nothing answered. | |
| 188 | * A missing commit or a refused ref is the job's, and is not waited out. | |
| 189 | */ | |
| 190 | export function forgeAway(refused: string): boolean { | |
| 191 | return /\b(?:404|502|503)\b|Connection refused|Failed to connect|Could not resolve host|Empty reply|Connection reset|timed out/i.test(refused); | |
| 192 | } | |
| 193 | ||
| 194 | /** | |
| 195 | * The fetch, waited out across a forge restart: a failure that is the forge away is tried again | |
| 196 | * every `pauseMs` for up to `waitMs`, while the job is still this runner's (`still`: the forge has | |
| 197 | * not dropped it, as the report loop hears). Handing the job back instead made a restart run a | |
| 198 | * sharded check whole on the forge itself (2026-12-02). The first answer that is not the forge | |
| 199 | * away, success or the job's own failure, is the answer. | |
| 200 | */ | |
| 201 | export async function fetchUntilBack( | |
| 202 | fetch: () => Promise<string | undefined>, | |
| 203 | still: () => boolean, | |
| 204 | waitMs: number, | |
| 205 | pause: () => Promise<void>, | |
| 206 | now: () => number = Date.now, | |
| 207 | ): Promise<string | undefined> { | |
| 208 | const until = now() + waitMs; | |
| 209 | for (;;) { | |
| 210 | const refused = await fetch(); | |
| 211 | if (refused === undefined || !forgeAway(refused) || !still() || now() >= until) return refused; | |
| 212 | await pause(); | |
| 213 | } | |
| 214 | } | |
| 215 | ||
| 185 | 216 | /** A report of a step's lines, as the runner sends them. */ |
| 186 | 217 | export interface LineBatch { |
| 187 | 218 | step: string; |
| @@ -405,7 +436,19 @@ export async function runRunner(options: RunnerOptions): Promise<RunnerEnd> { | ||
| 405 | 436 | mirror, |
| 406 | 437 | fetched.catch(() => {}), |
| 407 | 438 | ); |
| 408 | const refused = await fetched; | |
| 439 | // A forge restarting answers the job's git door 404 until it takes the job back: the fetch waits | |
| 440 | // it out, while the job is still this runner's, rather than hand a check back to run there. | |
| 441 | const first = await fetched; | |
| 442 | const refused = | |
| 443 | first === undefined || !forgeAway(first) | |
| 444 | ? first | |
| 445 | : (line("runner", `the forge is away (${first.slice(0, 120)}); fetching again once it is back`), | |
| 446 | await fetchUntilBack( | |
| 447 | () => fillMirror(mirror, url, job, depth, (args) => runGit([...args], { cwd: mirror, env: auth, timeoutMs: FETCH_MS, signal: stop.signal })), | |
| 448 | () => !dropped && !stop.signal.aborted, | |
| 449 | HANDOVER_WAIT_MS, | |
| 450 | () => pause(RETRY_MS, stop.signal), | |
| 451 | )); | |
| 409 | 452 | if (refused !== undefined) return await end({ abandon: refused }); |
| 410 | 453 | const fetchedAt = Date.now(); |
| 411 | 454 | mkdirSync(jobDir, { recursive: true }); |
test/runner-fetch-restart.test.ts added +53
| @@ -0,0 +1,53 @@ | ||
| 1 | /** | |
| 2 | * A runner's fetch across a forge restart (`fetchUntilBack`, `src/runner/main.ts`): the job's git | |
| 3 | * door answers 404 (or nothing) until the new forge takes the job back, and the runner waits that | |
| 4 | * out while the job is still its own, rather than hand the check back to run on the forge. A | |
| 5 | * failure that is the job's own (a missing commit) is answered at once, and so is a job the forge | |
| 6 | * dropped meanwhile. | |
| 7 | */ | |
| 8 | import { expect, test } from "bun:test"; | |
| 9 | import { fetchUntilBack, forgeAway } from "../src/runner/main.ts"; | |
| 10 | ||
| 11 | test("the forge away: its door's 404, 502, 503 or no answer; a missing commit is the job's own", () => { | |
| 12 | for (const away of [ | |
| 13 | "git fetch 641629b: fatal: unable to access 'http://127.0.0.1:8080/api/v1/runner/jobs/j1/git/': The requested URL returned error: 404", | |
| 14 | "fatal: unable to access '…': Failed to connect to 127.0.0.1 port 8080 after 0 ms: Connection refused", | |
| 15 | "The requested URL returned error: 503", | |
| 16 | "fatal: unable to access '…': Empty reply from server", | |
| 17 | ]) | |
| 18 | expect([away, forgeAway(away)]).toEqual([away, true]); | |
| 19 | for (const own of ["fatal: remote error: upload-pack: not our ref 641629b", "fatal: couldn't find remote ref refs/heads/x"]) | |
| 20 | expect([own, forgeAway(own)]).toEqual([own, false]); | |
| 21 | }); | |
| 22 | ||
| 23 | /** A clock the waits step: each pause moves it on by `step` ms. */ | |
| 24 | function clock(step: number) { | |
| 25 | let at = 0; | |
| 26 | return { now: () => at, pause: async () => void (at += step) }; | |
| 27 | } | |
| 28 | ||
| 29 | test("a 404 while the forge restarts is fetched again until the door answers, then the job goes on", async () => { | |
| 30 | const answers = ["404", "Connection refused", "404", undefined]; | |
| 31 | let tries = 0; | |
| 32 | const c = clock(5_000); | |
| 33 | const got = await fetchUntilBack(async () => (tries++, answers.shift() === undefined ? undefined : "The requested URL returned error: 404"), () => true, 180_000, c.pause, c.now); | |
| 34 | expect(got).toBeUndefined(); | |
| 35 | expect(tries).toBe(4); | |
| 36 | }); | |
| 37 | ||
| 38 | test("the job's own failure, a dropped job, or the wait spent: the fetch is answered as it failed", async () => { | |
| 39 | const c = clock(5_000); | |
| 40 | let tries = 0; | |
| 41 | // Its own: no second try. | |
| 42 | expect(await fetchUntilBack(async () => (tries++, "fatal: remote error: not our ref"), () => true, 180_000, c.pause, c.now)).toContain("not our ref"); | |
| 43 | expect(tries).toBe(1); | |
| 44 | // Dropped by the forge meanwhile (the report loop heard a 404 for the job): no second try. | |
| 45 | tries = 0; | |
| 46 | expect(await fetchUntilBack(async () => (tries++, "returned error: 404"), () => false, 180_000, c.pause, c.now)).toContain("404"); | |
| 47 | expect(tries).toBe(1); | |
| 48 | // Away for longer than the wait: handed back once it is spent, after a try every pause. | |
| 49 | tries = 0; | |
| 50 | const d = clock(60_000); | |
| 51 | expect(await fetchUntilBack(async () => (tries++, "returned error: 404"), () => true, 180_000, d.pause, d.now)).toContain("404"); | |
| 52 | expect(tries).toBe(4); | |
| 53 | }); |
new above
offline
Nit:
timed outalso matches git's own FETCH_MS timeout (600 s). That try is then retried and the 180 s wait is not a real bound. Limit each retry to the time left, or leavetimed outout.