Public: anyone can read this; the crew can change it.
Sentinel: 2 deploy runs wait with no slot being used #17123
Merged 42h 17m ago, live in production
scout/forge:scout/16948-sentinel-2-deploy-runs-wait-with-no-slot → main 0 commits fork 1 thread · 1 unresolved
2 files
changed +106
−2
src/apps/runtime_deploy.ts +28 −2
| @@ -21,11 +21,12 @@ import { getPull } from "../core/pulls.ts"; | ||
| 21 | 21 | import { getRepoById, type Repo } from "../core/repos.ts"; |
| 22 | 22 | import { runningLimitSentence } from "../settings/quotas.ts"; |
| 23 | 23 | import { meterOf, trackCompute } from "../core/run_compute.ts"; |
| 24 | import { cancelRun, claimRun, finishRun, markServing, STOPPED_SUMMARY, type Run } from "../core/runs.ts"; | |
| 24 | import { cancelRun, claimRun, getRun, finishRun, markServing, STOPPED_SUMMARY, type Run } from "../core/runs.ts"; | |
| 25 | 25 | import { resolveOid } from "../git/merge.ts"; |
| 26 | 26 | import { repoDir } from "../git/repos.ts"; |
| 27 | 27 | import type { Result } from "../kernel/result.ts"; |
| 28 | 28 | import { holdLease } from "../runs/lease.ts"; |
| 29 | import { trackRun, untrackRun } from "../runs/stoppable.ts"; | |
| 29 | 30 | import { appendRunLog } from "../runs/log.ts"; |
| 30 | 31 | import { readFileAt } from "./checkout.ts"; |
| 31 | 32 | import { seconds, Stopwatch } from "./timing.ts"; |
| @@ -176,11 +177,32 @@ function settleAbandoned(rt: AppsRuntime, state: Environment, bringing: ReturnTy | ||
| 176 | 177 | return settled; |
| 177 | 178 | } |
| 178 | 179 | |
| 180 | /** | |
| 181 | * Work one queued deploy. A person's cancel (`cancelRepoRun`) marks the row and then stops | |
| 182 | * this run by id: the abort ends the bring-up in flight, so the deploy lets go of the permit | |
| 183 | * and the environment's chain rather than carrying on behind a row that says finished (#16948). | |
| 184 | */ | |
| 179 | 185 | export async function execute( |
| 180 | 186 | rt: AppsRuntime, |
| 181 | 187 | state: Environment, |
| 182 | 188 | queued: Repo, |
| 183 | 189 | run: Run, |
| 190 | ): Promise<void> { | |
| 191 | const abort = new AbortController(); | |
| 192 | trackRun(run.id, abort); | |
| 193 | try { | |
| 194 | await executeTracked(rt, state, queued, run, abort); | |
| 195 | } finally { | |
| 196 | untrackRun(run.id, abort); | |
| 197 | } | |
| 198 | } | |
| 199 | ||
| 200 | async function executeTracked( | |
| 201 | rt: AppsRuntime, | |
| 202 | state: Environment, | |
| 203 | queued: Repo, | |
| 204 | run: Run, | |
| 205 | abort: AbortController, | |
| 184 | 206 | ): Promise<void> { |
| 185 | 207 | // The row as it is now, not as it was when the deploy was asked for: a |
| 186 | 208 | // move (#570) between the two gives the repository a new owner and a |
| @@ -190,7 +212,6 @@ export async function execute( | ||
| 190 | 212 | if (!claimed) return; // cancelled meanwhile |
| 191 | 213 | const lease = holdLease(rt.app.db, run.id); |
| 192 | 214 | trackCompute(meterOf(rt.app.db), run.id, `app:${state.app.slug}/${state.env}`); |
| 193 | const abort = new AbortController(); | |
| 194 | 215 | state.deploying = { run: claimed, abort }; |
| 195 | 216 | state.lastRunId = run.id; |
| 196 | 217 | const write = (line: string): void => |
| @@ -281,6 +302,11 @@ export async function execute( | ||
| 281 | 302 | // So is a person stopping or removing this environment while it deployed (`EnvStop`): the run |
| 282 | 303 | // is cancelled with what they did, so the environment reads stopped, not failed. |
| 283 | 304 | const because = !abort.signal.aborted ? undefined : rt.stopped ? STOPPED_SUMMARY : abort.signal.reason instanceof EnvStop ? abort.signal.reason.summary : undefined; |
| 305 | // A person's cancel already marked the row; the run ends as it was marked. | |
| 306 | if (abort.signal.aborted && getRun(rt.app.db, run.id)?.state === "cancelled") { | |
| 307 | write("cancelled: stopped while deploying"); | |
| 308 | return; | |
| 309 | } | |
| 284 | 310 | if (because !== undefined) { |
| 285 | 311 | const done = cancelRun(rt.app.db, run.id, because); |
| 286 | 312 | write(`cancelled: ${because}`); |
test/deploy-cancel-frees-slot.test.ts added +78
| @@ -0,0 +1,78 @@ | ||
| 1 | /** | |
| 2 | * A person's cancel of a running deploy ends its bring-up and frees the permit and the | |
| 3 | * environment's chain, so the next deploy is not left waiting behind a row that says | |
| 4 | * cancelled (#16948). Drives `execute` and `cancelRepoRun`. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, spyOn, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 12 | import { newApp } from "../src/actions/apps.ts"; | |
| 13 | import { cancelRepoRun } from "../src/actions/runs.ts"; | |
| 14 | import { getAppById } from "../src/core/apps.ts"; | |
| 15 | import { getRepoById } from "../src/core/repos.ts"; | |
| 16 | import { createRun, getRun } from "../src/core/runs.ts"; | |
| 17 | import { createUser, type User } from "../src/core/users.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | ||
| 22 | let dataDir = ""; | |
| 23 | let app: App; | |
| 24 | let runtime: AppsRuntime; | |
| 25 | let bring: ReturnType<typeof spyOn>; | |
| 26 | let alice: User; | |
| 27 | let appId = ""; | |
| 28 | let repoId = ""; | |
| 29 | let signal: AbortSignal | undefined; | |
| 30 | let started: () => void = () => {}; | |
| 31 | const up = new Promise<void>((r) => (started = r)); | |
| 32 | ||
| 33 | beforeAll(async () => { | |
| 34 | dataDir = await mkdtemp(join(tmpdir(), "forge-cancel-")); | |
| 35 | app = createApp({ dataDir, port: 0 }); | |
| 36 | runtime = new AppsRuntime(app, { domain: "apps.test", scheme: "http", forgeUrl: "http://forge.test", concurrency: 1 }); | |
| 37 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", | |
| 38 | email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 39 | if (!made.ok) throw new Error(made.error); | |
| 40 | alice = made.value; | |
| 41 | const created = await newApp(app, alice, { name: "hello", description: "d", visibility: "public" }); | |
| 42 | if (!created.ok) throw new Error(String(created.error)); | |
| 43 | appId = created.value.app.id; | |
| 44 | repoId = created.value.repo.id; | |
| 45 | bring = spyOn(AppsRuntime.prototype, "bringUp").mockImplementation( | |
| 46 | async (...a: Parameters<AppsRuntime["bringUp"]>): ReturnType<AppsRuntime["bringUp"]> => { | |
| 47 | if (a[2].env !== "production") return { ok: false, error: "next ran" }; | |
| 48 | signal = a[4]; | |
| 49 | started(); | |
| 50 | await new Promise<void>((done) => signal!.addEventListener("abort", () => done())); | |
| 51 | return { ok: false, error: "aborted" }; | |
| 52 | }, | |
| 53 | ); | |
| 54 | }); | |
| 55 | ||
| 56 | afterAll(async () => { | |
| 57 | bring.mockRestore(); | |
| 58 | app.close(); | |
| 59 | await rm(dataDir, { recursive: true, force: true }); | |
| 60 | }); | |
| 61 | ||
| 62 | test("cancelling a running deploy aborts it, keeps it cancelled, and the next deploy runs", async () => { | |
| 63 | const record = getAppById(app.db, appId)!; | |
| 64 | const repo = getRepoById(app.db, repoId)!; | |
| 65 | const mk = (env: "production" | "pull-1") => createRun(app.db, { repoId, kind: "deploy", env, commitOid: "3".repeat(40), reason: "push" }); | |
| 66 | const a = mk("production"); | |
| 67 | const b = mk("pull-1"); | |
| 68 | const first = runtime.execute(runtime.envFor(record, "production"), repo, a); | |
| 69 | const second = runtime.execute(runtime.envFor(record, "pull-1"), repo, b); | |
| 70 | await up; | |
| 71 | expect(getRun(app.db, a.id)?.state).toBe("running"); | |
| 72 | expect(cancelRepoRun(app, alice, repo, a.id).ok).toBe(true); | |
| 73 | await Promise.race([Promise.all([first, second]), new Promise((_, no) => setTimeout(() => no(new Error("slot held")), 5_000))]); | |
| 74 | expect(signal?.aborted).toBe(true); | |
| 75 | expect(getRun(app.db, a.id)?.state).toBe("cancelled"); | |
| 76 | expect(getRun(app.db, a.id)?.summary).toBe("cancelled by alice"); | |
| 77 | expect(getRun(app.db, b.id)?.summary).toContain("next ran"); | |
| 78 | }); |
new above
offline
This only runs when the bring-up failed. A cancel that lands after bringUp succeeds (gate tests, switch) is not seen here, so the deploy can finish and mark itself serving behind a cancelled row. Next fix: check abort.signal after the gate too.