Skip to content

Commit 09bcea8

Browse files
fix(scripts): time out the patch check like other steps
The unused-patch install ran through spawnSync, so a timeout killed only vp and was reported as FAIL. It now runs through the same spawn path as the other commands. The memory budget uses process.availableMemory() so a cgroup limit counts, and timeouts above Node's 32-bit timer maximum are clamped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 357639b commit 09bcea8

3 files changed

Lines changed: 38 additions & 34 deletions

File tree

‎scripts/check-changed.ts‎

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ const runPatchCheck = files.some(
232232
// Resolve without the lockfile in a temp copy to preserve the working tree and lockfile.
233233
// Copy mobile modules too: they are file: dependencies, not workspace packages.
234234
// Cached registry metadata limits this to patches stranded by local changes, not registry drift.
235-
const checkPatches = (): boolean => {
235+
const checkPatches = async () => {
236236
const temp = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-patch-check-"));
237237
try {
238238
const copy = (relative: string) => {
@@ -253,24 +253,15 @@ const checkPatches = (): boolean => {
253253
for (const extra of ["apps/mobile/deps", "patches", ".pnpmfile.cjs"]) {
254254
if (NodeFS.existsSync(NodePath.join(root, extra))) copy(extra);
255255
}
256-
const { status, stdout, stderr, error } = NodeChildProcess.spawnSync(
257-
"vp",
258-
["install", "--lockfile-only", "--ignore-scripts"],
259-
{ cwd: temp, encoding: "utf8", maxBuffer: 64 * 1024 * 1024, timeout: timeoutSeconds * 1000 },
260-
);
261-
if (status === 0) return true;
262-
if (stdout) console.log(stdout);
263-
if (stderr) console.error(stderr);
264-
if (error) console.error(error.message);
265-
if (`${stdout ?? ""}${stderr ?? ""}`.includes("ERR_PNPM_UNUSED_PATCH")) {
266-
console.error(
267-
"Unused dependency patches: remove each patch named above from " +
268-
"patchedDependencies in pnpm-workspace.yaml and from patches/, " +
269-
"update it to the version the install resolves, " +
270-
"or pin the dependency that pulls it in to the patched version.",
271-
);
272-
}
273-
return false;
256+
const result = await spawnStep(["install", "--lockfile-only", "--ignore-scripts"], temp);
257+
if (result.outcome !== "FAIL" || !result.output.includes("ERR_PNPM_UNUSED_PATCH"))
258+
return result;
259+
const hint =
260+
"Unused dependency patches: remove each patch named above from " +
261+
"patchedDependencies in pnpm-workspace.yaml and from patches/, " +
262+
"update it to the version the install resolves, " +
263+
"or pin the dependency that pulls it in to the patched version.\n";
264+
return { ...result, output: Buffer.concat([result.output, Buffer.from(hint)]) };
274265
} finally {
275266
NodeFS.rmSync(temp, { recursive: true, force: true });
276267
}
@@ -284,18 +275,18 @@ const graphql = ["apps/server", "packages/source-control-github"].filter((dir) =
284275
// oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone script has no Effect runtime.
285276
const platform = process.platform;
286277
// The typecheck step gets the same budget for its own package runs. macOS
287-
// counts its file cache as used, so freemem() there would starve a large Mac.
278+
// counts its file cache as used, so available memory there would starve a large
279+
// Mac. Elsewhere availableMemory() also honors a container's cgroup limit.
288280
const concurrency =
289281
positiveInteger(process.env.CHECK_CHANGED_CONCURRENCY) ??
290282
defaultConcurrency(
291283
NodeOS.availableParallelism(),
292-
platform === "darwin" ? NodeOS.totalmem() : NodeOS.freemem(),
284+
platform === "darwin" ? NodeOS.totalmem() : process.availableMemory(),
293285
);
294286
const timeoutSeconds = positiveInteger(process.env.CHECK_CHANGED_TIMEOUT) ?? 900;
295287

296288
const filters = (names: ReadonlyArray<string>) => names.flatMap((name) => ["--filter", name]);
297289
const steps = [
298-
// Runs first; it takes about a second.
299290
runPatchCheck && { name: "unused patches", run: checkPatches },
300291
present.length > 0 && {
301292
name: "format",
@@ -354,6 +345,15 @@ for (const signal of ["SIGINT", "SIGTERM"] as const) {
354345
process.exit(signal === "SIGINT" ? 130 : 143);
355346
});
356347
}
348+
const spawnStep = async (args: ReadonlyArray<string>, cwd = root) => {
349+
const child = NodeChildProcess.spawn("vp", args, { cwd, env, detached: !windows });
350+
running.add(child);
351+
try {
352+
return await awaitStep(child, timeoutSeconds * 1000, () => killTree(child));
353+
} finally {
354+
running.delete(child);
355+
}
356+
};
357357
const runStep = async (step: (typeof steps)[number]) => {
358358
const started = performance.now();
359359
const finish = (outcome: StepOutcome, output?: Buffer) => {
@@ -365,13 +365,7 @@ const runStep = async (step: (typeof steps)[number]) => {
365365
if (output) process.stdout.write(output);
366366
return { name: step.name, outcome, time };
367367
};
368-
// In-process steps print their own output. The patch check is first and
369-
// takes about a second, so the commands start right after it.
370-
if ("run" in step) return finish(step.run() ? "pass" : "FAIL");
371-
const child = NodeChildProcess.spawn("vp", step.args, { cwd: root, env, detached: !windows });
372-
running.add(child);
373-
const { outcome, output } = await awaitStep(child, timeoutSeconds * 1000, () => killTree(child));
374-
running.delete(child);
368+
const { outcome, output } = await ("run" in step ? step.run() : spawnStep(step.args));
375369
return finish(outcome, output);
376370
};
377371

‎scripts/lib/check-changed-steps.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ describe("check:changed step timeout", () => {
7777
[2, "FAIL"],
7878
] as const) {
7979
const child = fakeChild();
80-
const result = awaitStep(child, 1000, () => {});
80+
const kill = vi.fn();
81+
// Past Node's 32-bit timer limit, which would otherwise fire after 1 ms.
82+
const result = awaitStep(child, 2 ** 32, kill);
83+
vi.advanceTimersByTime(1);
84+
expect(kill).not.toHaveBeenCalled();
8185
child.stdout.emit("data", Buffer.from("out "));
8286
child.stderr.emit("data", Buffer.from("err"));
8387
child.emit("close", status);

‎scripts/lib/check-changed-steps.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,23 @@ interface Child {
3535
on(event: "close", listener: (status: number | null) => void): unknown;
3636
}
3737

38+
// Node fires a delay above its 32-bit maximum after 1 ms instead.
39+
const MAX_TIMER_MS = 2 ** 31 - 1;
40+
3841
// Collects a step's output and settles when it exits. Past timeoutMs it calls
3942
// kill, which must end the whole process tree, and reports TIMEOUT whatever the
4043
// exit status.
4144
export const awaitStep = (child: Child, timeoutMs: number, kill: () => void) =>
4245
new Promise<{ outcome: StepOutcome; output: Buffer }>((resolve) => {
4346
const output: Array<Buffer> = [];
4447
let timedOut = false;
45-
const timer = setTimeout(() => {
46-
timedOut = true;
47-
kill();
48-
}, timeoutMs);
48+
const timer = setTimeout(
49+
() => {
50+
timedOut = true;
51+
kill();
52+
},
53+
Math.min(timeoutMs, MAX_TIMER_MS),
54+
);
4955
child.stdout?.on("data", (chunk: Buffer) => output.push(chunk));
5056
child.stderr?.on("data", (chunk: Buffer) => output.push(chunk));
5157
// A failed spawn emits "error", then "close" with a nonzero code.

0 commit comments

Comments
 (0)