Skip to content

Commit 5c68c8f

Browse files
authored
fix(ci): keep slow startup from failing cancellation tests (#132203)
* fix(ci): keep slow startup from failing cancellation tests * test(ci): canonicalize checkout process records * test(ci): reuse the live process census for cancellation
1 parent cac0ec2 commit 5c68c8f

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎test/scripts/ci-platform-checkout.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Report = {
1616
boundaries: Boundary[];
1717
readyAttempts: number[];
1818
cleanupRemaining: ProcessRecord[];
19+
ownedProcesses: ProcessRecord[];
1920
commands: { cwd: string; args: string[] }[];
2021
output: string;
2122
};
@@ -84,6 +85,10 @@ it.each([
8485
const root = realpathSync(mkdtempSync(path.join(tmpdir(), "ci platform checkout ")));
8586
const workspace = path.join(root, "workspace");
8687
mkdirSync(workspace);
88+
if (scenario.startsWith("cancel-")) {
89+
// Inject slow startup before fetch, beyond the former cancellation readiness deadline.
90+
writeFileSync(path.join(root, "fixture-config.json"), JSON.stringify({ initDelayMs: 4_100 }));
91+
}
8792
if (linux) {
8893
writeFileSync(path.join(workspace, ".previous-checkout"), "stale\n");
8994
}
@@ -171,6 +176,23 @@ def run_git(`,
171176
expect(report.output.includes("refusing reuse or retry")).toBe(
172177
scenario === "cleanup-failure",
173178
);
179+
if (scenario.startsWith("cancel-")) {
180+
const alive = report.ownedProcesses.filter((entry) => entry.attempt === 1);
181+
expect(alive.map((entry) => entry.role).toSorted()).toEqual([
182+
"child",
183+
"grandchild",
184+
"parent",
185+
]);
186+
const owner = expectDefined(
187+
report.ownedProcesses.find((entry) => entry.role === "shell"),
188+
"workflow owner",
189+
);
190+
expect(owner.pid).toBeGreaterThan(1);
191+
const signal = scenario.slice("cancel-".length);
192+
expect(report.output).toContain(
193+
`cancellation: ${JSON.stringify({ signal, owner: owner.pid, alive })}\n`,
194+
);
195+
}
174196
if (code === 0) {
175197
const fetches = report.commands.filter(({ args }) => args.includes("fetch"));
176198
const candidateFetch = expectDefined(fetches[0], "candidate fetch");

‎test/scripts/fixtures/ci-platform-checkout.mjs‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ function record(pid, role, attempt = 0) {
2727
}
2828

2929
function records() {
30+
// Keep producer observations and shutdown reports in the same order.
3031
return fs
3132
.readdirSync(recordsDir)
3233
.filter((file) => file.endsWith(".json"))
34+
.toSorted()
3335
.map((file) => JSON.parse(fs.readFileSync(path.join(recordsDir, file), "utf8")));
3436
}
3537

@@ -157,6 +159,10 @@ async function command() {
157159
const operation = args.shift();
158160
if (operation === "init") {
159161
boundary("init");
162+
const config = path.join(root, "fixture-config.json");
163+
if (fs.existsSync(config)) {
164+
await delay(JSON.parse(fs.readFileSync(config, "utf8")).initDelayMs);
165+
}
160166
fs.mkdirSync(insideWorkspace(args[0]), { recursive: true });
161167
if (linux && cwd === workspace) {
162168
if (fs.readdirSync(workspace).length !== 0) {
@@ -172,6 +178,26 @@ async function command() {
172178
record(process.pid, "parent", attempt);
173179
launch("child", attempt);
174180
await until(() => fs.existsSync(path.join(root, `ready-${attempt}.json`)), "tree readiness");
181+
if (scenario.startsWith("cancel-")) {
182+
const owned = liveRecords();
183+
const alive = owned.filter((entry) => entry.attempt === attempt);
184+
if (
185+
!["parent", "child", "grandchild"].every((role) =>
186+
alive.some((entry) => entry.role === role),
187+
)
188+
) {
189+
throw new Error("Cancellation tree is no longer alive");
190+
}
191+
const owner = owned.find((entry) => entry.role === "shell");
192+
// Both shells exec their replacements. Validate the current Python parent,
193+
// never an orphan's new parent or the Git group, before sending cancellation.
194+
if (!owner || owner.pid <= 1 || process.ppid !== owner.pid) {
195+
throw new Error("Cancellation owner is no longer the registered workflow parent");
196+
}
197+
const signal = scenario.slice("cancel-".length);
198+
fs.writeSync(1, `cancellation: ${JSON.stringify({ signal, owner: owner.pid, alive })}\n`);
199+
process.kill(owner.pid, signal);
200+
}
175201
if (scenario === "early-leader-exit") {
176202
process.exit(0);
177203
}
@@ -195,7 +221,7 @@ async function command() {
195221
}
196222
// Expire the two-second fetch budget only after the full tree is ready.
197223
// Immutable ticks avoid replacing a clock file held open by Windows readers.
198-
// Cancellation scenarios instead wait for the supervisor's actual signal.
224+
// Cancellation scenarios signal from the ready fetch without advancing this clock.
199225
if (!scenario.startsWith("cancel-")) {
200226
publish(`fetch-tick-${attempt}.json`, attempt);
201227
}
@@ -402,11 +428,6 @@ async function supervise() {
402428
record(shell.pid, "shell");
403429
}
404430
const exited = once(shell, "exit");
405-
if (scenario.startsWith("cancel-")) {
406-
await until(() => fs.existsSync(path.join(root, "ready-1.json")), "cancellation readiness");
407-
// exec replaces Bash on POSIX: this is the owner, not the Git group.
408-
shell.kill(scenario.slice("cancel-".length));
409-
}
410431
const [code] = await exited;
411432
report.code = code;
412433
boundary("exit");

0 commit comments

Comments
 (0)