GitHub runners: replace a runner whose container stopped before it took a job
While a container start that Cloudflare had no machine for is tried again, the containers library's alarm reports the container stopped with exit code 0. Diffinery then ended the runner's row and removed the runner from GitHub, and the job it was started for waited 24 hours for another runner: 2 of 10 jobs on 2026-10-30 and 3 of 10 on 2026-10-31. - A GitHub runner's container ignores stops reported while Diffinery's own start() for it is still under way. - A runner that stops before it took a job, without crashing, is replaced (up to MAX_TRIES) while GitHub still has its job queued. - The container says which container stopped, so a late stop of one the row no longer runs in changes nothing.
7 files changed+175−28
Files (7)
- apps/diffinery/docs/actions/github-runners.md+2−0
- apps/diffinery/src/github-runners/GitHubRunnersDO.ts+3−3
- apps/diffinery/src/github-runners/github.ts+10−0
- apps/diffinery/src/github-runners/service.ts+28−12
- apps/diffinery/src/runner/containers.ts+25−13
- apps/diffinery/test/unit/github-runners.test.ts+47−0
- apps/diffinery/test/unit/runner-containers.test.ts+60−0
modifiedapps/diffinery/docs/actions/github-runners.md+2−0View file
| @@ -128,4 +128,6 @@ | ||
| 128 | 128 | |
| 129 | 129 | **"The runner stopped before it took a job (exit code N); starting another".** GitHub's runner exited before a job started, for example because GitHub was unreachable. Diffinery tries up to 3 runners for a job. |
| 130 | 130 | |
| 131 | +**"The runner's container stopped before it took a job; starting another".** The runner's container stopped without an error before GitHub handed it a job, for example because Cloudflare lost the machine it ran on. GitHub still had the job queued, so Diffinery started another runner for it. As with a runner that exited with an error, Diffinery tries up to 3 runners for a job. | |
| 132 | + | |
| 131 | 133 | **Settings → GitHub runners says "This Diffinery has no GitHub App".** The instance's administrator hasn't set the app up. See [Configuration](/docs/self-hosting/configuration#github-runners). |
modifiedapps/diffinery/src/github-runners/GitHubRunnersDO.ts+3−3View file
| @@ -40,11 +40,11 @@ | ||
| 40 | 40 | await this.wake(accountId, 0); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - /** A runner's container stopped (runner/containers.ts onStop). */ | |
| 44 | - async stopped(accountId: string, runnerId: string, exitCode: number | null): Promise<void> { | |
| 43 | + /** A runner's container stopped (runner/containers.ts onStop); containers started before it said which don't. */ | |
| 44 | + async stopped(accountId: string, runnerId: string, exitCode: number | null, container?: string): Promise<void> { | |
| 45 | 45 | const r = this.runners(); |
| 46 | 46 | if (!r) return; |
| 47 | - await r.stopped(runnerId, exitCode); | |
| 47 | + await r.stopped(runnerId, exitCode, container); | |
| 48 | 48 | await this.wake(accountId, 0); |
| 49 | 49 | } |
| 50 | 50 |
modifiedapps/diffinery/src/github-runners/github.ts+10−0View file
| @@ -193,6 +193,16 @@ | ||
| 193 | 193 | } |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | + /** A job's status on GitHub (queued, in_progress, completed and others); null once GitHub no longer has it. */ | |
| 197 | + async jobStatus(installationId: number, repo: string, jobId: number): Promise<string | null> { | |
| 198 | + try { | |
| 199 | + return (await this.asInstallation<{ status: string }>(installationId, "GET", `repos/${repo}/actions/jobs/${jobId}`)).status; | |
| 200 | + } catch (e) { | |
| 201 | + if (e instanceof GitHubApiError && e.status === 404) return null; | |
| 202 | + throw e; | |
| 203 | + } | |
| 204 | + } | |
| 205 | + | |
| 196 | 206 | /** Removes a runner that never took a job (one that did removes itself). */ |
| 197 | 207 | async removeRunner(installationId: number, repo: string, runnerId: number): Promise<void> { |
| 198 | 208 | try { |
modifiedapps/diffinery/src/github-runners/service.ts+28−12View file
| @@ -107,7 +107,7 @@ | ||
| 107 | 107 | export function containerHost(env: Env): RunnerHost { |
| 108 | 108 | const ns = (container: string) => env[RUNNER_SIZES[sizeOfContainer(container)].binding] ?? env.RUNNER; |
| 109 | 109 | return { |
| 110 | - start: (container, input) => ns(container).get(ns(container).idFromName(container)).startGitHubRunner(input), | |
| 110 | + start: (container, input) => ns(container).get(ns(container).idFromName(container)).startGitHubRunner({ ...input, container }), | |
| 111 | 111 | stop: (container) => ns(container).get(ns(container).idFromName(container)).stopJob(false), |
| 112 | 112 | has: (size) => size === "large" || !!env[RUNNER_SIZES[size].binding], |
| 113 | 113 | }; |
| @@ -355,23 +355,39 @@ | ||
| 355 | 355 | await this.ended(row.id, null); |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | - /** A runner's container stopped (runner/containers.ts onStop), or Diffinery stopped it. */ | |
| 359 | - async stopped(rowId: string, exitCode: number | null): Promise<void> { | |
| 358 | + /** A runner's container stopped (runner/containers.ts onStop), or Diffinery stopped it. `container` is the one that stopped, when it says. */ | |
| 359 | + async stopped(rowId: string, exitCode: number | null, container?: string): Promise<void> { | |
| 360 | 360 | const row = await this.row(rowId); |
| 361 | 361 | if (!row) return; |
| 362 | - // A runner that crashed before it took a job (nothing of the job ran) is replaced, a few times. | |
| 363 | - if (row.status === "starting" && !row.stop_reason && exitCode !== 0 && exitCode !== null && row.tries < MAX_TRIES) { | |
| 364 | - if (row.container) await endCompute(this.env, row.container).catch(() => {}); | |
| 365 | - if (row.github_runner_id) await this.gh.removeRunner(row.installation_id, row.repo, row.github_runner_id).catch(() => {}); | |
| 366 | - await this.db | |
| 367 | - .prepare("UPDATE github_runner_jobs SET status = 'queued', container = NULL, message = ? WHERE id = ? AND status = 'starting'") | |
| 368 | - .bind(`The runner stopped before it took a job (exit code ${exitCode}); starting another`, row.id) | |
| 369 | - .run(); | |
| 370 | - return; | |
| 362 | + // A container the row no longer runs in: one given up on (back in line, or replaced) that stopped late. | |
| 363 | + if (container && row.container !== container) return; | |
| 364 | + // A runner that stopped before it took a job (nothing of the job ran) is replaced, a few times: one that crashed, | |
| 365 | + // and one that stopped without crashing while GitHub still has its job queued. The containers library reports | |
| 366 | + // exit code 0 for a container the platform lost, and no runner took the job then. | |
| 367 | + if (row.status === "starting" && !row.stop_reason && row.tries < MAX_TRIES) { | |
| 368 | + const crashed = exitCode !== 0 && exitCode !== null; | |
| 369 | + if (crashed || (await this.stillQueued(row))) { | |
| 370 | + if (row.container) await endCompute(this.env, row.container).catch(() => {}); | |
| 371 | + if (row.github_runner_id) await this.gh.removeRunner(row.installation_id, row.repo, row.github_runner_id).catch(() => {}); | |
| 372 | + await this.db | |
| 373 | + .prepare("UPDATE github_runner_jobs SET status = 'queued', container = NULL, message = ? WHERE id = ? AND status = 'starting'") | |
| 374 | + .bind(crashed ? `The runner stopped before it took a job (exit code ${exitCode}); starting another` : "The runner's container stopped before it took a job; starting another", row.id) | |
| 375 | + .run(); | |
| 376 | + return; | |
| 377 | + } | |
| 371 | 378 | } |
| 372 | 379 | await this.ended(rowId, exitCode); |
| 373 | 380 | } |
| 374 | 381 | |
| 382 | + /** | |
| 383 | + * Whether GitHub still has a row's job queued. When GitHub doesn't answer, it counts as queued: a job left | |
| 384 | + * without a runner waits 24 hours, a runner started for nothing only IDLE_MS. | |
| 385 | + */ | |
| 386 | + private async stillQueued(row: RunnerJobRow): Promise<boolean> { | |
| 387 | + const status = await this.gh.jobStatus(row.installation_id, row.repo, row.github_job_id).catch(() => "queued"); | |
| 388 | + return status === "queued"; | |
| 389 | + } | |
| 390 | + | |
| 375 | 391 | private async ended(rowId: string, exitCode: number | null): Promise<void> { |
| 376 | 392 | const row = await this.row(rowId); |
| 377 | 393 | if (!row) return; |
modifiedapps/diffinery/src/runner/containers.ts+25−13View file
| @@ -40,7 +40,7 @@ | ||
| 40 | 40 | /** The job a runner container runs; stopRequested once Diffinery asked it to stop (stopJob). */ |
| 41 | 41 | type RunnerJob = { repoId: string; jobId: string; container?: string; stopRequested?: boolean }; |
| 42 | 42 | /** A GitHub job's runner (github-runners/): GitHub's own runner, for the account that pays. */ |
| 43 | -type GitHubRunner = { accountId: string; runnerId: string }; | |
| 43 | +type GitHubRunner = { accountId: string; runnerId: string; container?: string }; | |
| 44 | 44 | |
| 45 | 45 | /** How long a job's container may take to come up, and how often it is checked meanwhile. */ |
| 46 | 46 | const START_WAIT_MS = 180_000; |
| @@ -53,6 +53,8 @@ | ||
| 53 | 53 | export class RunnerContainer extends HostedContainer { |
| 54 | 54 | // Jobs make no inbound requests, so activity-based sleep must not stop them. |
| 55 | 55 | override sleepAfter = "6h"; |
| 56 | + /** True while start() runs (startRunner). */ | |
| 57 | + private starting = false; | |
| 56 | 58 | |
| 57 | 59 | async startJob(input: { repoId: string; jobId: string; container?: string; token: string; apiUrl: string; egress?: Egress; vcpu?: number }): Promise<void> { |
| 58 | 60 | await this.ctx.storage.put("job", { repoId: input.repoId, jobId: input.jobId, container: input.container }); |
| @@ -71,21 +73,26 @@ | ||
| 71 | 73 | * configuration GitHub made for it, which holds the runner's credentials, so it is only ever in the |
| 72 | 74 | * container's environment. |
| 73 | 75 | */ |
| 74 | - async startGitHubRunner(input: { accountId: string; runnerId: string; jitConfig: string; egress: Egress; vcpu: number }): Promise<void> { | |
| 75 | - await this.ctx.storage.put("github", { accountId: input.accountId, runnerId: input.runnerId } satisfies GitHubRunner); | |
| 76 | + async startGitHubRunner(input: { accountId: string; runnerId: string; container?: string; jitConfig: string; egress: Egress; vcpu: number }): Promise<void> { | |
| 77 | + await this.ctx.storage.put("github", { accountId: input.accountId, runnerId: input.runnerId, container: input.container } satisfies GitHubRunner); | |
| 76 | 78 | await this.startRunner({ DIFFINERY_GITHUB_JIT_CONFIG: input.jitConfig, DIFFINERY_RUNNER_VCPU: String(input.vcpu), ...(await this.useEgress(input.egress)) }); |
| 77 | 79 | } |
| 78 | 80 | |
| 79 | 81 | private async startRunner(envVars: Record<string, string>): Promise<void> { |
| 80 | - await this.start( | |
| 81 | - { envVars }, | |
| 82 | - // The library gives up after 8 s and reports "no container instance", but a container on a host | |
| 83 | - // that hasn't pulled the runner image yet takes longer than that to come up: such jobs went back | |
| 84 | - // in line for 30 to 90 s more (measured on 10-leg matrices). Wait for it instead. | |
| 85 | - // The runner listens on no port: the library's own fallback port (33) answers "not listening" once it is up. | |
| 86 | - // Each check can itself take up to 5 s, so the signal bounds the whole wait. | |
| 87 | - { portToCheck: 33, waitInterval: START_POLL_MS, retries: Math.ceil(START_WAIT_MS / START_POLL_MS), signal: AbortSignal.timeout(START_WAIT_MS + 60_000) }, | |
| 88 | - ); | |
| 82 | + this.starting = true; | |
| 83 | + try { | |
| 84 | + await this.start( | |
| 85 | + { envVars }, | |
| 86 | + // The library gives up after 8 s and reports "no container instance", but a container on a host | |
| 87 | + // that hasn't pulled the runner image yet takes longer than that to come up: such jobs went back | |
| 88 | + // in line for 30 to 90 s more (measured on 10-leg matrices). Wait for it instead. | |
| 89 | + // The runner listens on no port: the library's own fallback port (33) answers "not listening" once it is up. | |
| 90 | + // Each check can itself take up to 5 s, so the signal bounds the whole wait. | |
| 91 | + { portToCheck: 33, waitInterval: START_POLL_MS, retries: Math.ceil(START_WAIT_MS / START_POLL_MS), signal: AbortSignal.timeout(START_WAIT_MS + 60_000) }, | |
| 92 | + ); | |
| 93 | + } finally { | |
| 94 | + this.starting = false; | |
| 95 | + } | |
| 89 | 96 | } |
| 90 | 97 | |
| 91 | 98 | /** |
| @@ -106,8 +113,13 @@ | ||
| 106 | 113 | override async onStop(params: StopParams): Promise<void> { |
| 107 | 114 | const gh = await this.ctx.storage.get<GitHubRunner>("github"); |
| 108 | 115 | if (gh) { |
| 116 | + // While a start the platform had no machine for is tried again, the library's alarm reports the container | |
| 117 | + // stopped with exit code 0, though nothing ran and the start goes on. Told so, Diffinery removed the runner | |
| 118 | + // from GitHub and its job waited 24 hours for another (2 or 3 jobs of 10 started at once). The start ends in | |
| 119 | + // a running runner, or in an error that github-runners/service.ts place() handles. | |
| 120 | + if (this.starting) return; | |
| 109 | 121 | const ns = this.env.GITHUB_RUNNERS; |
| 110 | - if (ns) await ns.get(ns.idFromName(gh.accountId)).stopped(gh.accountId, gh.runnerId, params.exitCode ?? null); | |
| 122 | + if (ns) await ns.get(ns.idFromName(gh.accountId)).stopped(gh.accountId, gh.runnerId, params.exitCode ?? null, gh.container); | |
| 111 | 123 | await this.ctx.storage.delete("github"); |
| 112 | 124 | return; |
| 113 | 125 | } |
modifiedapps/diffinery/test/unit/github-runners.test.ts+47−0View file
| @@ -41,6 +41,7 @@ | ||
| 41 | 41 | const gh = { |
| 42 | 42 | fail: null as Error | null, |
| 43 | 43 | busy: false as boolean | null, |
| 44 | + jobState: "queued" as string | Error | null, | |
| 44 | 45 | async jitRunner(installationId: number, repo: string, name: string, labels: string[]) { |
| 45 | 46 | calls.push({ op: "jit", args: [installationId, repo, name, labels] }); |
| 46 | 47 | if (gh.fail) throw gh.fail; |
| @@ -53,6 +54,11 @@ | ||
| 53 | 54 | async removeRunner(installationId: number, repo: string, runnerId: number) { |
| 54 | 55 | calls.push({ op: "remove", args: [installationId, repo, runnerId] }); |
| 55 | 56 | }, |
| 57 | + async jobStatus(installationId: number, repo: string, jobId: number) { | |
| 58 | + calls.push({ op: "job", args: [installationId, repo, jobId] }); | |
| 59 | + if (gh.jobState instanceof Error) throw gh.jobState; | |
| 60 | + return gh.jobState; | |
| 61 | + }, | |
| 56 | 62 | }; |
| 57 | 63 | return { gh, calls, client: gh as unknown as GitHubAppClient }; |
| 58 | 64 | } |
| @@ -319,6 +325,47 @@ | ||
| 319 | 325 | expect(w.started.map((s) => s.container)).toEqual([1, 2, 3].map((n) => `gh-${w.rows()[0]!.id}-${n}`)); |
| 320 | 326 | }); |
| 321 | 327 | |
| 328 | + it("replace a runner whose container stopped before it took a job while GitHub still has the job queued", async () => { | |
| 329 | + const w = world(); | |
| 330 | + await w.svc.queued("ann", job(1), "large"); | |
| 331 | + await w.svc.pump("ann"); | |
| 332 | + const first = w.rows()[0]!; | |
| 333 | + // The library reports exit code 0 for a container the platform lost: the job still waits on GitHub. | |
| 334 | + await w.svc.stopped(first.id, 0, first.container!); | |
| 335 | + expect(w.rows()[0]).toMatchObject({ status: "queued", tries: 1, container: null, message: "The runner's container stopped before it took a job; starting another" }); | |
| 336 | + expect(w.calls.filter((c) => c.op === "job" || c.op === "remove")).toEqual([ | |
| 337 | + { op: "job", args: [11, "ann-gh/app", 1] }, | |
| 338 | + { op: "remove", args: [11, "ann-gh/app", 501] }, | |
| 339 | + ]); | |
| 340 | + expect(w.compute()[0]!.ended_at).not.toBeNull(); | |
| 341 | + // The lost container's stop again, late: the row no longer runs in it. | |
| 342 | + await w.svc.stopped(first.id, 0, first.container!); | |
| 343 | + expect(w.rows()[0]).toMatchObject({ status: "queued", tries: 1 }); | |
| 344 | + | |
| 345 | + // GitHub doesn't answer: the job counts as queued. Without an exit code it is the same. | |
| 346 | + await w.svc.pump("ann"); | |
| 347 | + w.gh.jobState = new GitHubApiError(502, "bad gateway"); | |
| 348 | + await w.svc.stopped(first.id, null, w.rows()[0]!.container!); | |
| 349 | + expect(w.rows()[0]).toMatchObject({ status: "queued", tries: 2 }); | |
| 350 | + | |
| 351 | + // The job ran on another runner or was cancelled meanwhile: the row ends. | |
| 352 | + await w.svc.pump("ann"); | |
| 353 | + w.gh.jobState = "completed"; | |
| 354 | + await w.svc.stopped(first.id, 0, w.rows()[0]!.container!); | |
| 355 | + expect(w.rows()[0]).toMatchObject({ status: "completed", tries: 3, exit_code: 0, message: null }); | |
| 356 | + expect(w.started.map((s) => s.container)).toEqual([1, 2, 3].map((n) => `gh-${first.id}-${n}`)); | |
| 357 | + | |
| 358 | + // A runner that took its job and stopped isn't asked about. | |
| 359 | + const v = world(); | |
| 360 | + await v.svc.queued("ann", job(1), "large"); | |
| 361 | + await v.svc.pump("ann"); | |
| 362 | + const row = v.rows()[0]!; | |
| 363 | + await v.svc.jobEvent({ installationId: 11, githubJobId: 1, action: "in_progress", runnerName: row.runner_name, jobName: "test 1", conclusion: null }); | |
| 364 | + await v.svc.stopped(row.id, 0, row.container!); | |
| 365 | + expect(v.rows()[0]).toMatchObject({ status: "completed", exit_code: 0 }); | |
| 366 | + expect(v.calls.filter((c) => c.op === "job")).toEqual([]); | |
| 367 | + }); | |
| 368 | + | |
| 322 | 369 | it("stop a runner no job took within the idle time, unless GitHub says it is busy", async () => { |
| 323 | 370 | const w = world(); |
| 324 | 371 | await w.svc.queued("ann", job(1), "large"); |
addedapps/diffinery/test/unit/runner-containers.test.ts+60−0View file
| @@ -1,0 +1,60 @@ | ||
| 1 | +import { describe, expect, it, vi } from "vitest"; | |
| 2 | +import type { Env } from "../../src/env.js"; | |
| 3 | + | |
| 4 | +// The containers library's Container, with a start() each test gives: the real one needs the Workers runtime. | |
| 5 | +const lib = vi.hoisted(() => ({ start: async (_self: { onStop(p: { exitCode?: number; reason: string }): Promise<void> }): Promise<void> => {} })); | |
| 6 | +vi.mock("@cloudflare/containers", () => ({ | |
| 7 | + Container: class { | |
| 8 | + constructor( | |
| 9 | + protected ctx: unknown, | |
| 10 | + protected env: unknown, | |
| 11 | + ) {} | |
| 12 | + async start(): Promise<void> { | |
| 13 | + await lib.start(this as never); | |
| 14 | + } | |
| 15 | + async setAllowedHosts(): Promise<void> {} | |
| 16 | + }, | |
| 17 | +})); | |
| 18 | + | |
| 19 | +const { RunnerContainer } = await import("../../src/runner/containers.js"); | |
| 20 | + | |
| 21 | +function runner() { | |
| 22 | + const kept = new Map<string, unknown>(); | |
| 23 | + const ctx = { | |
| 24 | + storage: { | |
| 25 | + kv: { get: () => undefined, put: () => {} }, | |
| 26 | + put: async (k: string, v: unknown) => void kept.set(k, v), | |
| 27 | + get: async (k: string) => kept.get(k), | |
| 28 | + delete: async (k: string) => kept.delete(k), | |
| 29 | + }, | |
| 30 | + }; | |
| 31 | + const stops: unknown[][] = []; | |
| 32 | + const env = { GITHUB_RUNNERS: { idFromName: (n: string) => n, get: () => ({ stopped: async (...a: unknown[]) => void stops.push(a) }) } }; | |
| 33 | + return { c: new RunnerContainer(ctx as never, env as unknown as Env), stops, kept }; | |
| 34 | +} | |
| 35 | + | |
| 36 | +const input = { accountId: "ann", runnerId: "row1", container: "gh-row1-1", jitConfig: "jit", egress: "open" as const, vcpu: 4 }; | |
| 37 | + | |
| 38 | +describe("a GitHub runner's container", () => { | |
| 39 | + it("isn't reported stopped while its start is still under way", async () => { | |
| 40 | + const { c, stops } = runner(); | |
| 41 | + // The platform had no machine for the first try: the library's alarm reports the container stopped with | |
| 42 | + // exit code 0 while start() tries again, and the second try starts it. | |
| 43 | + lib.start = async (self) => self.onStop({ exitCode: 0, reason: "exit" }); | |
| 44 | + await c.startGitHubRunner(input); | |
| 45 | + expect(stops).toEqual([]); | |
| 46 | + // Its real stop, after the job, with the container that stopped. | |
| 47 | + await c.onStop({ exitCode: 0, reason: "exit" }); | |
| 48 | + expect(stops).toEqual([["ann", "row1", 0, "gh-row1-1"]]); | |
| 49 | + }); | |
| 50 | + | |
| 51 | + it("is reported stopped after a start that failed", async () => { | |
| 52 | + const { c, stops } = runner(); | |
| 53 | + lib.start = async () => { | |
| 54 | + throw new Error("there is no container instance that can be provided to this durable object"); | |
| 55 | + }; | |
| 56 | + await expect(c.startGitHubRunner(input)).rejects.toThrow(/no container instance/); | |
| 57 | + await c.onStop({ exitCode: 1, reason: "exit" }); | |
| 58 | + expect(stops).toEqual([["ann", "row1", 1, "gh-row1-1"]]); | |
| 59 | + }); | |
| 60 | +}); |