From e8ad79623f69de3fe1de487947153bebdb4c4e68 Mon Sep 17 00:00:00 2001 From: Timothy Date: Sat, 25 Jul 2026 12:24:57 +0200 Subject: [PATCH] fix(445): make the boot wait interruptible so a mid-boot signal can't orphan the server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Codex round-2 verdict was BLOCKED @ 2a5f26c0 on one High finding, which was correct and is the subtlest defect in this whole change. ## The defect Bash DEFERS a trapped signal while it waits on a FOREGROUND command. The boot was OUT="$(ETV_UI_PORT=... scripts/e2e-local.sh "$CONFIG_DIR")" so a TERM arriving during the (up to 120s) readiness wait did not run `on_signal` until boot completed — and a supervisor escalating TERM->KILL means the trap never runs at all, orphaning the server on its port. The pidfile added in the previous commit only helps IF the trap runs; this is the case where it doesn't. Proved the mechanism in isolation rather than asserting it, since the real boot is only ~2s locally and kept winning the race: foreground command substitution : TERM at t=1s -> trap ran at t=6s (deferred) background job + `wait` : TERM at t=1s -> trap ran at t=1s (prompt) ## The fix Boot in the background and `wait` on it. `wait` is interruptible, so the handler runs immediately; by then e2e-local.sh has already written $ETV_PIDFILE, so cleanup can reap a server whose PID this script has not yet parsed. Cleanup also now stops the backgrounded e2e-local.sh itself, so it cannot sit waiting on a server we just killed, and removes its temp output file. Boot diagnostics are preserved on the failure path (verified with a deliberately bad ETV_BUILD_CONFIG: the underlying "bin/ does not exist" error is surfaced, along with the exit status, which is now propagated rather than flattened to 1). ## Verification - mechanism: deferred-vs-prompt trap proven as above - passing run 3/3 green; failing run (--grep ZZZ_NOPE) exits 1 - boot-failure path prints e2e-local.sh's real error and its exit status - targeted SIGTERM: exit 143, no listener, no stray process - curl harness unaffected: 45/45 PASS - no orphaned processes after the full gate (an earlier count of 1 was a graceful shutdown still in flight, not a leak — re-checked clean) Refs #445 --- scripts/e2e-ui.sh | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/scripts/e2e-ui.sh b/scripts/e2e-ui.sh index f1556a367..1beb60474 100755 --- a/scripts/e2e-ui.sh +++ b/scripts/e2e-ui.sh @@ -120,6 +120,14 @@ reap() { } cleanup() { + # If we were signalled mid-boot, e2e-local.sh itself is still running (backgrounded, see below). + # Stop it first so it can't keep waiting on a server we are about to kill. + if [ -n "${BOOT_PID:-}" ]; then + kill "$BOOT_PID" 2>/dev/null || true + BOOT_PID="" + fi + rm -f "${BOOT_OUT:-}" 2>/dev/null || true + local pid="$SERVER_PID" if [ -z "$pid" ] && [ -s "$PIDFILE" ]; then pid="$(tr -dc '0-9' <"$PIDFILE" 2>/dev/null || true)" @@ -142,12 +150,30 @@ trap 'on_signal INT' INT trap 'on_signal TERM' TERM echo "Booting instance for UI-E2E (config: $CONFIG_DIR, port: $PORT)..." -# `if ! OUT=$(...)` rather than a bare assignment: under `set -e` a failing assignment aborts the -# script before the diagnostics below can be printed, losing the log path. -if ! OUT="$(ETV_UI_PORT="$PORT" "$REPO_ROOT/scripts/e2e-local.sh" "$CONFIG_DIR")"; then - echo "error: e2e-local.sh failed to boot the instance. Its output:" >&2 + +# Boot in the BACKGROUND and `wait` on it, rather than the obvious +# `OUT="$(... e2e-local.sh ...)"`. Bash defers a trapped signal until the current FOREGROUND command +# finishes, so with a command substitution a TERM arriving during the (up to 120s) readiness wait +# would not run `on_signal` until boot completed — and a supervisor that escalates TERM->KILL would +# mean the trap never runs at all, orphaning the server on its port. `wait` is interruptible: the +# handler runs immediately, and by then e2e-local.sh has already written $ETV_PIDFILE, so cleanup +# can reap a server whose PID this script has not yet parsed. +BOOT_OUT="$(mktemp)" +ETV_UI_PORT="$PORT" "$REPO_ROOT/scripts/e2e-local.sh" "$CONFIG_DIR" >"$BOOT_OUT" 2>&1 & +BOOT_PID=$! + +set +e +wait "$BOOT_PID" +boot_status=$? +set -e + +OUT="$(cat "$BOOT_OUT")" +rm -f "$BOOT_OUT" + +if [ "$boot_status" -ne 0 ]; then + echo "error: e2e-local.sh failed to boot the instance (exit $boot_status). Its output:" >&2 printf '%s\n' "$OUT" >&2 - exit 1 + exit "$boot_status" fi printf '%s\n' "$OUT" SERVER_PID="$(printf '%s\n' "$OUT" | awk -F= '/^PID=/{print $2}')"