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}')"