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

A stop asked while a hosted forge is still starting waits and stops what came up #14207

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14205-a-stop-asked-while-a-hosted-forge-is-sti → main 2 commits fork

  • Merged. scout/14205-a-stop-asked-while-a-hosted-forge-is-sti was merged into main as 12eaae0.

Review

Sign in to review this change.
2 files changed +118 −0
src/spawn/cell.ts +5
@@ -611,6 +611,11 @@ export function makeRegistry(parent: App, launcher?: Launcher): CellChildren {
611611 return flight;
612612 },
613613 async stop(bodyId) {
614 // A start asked before adoption is over begins after it: this stop takes its turn behind it.
615 if (!adoptionOver) await adopted;
616 // Asked while it is starting: the start finishes, and what came up is the one to stop.
617 const coming = starting.get(bodyId);
618 if (coming !== undefined) await coming.catch(() => {});
614619 const leaving = stopping.get(bodyId);
615620 if (leaving !== undefined) return leaving;
616621 const child = held.get(bodyId);
test/stop-while-starting.test.ts added +113
@@ -0,0 +1,113 @@
1 /**
2 * A stop asked while a forge is starting waits for the start and stops what came up (#14205).
3 */
4 import { afterAll, describe, expect, test } from "bun:test";
5 import { mkdtempSync, rmSync } from "node:fs";
6 import { tmpdir } from "node:os";
7 import { join } from "node:path";
8 import { createApp } from "../src/app.ts";
9 import { cellLimits, makeRegistry, type CellChild } from "../src/spawn/cell.ts";
10 import { quietLogs } from "./helpers/globals.ts";
11
12 quietLogs();
13 const tmp = mkdtempSync(join(tmpdir(), "one-process-"));
14 afterAll(() => rmSync(tmp, { recursive: true, force: true }));
15
16 /**
17 * A stand-in for a forge that is slow to leave: it says "ready" once it listens, says "term" when
18 * told to go, and goes (exit 7) only when the test closes its stdin.
19 */
20 function slowLeaver() {
21 const proc = Bun.spawn(
22 [process.execPath, "-e", "process.on('SIGTERM', () => console.log('term')); process.stdin.on('end', () => process.exit(7)); process.stdin.resume(); console.log('ready');"],
23 { stdin: "pipe", stdout: "pipe", stderr: "ignore" },
24 );
25 const reader = proc.stdout.getReader();
26 let said = "";
27 const says = async (word: string): Promise<void> => {
28 while (!said.includes(word)) {
29 const { value, done } = await reader.read();
30 if (done) throw new Error(`the process ended before it said "${word}"`);
31 said += new TextDecoder().decode(value);
32 }
33 };
34 return { proc, says, leave: () => void proc.stdin.end() };
35 }
36
37 /** A registry whose children are slow leavers; `fails` makes the launcher give up instead. */
38 function watched(name: string, fails?: "none" | "throws") {
39 const parent = createApp({ dataDir: join(tmp, name), url: "https://f.test" });
40 const made: ReturnType<typeof slowLeaver>[] = [];
41 const alive = new Set<number>();
42 const cells = makeRegistry(parent, async (input): Promise<CellChild> => {
43 if (fails === "none") return undefined as unknown as CellChild;
44 if (fails === "throws") throw new Error("no cell");
45 const one = slowLeaver();
46 made.push(one);
47 alive.add(one.proc.pid);
48 void one.proc.exited.then(() => alive.delete(one.proc.pid));
49 await one.says("ready");
50 return { ...input, port: 1, limits: cellLimits("small"), pid: one.proc.pid, exited: one.proc.exited };
51 });
52 const end = (): void => {
53 for (const one of made) one.proc.kill("SIGKILL");
54 parent.close();
55 };
56 return { parent, cells, made, alive, end };
57 }
58
59 const KID = { bodyId: "cell-one", name: "kid", size: "small" } as const;
60
61 describe("stop while starting", () => {
62 test("a stop asked while the forge is starting waits for the start and stops what came up", async () => {
63 const w = watched("start");
64 try {
65 const starting = w.cells.start(KID);
66 const stopping = w.cells.stop(KID.bodyId);
67 const child = await starting;
68 expect(child?.pid).toBe(w.made[0]!.proc.pid);
69 await w.made[0]!.says("term");
70 w.made[0]!.leave();
71 await stopping;
72 expect(w.alive.size).toBe(0);
73 expect(w.cells.get(KID.bodyId)).toBeUndefined();
74 expect(w.made.length).toBe(1);
75 } finally {
76 w.end();
77 }
78 });
79
80 test("two stops asked while starting both resolve, with one process stopped and none left", async () => {
81 const w = watched("twice");
82 try {
83 const starting = w.cells.start(KID);
84 const first = w.cells.stop(KID.bodyId);
85 const second = w.cells.stop(KID.bodyId);
86 await starting;
87 await w.made[0]!.says("term");
88 w.made[0]!.leave();
89 await Promise.all([first, second]);
90 expect(w.made.length).toBe(1);
91 expect(w.alive.size).toBe(0);
92 expect(w.cells.get(KID.bodyId)).toBeUndefined();
93 } finally {
94 w.end();
95 }
96 });
97
98 for (const how of ["none", "throws"] as const) {
99 test(`a stop asked while a start fails (${how}) resolves and holds nothing`, async () => {
100 const w = watched(`fails-${how}`, how);
101 try {
102 const starting = w.cells.start(KID);
103 const stopping = w.cells.stop(KID.bodyId);
104 expect(await starting).toBeUndefined();
105 await stopping;
106 expect(w.made.length).toBe(0);
107 expect(w.cells.get(KID.bodyId)).toBeUndefined();
108 } finally {
109 w.end();
110 }
111 });
112 }
113 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs