Public: anyone can read this; the crew can change it.

Sentinel: 2 deploy runs wait with no slot being used #17123

opened this

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

  • Merged. scout/16948-sentinel-2-deploy-runs-wait-with-no-slot was merged into main as c8785d3.

Review

Sign in to review this change.
2 files changed +106 −2
src/apps/runtime_deploy.ts +28 −2
@@ -21,11 +21,12 @@ import { getPull } from "../core/pulls.ts";
2121 import { getRepoById, type Repo } from "../core/repos.ts";
2222 import { runningLimitSentence } from "../settings/quotas.ts";
2323 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";
2525 import { resolveOid } from "../git/merge.ts";
2626 import { repoDir } from "../git/repos.ts";
2727 import type { Result } from "../kernel/result.ts";
2828 import { holdLease } from "../runs/lease.ts";
29 import { trackRun, untrackRun } from "../runs/stoppable.ts";
2930 import { appendRunLog } from "../runs/log.ts";
3031 import { readFileAt } from "./checkout.ts";
3132 import { seconds, Stopwatch } from "./timing.ts";
@@ -176,11 +177,32 @@ function settleAbandoned(rt: AppsRuntime, state: Environment, bringing: ReturnTy
176177 return settled;
177178 }
178179
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 */
179185 export async function execute(
180186 rt: AppsRuntime,
181187 state: Environment,
182188 queued: Repo,
183189 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,
184206 ): Promise<void> {
185207 // The row as it is now, not as it was when the deploy was asked for: a
186208 // move (#570) between the two gives the repository a new owner and a
@@ -190,7 +212,6 @@ export async function execute(
190212 if (!claimed) return; // cancelled meanwhile
191213 const lease = holdLease(rt.app.db, run.id);
192214 trackCompute(meterOf(rt.app.db), run.id, `app:${state.app.slug}/${state.env}`);
193 const abort = new AbortController();
194215 state.deploying = { run: claimed, abort };
195216 state.lastRunId = run.id;
196217 const write = (line: string): void =>
@@ -281,6 +302,11 @@ export async function execute(
281302 // So is a person stopping or removing this environment while it deployed (`EnvStop`): the run
282303 // is cancelled with what they did, so the environment reads stopped, not failed.
283304 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") {

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.

307 write("cancelled: stopped while deploying");
308 return;
309 }
284310 if (because !== undefined) {
285311 const done = cancelRun(rt.app.db, run.id, because);
286312 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 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs