diff --git a/scripts/ci-hermit-retry.sh b/scripts/ci-hermit-retry.sh index b3d9795e68..5ac7668d45 100755 --- a/scripts/ci-hermit-retry.sh +++ b/scripts/ci-hermit-retry.sh @@ -55,25 +55,33 @@ for attempt in $(seq 1 "$attempts"); do set -m "$@" & pid=$! + # The watchdog exits 0 if stopped before it signals the attempt and 124 once + # it has, so a timeout is reported only when the watchdog really stopped the + # attempt. Elapsed time cannot tell: $SECONDS counts whole wall-clock + # seconds, so an instant failure that straddled a second boundary looked + # like it had run for the whole 1s deadline. ( + trap 'exit 0' TERM sleep "$deadline" + trap 'exit 124' TERM kill -TERM -- "-$pid" 2>/dev/null || exit 0 sleep "$kill_after" kill -KILL -- "-$pid" 2>/dev/null || true + exit 124 ) >/dev/null 2>&1 & watchdog=$! set +m - started=$SECONDS rc=0 # Bash reports a job killed by a signal on wait's stderr; the warning below # says the same thing. wait "$pid" 2>/dev/null || rc=$? kill -- "-$watchdog" 2>/dev/null || true - wait "$watchdog" 2>/dev/null || true + watchdog_rc=0 + wait "$watchdog" 2>/dev/null || watchdog_rc=$? [ "$rc" -eq 0 ] && exit 0 # The leader can exit on TERM while a child that ignores it lives on. kill -KILL -- "-$pid" 2>/dev/null || true - if [ $((SECONDS - started)) -ge "$deadline" ]; then + if [ "$watchdog_rc" -eq 124 ]; then echo "::warning::attempt $attempt/$attempts timed out after ${deadline}s: $*" else echo "::warning::attempt $attempt/$attempts failed (exit $rc): $*" diff --git a/scripts/ci-hermit-retry.test.mjs b/scripts/ci-hermit-retry.test.mjs index 14fff2fa4f..7eb131fc1f 100644 --- a/scripts/ci-hermit-retry.test.mjs +++ b/scripts/ci-hermit-retry.test.mjs @@ -98,6 +98,18 @@ test("persistent failure backs off between attempts and stops at the budget", () assert.ok(r.seconds >= 3, `took ${r.seconds}s`); }); +// A quick failure that straddles a wall-clock second boundary is still a +// failure: only the watchdog stopping the attempt makes it a timeout. Start +// 0.8s into a second so the 0.4s attempt crosses the next boundary while +// finishing well inside its 1s deadline. +test("a quick failure across a second boundary is not a timeout", async () => { + await sleep((1800 - (Date.now() % 1000)) % 1000); + const r = run("sleep 0.4; exit 7", { CI_HERMIT_ATTEMPTS: "1" }); + assert.equal(r.status, 1, r.stdout); + assert.match(r.stdout, /attempt 1\/1 failed \(exit 7\)/); + assert.doesNotMatch(r.stdout, /timed out/); +}); + test("no command is a usage error", () => { assert.equal(spawnSync("bash", [script]).status, 2); });