fix(scripts): clamp package docker timers

This commit is contained in:
Vincent Koc
2026-06-22 02:47:41 +02:00
parent 75c6a8fff5
commit 851b65c060
2 changed files with 78 additions and 5 deletions

View File

@@ -17,6 +17,7 @@ const DEFAULT_TIMEOUT_KILL_AFTER_MS = 5_000;
const PROCESS_GROUP_EXIT_POLL_MS = 25;
const POST_FORCE_KILL_WAIT_MS = 1_000;
const DEFAULT_CAPTURED_STDOUT_MAX_BYTES = 1024 * 1024;
const MAX_TIMER_TIMEOUT_MS = 2_147_000_000;
const ACTIVE_CHILD_KILLERS = new Set();
const SIGNAL_EXIT_CODES = {
SIGHUP: 129,
@@ -65,6 +66,23 @@ function resolveTimeoutMs(envName, defaultValue) {
return parsed;
}
function numericTimerValueMs(valueMs) {
const value = Number(valueMs);
return Number.isFinite(value) ? Math.floor(value) : undefined;
}
function resolveTimerTimeoutMs(valueMs, fallbackMs = MAX_TIMER_TIMEOUT_MS) {
const value = numericTimerValueMs(valueMs) ?? numericTimerValueMs(fallbackMs);
return Math.min(Math.max(value ?? MAX_TIMER_TIMEOUT_MS, 1), MAX_TIMER_TIMEOUT_MS);
}
function resolveOptionalTimerTimeoutMs(valueMs) {
if (valueMs === undefined) {
return undefined;
}
return resolveTimerTimeoutMs(valueMs, 1);
}
function readOptionValue(argv, index, optionName) {
const value = argv[index + 1];
if (value === undefined || value === "" || value.startsWith("-")) {
@@ -149,6 +167,11 @@ export function parseArgs(argv) {
function run(command, args, cwd, options = {}) {
return new Promise((resolve, reject) => {
const resolvedTimeoutMs = resolveOptionalTimerTimeoutMs(options.timeoutMs);
const resolvedKillAfterMs = resolveTimerTimeoutMs(
options.killAfterMs,
DEFAULT_TIMEOUT_KILL_AFTER_MS,
);
const useProcessGroup = process.platform !== "win32";
const child = spawn(command, args, {
cwd,
@@ -230,21 +253,21 @@ function run(command, args, cwd, options = {}) {
return;
}
killChild("SIGKILL");
}, options.killAfterMs ?? DEFAULT_TIMEOUT_KILL_AFTER_MS);
}, resolvedKillAfterMs);
forceKillTimeout.unref?.();
};
ACTIVE_CHILD_KILLERS.add(killChild);
const timeout =
options.timeoutMs === undefined
resolvedTimeoutMs === undefined
? undefined
: setTimeout(() => {
timedOut = true;
terminateChild();
}, options.timeoutMs);
}, resolvedTimeoutMs);
timeout?.unref?.();
const finishAfterTeardown = async (error, value = "") => {
if (processGroupAlive()) {
await waitForProcessGroupExit(options.killAfterMs ?? DEFAULT_TIMEOUT_KILL_AFTER_MS);
await waitForProcessGroupExit(resolvedKillAfterMs);
}
if (processGroupAlive()) {
killChild("SIGKILL");
@@ -275,7 +298,7 @@ function run(command, args, cwd, options = {}) {
child.on("close", (status, signal) => {
if (timedOut) {
void finishAfterTeardown(
new Error(`${command} ${args.join(" ")} timed out after ${options.timeoutMs}ms`),
new Error(`${command} ${args.join(" ")} timed out after ${resolvedTimeoutMs}ms`),
);
return;
}