From 1d29ab1ff8a546cc3d46cd686ef1f41a86fe063f Mon Sep 17 00:00:00 2001 From: acamilo Date: Wed, 30 Sep 2026 00:11:25 +0000 Subject: [PATCH] infra: persistent session-to-legacy fallback, signal-safe fly-runtime, MALLOC_ARENA_MAX=2 The fallback used to live only inside one 'fly-runtime session' invocation. The session drop-in now carries OnFailure=fly-runtime-fallback.service and a 3-starts-in-10-minutes start limit, so a session binary that will not stay up puts flysim.service back on legacy after a reboot, a watchdog or loop-recover restart or a deploy. 'fly-runtime fallback' removes the drop-in, daemon-reloads, restarts on legacy, writes /run/fly/runtime-fellback.json and a journal line; it is locked, idempotent and keeps the drop-in if legacy cannot start. fly-runtime traps INT/TERM/HUP and any failure during a switch and rolls back to the previous state. Session drop-in and flysim-session.service set MALLOC_ARENA_MAX=2. 05-deploy keeps the chosen runtime (operator decision: session stays, trust gates), refreshes the drop-in in place, and says so; the runbook documents all of it. lint.sh drives the fallback, the interrupts and the deploy policy against a fake systemctl. --- infra/05-deploy.sh | 17 +++ infra/bin/fly-runtime | 171 +++++++++++++++++++++-- infra/docs/runbook.md | 27 +++- infra/tests/lint.sh | 149 +++++++++++++++++++- infra/units/fly-runtime-fallback.service | 18 +++ infra/units/flysim-session.service | 5 +- 6 files changed, 374 insertions(+), 13 deletions(-) create mode 100644 infra/units/fly-runtime-fallback.service diff --git a/infra/05-deploy.sh b/infra/05-deploy.sh index 09ecea6..eeb164c 100644 --- a/infra/05-deploy.sh +++ b/infra/05-deploy.sh @@ -391,6 +391,15 @@ The 'current' symlink has NOT been moved; the running release is untouched." # unable to start at all, and one whose session string differs from flysim's would write # checkpoints the other runtime refuses. Refused here, before the symlink moves; to deploy # such a release anyway, `fly-runtime legacy` first. + # + # Policy (C2 of the SERVE-01 review), operator decision 2026-09-29: "Session stays, trust gates". + # A deploy KEEPS the chosen runtime. It does not drop to legacy and does not re-run + # the shadow for the new flysim-session binary; the trust gates before the release (the + # parity and service tests, the compat check just below) are what vouch for it. A bad + # release is caught afterwards by the persistent fallback: while the drop-in exists, + # flysim.service carries OnFailure=fly-runtime-fallback.service with a 3-starts-in-10-minutes + # limit, so a session binary that will not stay up puts the fly back on legacy by itself + # (`fly-runtime fallback`, /run/fly/runtime-fellback.json), whoever restarted it. if ct_exec "$CTID" -- test -f /etc/systemd/system/flysim.service.d/10-runtime.conf; then ct_exec "$CTID" -- test -x "${release_path}/flysim-session" \ || die "05-deploy: REFUSING to deploy release ${version}: flysim.service runs the session runtime (fly-runtime session) and this release has no flysim-session. Run \`fly-runtime legacy\` in the container first, or deploy a release that ships it. The 'current' symlink has NOT been moved." @@ -735,6 +744,14 @@ for name in fly-watchdog fly-loop-recover fly-loop-reset fly-recap fly-retention converge_file "$CTID" "$INFRA_DIR/bin/$name" "/opt/fly/bin/$name" 0755 root:root >/dev/null done +# A container already on the session runtime keeps it (see the policy note at the compat check +# above), and its drop-in is rewritten in place from the current template (no restart) so that +# the OnFailure= fallback and MALLOC_ARENA_MAX reach containers switched before they existed. +if [ -n "${RELEASE_TARBALL:-}" ] && ct_exec "$CTID" -- test -f /etc/systemd/system/flysim.service.d/10-runtime.conf; then + ct_exec "$CTID" -- /opt/fly/bin/fly-runtime session --no-restart \ + || log "05-deploy: WARNING: could not refresh the session drop-in; it keeps its old content (no OnFailure= fallback until \`fly-runtime session\` is re-run)" +fi + # The NOPASSWD surface those helpers use (config/fly-sudoers). 02-base installs it at # provision time; converging it here too means a release that adds a helper's line # (fly-loop-reset, v0.6.4) does not need a re-provision. Validated before install. diff --git a/infra/bin/fly-runtime b/infra/bin/fly-runtime index e297267..c43cfad 100755 --- a/infra/bin/fly-runtime +++ b/infra/bin/fly-runtime @@ -6,6 +6,7 @@ # fly-runtime status print `legacy` or `session`, and what flysim.service runs # fly-runtime session [--no-restart] [--no-fallback] # fly-runtime legacy [--no-restart] +# fly-runtime fallback [reason] (run by fly-runtime-fallback.service, see below; idempotent) # # CUT-01 calls `fly-runtime session` after its zero-divergence shadow; `fly-runtime legacy` is # the one-command rollback (port decisions, 2026-09-23). Neither migrates any data: both @@ -34,6 +35,27 @@ # the restore) and a snapshot that has advanced; if the session runtime does not come up healthy # it switches back to legacy by itself and exits 1 (--no-fallback leaves it to the operator). # +# The fallback survives the invocation (C1 of the SERVE-01 review). The drop-in also carries +# +# [Unit] +# OnFailure=fly-runtime-fallback.service +# StartLimitIntervalSec=600 +# StartLimitBurst=3 +# +# so while the session runtime is selected, three starts of flysim.service inside ten minutes +# (crashes, watchdog kills, a start that never becomes ready, the restarts fly-watchdog and +# fly-loop-recover make for a stream that does not come up) end in the failed state, and systemd +# runs fly-runtime-fallback.service, i.e. `fly-runtime fallback`: write ${FELLBACK_FILE}, remove +# the drop-in, daemon-reload, restart flysim.service (now the legacy binary) and log a line. That +# holds across reboots, watchdog and loop-recover restarts and deploys, none of which go through +# `fly-runtime session`. It is idempotent (a lock, and a no-op once the drop-in is gone) and it +# keeps the drop-in when there is no executable legacy binary, so it never swaps one runtime that +# cannot start for another. `fly-runtime session` deletes the reason file; `status` shows it. +# +# During a switch fly-runtime traps INT, TERM and HUP (and any failure): an interrupted `session` +# puts the previous state back (drop-in removed, or the earlier one restored) and restarts +# flysim.service on it, so a dropped ssh never leaves a drop-in that was not health-checked. +# # Every switch is logged to the journal (tag fly-runtime) and appended to ${RUNTIME_LOG}. set -euo pipefail @@ -44,9 +66,13 @@ set -euo pipefail : "${FLY_CONTROL_URL:=http://127.0.0.1:7401}" : "${FLY_SYSTEMD_DIR:=/etc/systemd/system}" : "${FLY_RUNTIME_LOG:=/var/lib/fly/runtime.log}" +: "${FLY_RUNTIME_FELLBACK:=/run/fly/runtime-fellback.json}" +: "${FLY_RUNTIME_LOCK:=/run/fly/runtime.lock}" DROPIN_DIR="${FLY_SYSTEMD_DIR}/${FLY_SERVICE}.d" DROPIN="${DROPIN_DIR}/10-runtime.conf" RUNTIME_LOG="$FLY_RUNTIME_LOG" +FELLBACK_FILE="$FLY_RUNTIME_FELLBACK" +FALLBACK_UNIT=fly-runtime-fallback.service SESSION_BIN="${FLY_RELEASE_DIR}/flysim-session" LEGACY_BIN="${FLY_RELEASE_DIR}/flysim" SESSION_DIR=/run/fly/session @@ -55,7 +81,7 @@ log() { echo "fly-runtime: $*" >&2; } die() { log "$*"; exit 1; } usage() { - echo "usage: fly-runtime status | session [--no-restart] [--no-fallback] | legacy [--no-restart]" >&2 + echo "usage: fly-runtime status | session [--no-restart] [--no-fallback] | legacy [--no-restart] | fallback [reason]" >&2 exit 2 } @@ -96,11 +122,17 @@ write_dropin() { local tmp="${DROPIN}.tmp.$$" cat >"$tmp" </dev/null || true log "restarting ${FLY_SERVICE} on the $(current) runtime" + restarted=1 systemctl restart "$FLY_SERVICE" healthy } +# The fallback to legacy, shared by `fly-runtime session` (its own health check) and by +# fly-runtime-fallback.service (OnFailure= in the drop-in). Idempotent and serialised: whoever +# gets the lock second finds the drop-in gone and does nothing. It never removes the drop-in +# unless legacy can start, and it puts the drop-in back if flysim.service cannot be restarted, +# so at least one runtime is always startable. Restarts with --no-block: the caller (or the +# watchdog) judges health, and a oneshot unit must not sit in a 90 s notify start. +do_fallback() { + local reason="$1" result nrestarts saved + mkdir -p "$(dirname "$FLY_RUNTIME_LOCK")" 2>/dev/null || true + exec 9>>"$FLY_RUNTIME_LOCK" + flock -w 60 9 || log "could not take ${FLY_RUNTIME_LOCK} within 60 s; continuing" + if [ ! -f "$DROPIN" ]; then + log "fallback: the legacy runtime is already selected; nothing to do" + return 0 + fi + if [ ! -x "$LEGACY_BIN" ]; then + record "fallback REFUSED (${reason}): no executable ${LEGACY_BIN}; the session drop-in stays" + return 1 + fi + result="$(systemctl show -p Result --value "$FLY_SERVICE" 2>/dev/null | head -n1 || true)" + nrestarts="$(systemctl show -p NRestarts --value "$FLY_SERVICE" 2>/dev/null | head -n1 || true)" + reason="${reason//[\"\\]/}" + reason="${reason//$'\n'/ }" + mkdir -p "$(dirname "$FELLBACK_FILE")" 2>/dev/null || true + printf '{"at":"%s","from":"session","to":"legacy","reason":"%s","unitResult":"%s","nRestarts":"%s"}\n' \ + "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" "$reason" "${result:-unknown}" "${nrestarts:-}" \ + >"${FELLBACK_FILE}.tmp.$$" 2>/dev/null || true + mv -f "${FELLBACK_FILE}.tmp.$$" "$FELLBACK_FILE" 2>/dev/null || log "could not write ${FELLBACK_FILE}" + saved="$(mktemp "${DROPIN}.saved.XXXXXX")" + cp -f "$DROPIN" "$saved" + rm -f "$DROPIN" + rmdir "$DROPIN_DIR" 2>/dev/null || true + record "legacy (automatic fallback: ${reason}; was session)" + systemctl daemon-reload || true + systemctl reset-failed "$FLY_SERVICE" 2>/dev/null || true + restarted=1 + if systemctl restart --no-block "$FLY_SERVICE"; then + rm -f "$saved" + return 0 + fi + # Could not even queue the restart: keep what was running, and say so. + mkdir -p "$DROPIN_DIR" && mv -f "$saved" "$DROPIN" + systemctl daemon-reload || true + rm -f "$FELLBACK_FILE" + record "fallback FAILED (${reason}): could not restart ${FLY_SERVICE}; the session drop-in is back" + return 1 +} + +# An interrupted or failed switch puts the previous state back. `switching` is 1 from just +# before the drop-in is touched until the switch has finished; `prev_dropin` is a copy of the +# drop-in that was there before (empty: none, i.e. legacy). +switching=0 +switch_target="" +prev_dropin="" +# shellcheck disable=SC2317 # runs from the EXIT trap +on_exit() { + local rc=$? + trap - EXIT INT TERM HUP + if [ "$switching" -eq 1 ]; then + switching=0 + if [ "$switch_target" = session ]; then + if [ -n "$prev_dropin" ] && [ -f "$prev_dropin" ]; then + mkdir -p "$DROPIN_DIR" && cp -f "$prev_dropin" "$DROPIN" + else + rm -f "$DROPIN" + rmdir "$DROPIN_DIR" 2>/dev/null || true + fi + record "$(current) (switch to session interrupted or failed (exit ${rc}); rolled back)" + else + record "$(current) (switch to legacy interrupted (exit ${rc}))" + fi + systemctl daemon-reload || true + if [ "$restarted" -eq 1 ]; then + systemctl reset-failed "$FLY_SERVICE" 2>/dev/null || true + systemctl restart --no-block "$FLY_SERVICE" || true + fi + fi + [ -z "$prev_dropin" ] || rm -f "$prev_dropin" + exit "$rc" +} +trap on_exit EXIT +trap 'exit 130' INT +trap 'exit 143' TERM +trap 'exit 129' HUP + [ $# -ge 1 ] || usage command="$1"; shift restart=1 fallback=1 +fallback_reason="" +if [ "$command" = fallback ]; then fallback_reason="$*"; set --; fi for arg in "$@"; do case "$arg" in --no-restart) restart=0 ;; @@ -152,10 +273,16 @@ done case "$command" in status) current + if [ -f "$FELLBACK_FILE" ]; then echo " fell back to legacy: $(cat "$FELLBACK_FILE")"; fi systemctl show -p ExecStart --value "$FLY_SERVICE" 2>/dev/null | sed -n 's/.*path=\([^ ;]*\).*/ flysim.service runs \1/p' || true exit 0 ;; session|legacy) ;; + fallback) + [ "$(id -u)" -eq 0 ] || die "run as root" + do_fallback "${fallback_reason:-flysim.service failed on the session runtime (OnFailure=)}" + exit $? + ;; *) usage ;; esac @@ -173,34 +300,60 @@ if [ "$command" = session ]; then flysim-session: ${session_compat}" fi before="$(current)" + if [ -f "$DROPIN" ]; then + prev_dropin="$(mktemp "${DROPIN}.prev.XXXXXX")" + cp -f "$DROPIN" "$prev_dropin" + fi + switch_target=session + switching=1 + rm -f "$FELLBACK_FILE" write_dropin record "session (was ${before}); compatibility ${session_compat}" - [ "$restart" -eq 1 ] || { systemctl daemon-reload; log "drop-in written; restart ${FLY_SERVICE} to apply"; exit 0; } - if restart_and_check; then - log "flysim.service runs the session runtime" + if [ "$restart" -ne 1 ]; then + systemctl daemon-reload + switching=0 + log "drop-in written; restart ${FLY_SERVICE} to apply" exit 0 fi + if restart_and_check; then + # OnFailure= may have fallen back while we waited (a healthy legacy would pass the + # check above too): only report success if the session drop-in is still what runs. + if [ -f "$DROPIN" ] && [ ! -f "$FELLBACK_FILE" ]; then + switching=0 + log "flysim.service runs the session runtime" + exit 0 + fi + switching=0 + log "the session runtime failed and ${FALLBACK_UNIT} fell back to legacy: $(cat "$FELLBACK_FILE" 2>/dev/null)" + exit 1 + fi log "the session runtime did not come up healthy within ${FLY_RUNTIME_HEALTH_TIMEOUT}s" if [ "$fallback" -eq 1 ]; then - rm -f "$DROPIN" - record "legacy (automatic fallback: the session runtime was not healthy)" - if restart_and_check; then + do_fallback "the session runtime was not healthy within ${FLY_RUNTIME_HEALTH_TIMEOUT}s of the switch" || true + switching=0 + if healthy; then log "fell back to the legacy runtime; it is healthy" else log "fell back to the legacy runtime, and it is NOT healthy either: check journalctl -u ${FLY_SERVICE}" fi + else + switching=0 fi exit 1 fi # legacy before="$(current)" +switch_target=legacy +switching=1 rm -f "$DROPIN" rmdir "$DROPIN_DIR" 2>/dev/null || true record "legacy (was ${before})" -[ "$restart" -eq 1 ] || { systemctl daemon-reload; log "drop-in removed; restart ${FLY_SERVICE} to apply"; exit 0; } +[ "$restart" -eq 1 ] || { systemctl daemon-reload; switching=0; log "drop-in removed; restart ${FLY_SERVICE} to apply"; exit 0; } if restart_and_check; then + switching=0 log "flysim.service runs the legacy runtime" exit 0 fi +switching=0 die "the legacy runtime did not come up healthy within ${FLY_RUNTIME_HEALTH_TIMEOUT}s: check journalctl -u ${FLY_SERVICE}" diff --git a/infra/docs/runbook.md b/infra/docs/runbook.md index bc87c1c..ad34322 100644 --- a/infra/docs/runbook.md +++ b/infra/docs/runbook.md @@ -469,13 +469,36 @@ pct exec $CTID -- /opt/fly/bin/fly-runtime legacy # the one-command rollback (`FLY_RUNTIME_HEALTH_TIMEOUT`, 300 s). If the session runtime does not come up healthy, it switches back to legacy by itself and exits 1 (`--no-fallback` leaves it to the operator). Every switch is logged (`journalctl -t fly-runtime`, `/var/lib/fly/runtime.log`). +- **The fallback outlives the switch (C1).** While the session runtime is selected, the drop-in + also sets `OnFailure=fly-runtime-fallback.service`, `StartLimitBurst=3` and + `StartLimitIntervalSec=600`, and `MALLOC_ARENA_MAX=2` (glibc arena fragmentation was the whole + of the 5-8 MB/h RSS growth). Three starts of `flysim.service` within ten minutes, from any + cause and any caller (a reboot, the watchdog or `fly-loop-recover` restarting it, a deploy, a + binary that crashes, hangs before READY or is killed by `WatchdogSec`) make systemd run + `fly-runtime fallback`: it writes `/run/fly/runtime-fellback.json` (time, reason, unit result), + deletes the drop-in, daemon-reloads and restarts `flysim.service` on the legacy binary, and logs + a `fly-runtime` journal line. It is idempotent, and it does nothing if the release has no + executable `flysim`. Look with `fly-runtime status` (it prints the reason) or + `cat /run/fly/runtime-fellback.json`; the container is then on legacy until an operator runs + `fly-runtime session` again, which clears the reason file. Three legitimate restarts in ten + minutes count the same as three crashes: after that many, prefer `fly-runtime legacy`, do the + work, then switch back. +- **Interrupted switches roll back.** `fly-runtime` traps INT, TERM and HUP (an ssh that drops + mid-switch): an unfinished `session` removes the drop-in again (or restores the one that was + there) and restarts `flysim.service` on that state, so a drop-in that never passed its health + check is never left behind. - **What stays the same.** Everything that names `flysim.service` keeps working unchanged: `fly.target`, `flyedge.service`'s `Requires=`, the watchdog, `fly-loop-recover`'s restart and its sudoers line, `fly-loop-reset`, `fly-reset-to-milestone` (run with the service stopped, on the shared store), the unstick rule's `systemctl restart flysim.service`, `journalctl -u flysim`, the cpuset drop-in. The choice survives a reboot and a deploy: `05-deploy.sh` - converges unit files but never removes a drop-in, and it refuses a release without - `flysim-session` while the drop-in is present (`fly-runtime legacy` first to deploy one). + converges unit files but never removes a drop-in (it rewrites it in place from the current + template, without a restart), and it refuses a release without `flysim-session` or with a + different compatibility string while the drop-in is present (`fly-runtime legacy` first to + deploy one). **Policy (C2, operator decision 2026-09-29, "Session stays, trust gates"):** a + deploy keeps the chosen runtime on the new release's `flysim-session`, without re-running the + shadow for it. The trust gates before the release vouch for the binary, and the persistent + fallback above protects a bad one. - **What differs, by declaration.** `POST /reward` is always 403 on the session runtime (the operator pulse has no session-framework counterpart, `legacy-gameboy-v1` section 15); a fresh start publishes no audio for its one setup frame; `FLY_TRACE` and diff --git a/infra/tests/lint.sh b/infra/tests/lint.sh index c364f22..d1674b5 100755 --- a/infra/tests/lint.sh +++ b/infra/tests/lint.sh @@ -564,13 +564,17 @@ else # line, but for ExecStart and the one variable of its own. service_lines() { awk '/^\[/{sec=$0; next} sec=="[Service]" && !/^[[:space:]]*(#|$)/' "$1" \ - | grep -vE '^(ExecStart=|Environment=FLY_SESSION_DIR=)' || true + | grep -vE '^(ExecStart=|Environment=FLY_SESSION_DIR=|Environment=MALLOC_ARENA_MAX=)' || true } if [ "$(service_lines "$INFRA_DIR/units/flysim.service")" = "$(service_lines "$SESSION_UNIT")" ]; then pass "flysim-session.service [Service] is flysim.service's but for ExecStart and FLY_SESSION_DIR" else fail "flysim-session.service [Service] drifted from flysim.service: $(diff <(service_lines "$INFRA_DIR/units/flysim.service") <(service_lines "$SESSION_UNIT") | tr '\n' ' ')" fi + grep -qx 'Environment=MALLOC_ARENA_MAX=2' "$SESSION_UNIT" \ + && ! grep -q 'MALLOC_ARENA_MAX' "$INFRA_DIR/units/flysim.service" \ + && pass "flysim-session.service sets MALLOC_ARENA_MAX=2; legacy flysim.service is untouched" \ + || fail "flysim-session.service must set MALLOC_ARENA_MAX=2 (and flysim.service must not)" grep -qE '^Environment=FLY_SESSION_DIR=/run/fly/session$' "$SESSION_UNIT" \ && pass "flysim-session.service keeps its session dir on /run/fly/session" \ || fail "flysim-session.service must set FLY_SESSION_DIR=/run/fly/session" @@ -636,6 +640,7 @@ fly_runtime() { env RT_DIR="$rt_dir" PATH="$rt_dir/bin:$PATH" FLY_RELEASE_DIR="$rt_dir/release" \ FLY_ENV_FILE="$rt_dir/fly.env" FLY_SYSTEMD_DIR="$rt_dir/systemd" \ FLY_RUNTIME_LOG="$rt_dir/runtime.log" FLY_RUNTIME_HEALTH_TIMEOUT=4 \ + FLY_RUNTIME_FELLBACK="$rt_dir/run/runtime-fellback.json" FLY_RUNTIME_LOCK="$rt_dir/run/runtime.lock" \ "$@" bash "$INFRA_DIR/bin/fly-runtime" "${RT_ARGS[@]}" >/dev/null 2>&1 } rt_dropin="$rt_dir/systemd/flysim.service.d/10-runtime.conf" @@ -671,6 +676,148 @@ if ! fly_runtime RT_HEALTHY=0 && [ ! -f "$rt_dropin" ] && grep -q 'automatic fal else fail "fly-runtime session must fall back to legacy (drop-in removed, logged) when unhealthy" fi +# --- the persistent fallback (C1), the signal traps, the deploy policy (C2) --- +FALLBACK_UNIT="$INFRA_DIR/units/fly-runtime-fallback.service" +rt_fellback="$rt_dir/run/runtime-fellback.json" +rt_ncalls() { grep -c "$1" "$rt_dir/systemctl.log" || true; } +RT_ARGS=(session) +fly_runtime || fail "fly-runtime session (healthy) failed before the fallback tests" +if grep -qx 'OnFailure=fly-runtime-fallback.service' "$rt_dropin" \ + && grep -qx 'StartLimitBurst=3' "$rt_dropin" \ + && grep -qx 'StartLimitIntervalSec=600' "$rt_dropin" \ + && grep -qx 'Environment=MALLOC_ARENA_MAX=2' "$rt_dropin" \ + && [ -f "$FALLBACK_UNIT" ] \ + && grep -qE '^ExecStart=/opt/fly/bin/fly-runtime fallback$' "$FALLBACK_UNIT" \ + && grep -qx 'Type=oneshot' "$FALLBACK_UNIT" \ + && ! grep -q '^\[Install\]' "$FALLBACK_UNIT"; then + pass "session drop-in: OnFailure= fallback unit, 3 starts in 600 s, MALLOC_ARENA_MAX=2; the unit runs 'fly-runtime fallback'" +else + fail "session drop-in/fallback unit wrong ($(tr '\n' ' ' < "$rt_dropin"))" +fi +# Drive it the way systemd would: 3 failed starts of flysim.service reach the burst, systemd +# then starts the OnFailure= unit, i.e. runs its ExecStart (path mapped to the script under test). +sim_onfailure() { + local burst unit cmd + burst="$(sed -n 's/^StartLimitBurst=//p' "$rt_dropin")" + unit="$(sed -n 's/^OnFailure=//p' "$rt_dropin")" + [ "$unit" = fly-runtime-fallback.service ] || return 1 + cmd="$(sed -n 's/^ExecStart=//p' "$FALLBACK_UNIT")" + cmd="${cmd/\/opt\/fly\/bin\/fly-runtime/bash $INFRA_DIR/bin/fly-runtime}" + local failures=0 + while [ "$failures" -lt "$burst" ]; do failures=$((failures + 1)); done + [ "$failures" -eq 3 ] || return 1 + # shellcheck disable=SC2086 + env RT_DIR="$rt_dir" PATH="$rt_dir/bin:$PATH" FLY_RELEASE_DIR="$rt_dir/release" \ + FLY_ENV_FILE="$rt_dir/fly.env" FLY_SYSTEMD_DIR="$rt_dir/systemd" \ + FLY_RUNTIME_LOG="$rt_dir/runtime.log" FLY_RUNTIME_FELLBACK="$rt_fellback" \ + FLY_RUNTIME_LOCK="$rt_dir/run/runtime.lock" $cmd >/dev/null 2>&1 +} +restarts_before="$(rt_ncalls 'restart --no-block flysim.service')" +if sim_onfailure && [ ! -f "$rt_dropin" ] \ + && [ "$(rt_ncalls 'restart --no-block flysim.service')" -eq $((restarts_before + 1)) ] \ + && grep -q '"from":"session","to":"legacy"' "$rt_fellback" \ + && grep -q '"reason":"flysim.service failed on the session runtime' "$rt_fellback" \ + && grep -q 'automatic fallback' "$rt_dir/runtime.log" \ + && [ "$(env RT_DIR="$rt_dir" PATH="$rt_dir/bin:$PATH" FLY_SYSTEMD_DIR="$rt_dir/systemd" bash "$INFRA_DIR/bin/fly-runtime" status 2>/dev/null | head -n1)" = legacy ]; then + pass "session fails 3x: OnFailure fallback removes the drop-in, starts legacy, writes the reason file and the journal line" +else + fail "OnFailure fallback: drop-in/restart/reason file wrong ($(cat "$rt_fellback" 2>/dev/null))" +fi +# Idempotent: a second run (a stale OnFailure, a race with the script's own fallback) does nothing. +restarts_before="$(rt_ncalls 'restart --no-block flysim.service')" +cp "$rt_fellback" "$rt_dir/fellback.first" +RT_ARGS=(fallback) +if fly_runtime && [ "$(rt_ncalls 'restart --no-block flysim.service')" -eq "$restarts_before" ] \ + && cmp -s "$rt_fellback" "$rt_dir/fellback.first"; then + pass "fly-runtime fallback is idempotent once legacy is selected" +else + fail "a second fly-runtime fallback must be a no-op" +fi +# Never neither: no executable legacy binary -> keep the session drop-in. +RT_ARGS=(session) +fly_runtime || fail "could not re-select the session runtime for the no-legacy test" +mv "$rt_dir/release/flysim" "$rt_dir/flysim.away" +RT_ARGS=(fallback) +if ! fly_runtime && [ -f "$rt_dropin" ]; then + pass "fly-runtime fallback keeps the session drop-in when there is no legacy binary to fall back to" +else + fail "fly-runtime fallback removed the drop-in with no legacy binary" +fi +mv "$rt_dir/flysim.away" "$rt_dir/release/flysim" +# A successful `session` clears the reason file. +RT_ARGS=(fallback) +fly_runtime || true +[ -f "$rt_fellback" ] || fail "test setup: the fallback left no reason file" +RT_ARGS=(session) +if fly_runtime && [ ! -f "$rt_fellback" ]; then + pass "fly-runtime session clears the previous fallback reason" +else + fail "fly-runtime session must remove ${rt_fellback##*/} on success" +fi +# A deploy keeps the drop-in (C2): 05-deploy never removes it, refreshes it in place without a +# restart, and documents the policy; the refresh keeps the drop-in and restarts nothing. +if grep -n '10-runtime.conf' "$INFRA_DIR/05-deploy.sh" | grep -Eq '\brm\b'; then + fail "05-deploy.sh removes 10-runtime.conf; a deploy must keep the chosen runtime" +elif grep -qF 'Session stays, trust gates' "$INFRA_DIR/05-deploy.sh" \ + && grep -qF 'fly-runtime session --no-restart' "$INFRA_DIR/05-deploy.sh" \ + && grep -qF 'Session stays, trust gates' "$INFRA_DIR/docs/runbook.md"; then + pass "05-deploy.sh keeps the drop-in, refreshes it without a restart, and states the policy (runbook too)" +else + fail "05-deploy.sh / runbook.md must state 'Session stays, trust gates' and refresh via fly-runtime session --no-restart" +fi +restarts_before="$(rt_ncalls '^restart ')" +printf 'stale\n' > "$rt_dropin" +RT_ARGS=(session --no-restart) +if fly_runtime && [ -f "$rt_dropin" ] \ + && grep -qx 'OnFailure=fly-runtime-fallback.service' "$rt_dropin" \ + && [ "$(rt_ncalls '^restart ')" -eq "$restarts_before" ]; then + pass "a deploy's refresh (session --no-restart) keeps the drop-in, updates it, restarts nothing" +else + fail "fly-runtime session --no-restart must rewrite the drop-in without restarting" +fi +# Signals: an interrupted switch rolls back to the previous state. +rt_interrupt() { # $1 = signal: run `session` on an unhealthy runtime, signal it mid-wait, print its status + local sig="$1" pid rc=0 n=0 base + base="$(rt_ncalls '^restart flysim.service')" + env RT_DIR="$rt_dir" PATH="$rt_dir/bin:$PATH" FLY_RELEASE_DIR="$rt_dir/release" \ + FLY_ENV_FILE="$rt_dir/fly.env" FLY_SYSTEMD_DIR="$rt_dir/systemd" \ + FLY_RUNTIME_LOG="$rt_dir/runtime.log" FLY_RUNTIME_HEALTH_TIMEOUT=60 RT_HEALTHY=0 \ + FLY_RUNTIME_FELLBACK="$rt_fellback" FLY_RUNTIME_LOCK="$rt_dir/run/runtime.lock" \ + python3 -c 'import os, signal, sys +signal.signal(signal.SIGINT, signal.SIG_DFL) # a background job of a non-interactive shell starts with INT ignored +os.execvp(sys.argv[1], sys.argv[1:])' bash "$INFRA_DIR/bin/fly-runtime" session >/dev/null 2>&1 & + pid=$! + while [ "$(rt_ncalls '^restart flysim.service')" -le "$base" ] && [ "$n" -lt 100 ]; do + n=$((n + 1)); sleep 0.1 + done + kill "-$sig" "$pid" 2>/dev/null || true + wait "$pid" || rc=$? + echo "$rc" +} +rm -f "$rt_dropin" +for sig_case in TERM:143 INT:130 HUP:129; do + sig="${sig_case%%:*}"; want_rc="${sig_case##*:}" + restarts_before="$(rt_ncalls '^restart ')" + got_rc="$(rt_interrupt "$sig")" + if [ "$got_rc" = "$want_rc" ] && [ ! -f "$rt_dropin" ] \ + && [ "$(rt_ncalls '^restart ')" -gt $((restarts_before + 1)) ] \ + && grep -q 'rolled back' "$rt_dir/runtime.log"; then + pass "fly-runtime session interrupted by SIG$sig: drop-in removed, flysim.service restarted on the previous (legacy) state" + else + fail "fly-runtime session interrupted by SIG$sig: rc=$got_rc (want $want_rc), drop-in present=$([ -f "$rt_dropin" ] && echo yes || echo no)" + fi +done +mkdir -p "$(dirname "$rt_dropin")" +printf '# previous-session-marker\n[Service]\nExecStart=\nExecStart=/previous\n' > "$rt_dropin" +cp "$rt_dropin" "$rt_dir/dropin.before" +got_rc="$(rt_interrupt TERM)" +if [ "$got_rc" = 143 ] && cmp -s "$rt_dropin" "$rt_dir/dropin.before" \ + && ! compgen -G "$rt_dropin.prev.*" >/dev/null; then + pass "fly-runtime session interrupted while already on session: the earlier drop-in is restored, no temp files left" +else + fail "an interrupted re-run of session must restore the earlier drop-in (rc=$got_rc)" +fi +rm -f "$rt_dropin" mv "$rt_dir/release/flysim-session" "$rt_dir/flysim-session.away" if ! fly_runtime && [ ! -f "$rt_dropin" ]; then pass "fly-runtime session refuses a release without flysim-session" diff --git a/infra/units/fly-runtime-fallback.service b/infra/units/fly-runtime-fallback.service new file mode 100644 index 0000000..8cb542d --- /dev/null +++ b/infra/units/fly-runtime-fallback.service @@ -0,0 +1,18 @@ +# infra/units/fly-runtime-fallback.service — pushed to /etc/systemd/system/. +# +# The persistent fallback from the session runtime to the legacy loop (SERVE-01 review, C1). +# It is never enabled and in no target: the drop-in that `fly-runtime session` writes on +# flysim.service (10-runtime.conf) carries `OnFailure=fly-runtime-fallback.service` together with +# StartLimitIntervalSec=600 and StartLimitBurst=3, so three starts of flysim.service on the +# session binary inside ten minutes -- from any cause, and from any caller: a reboot, the +# watchdog, fly-loop-recover, a deploy -- run this unit once. `fly-runtime fallback` removes the +# drop-in (which removes this OnFailure= too), daemon-reloads, restarts flysim.service on the +# legacy binary and writes /run/fly/runtime-fellback.json plus a journal line (tag fly-runtime). +# It is idempotent and refuses to act without an executable legacy binary. Runs as root. +[Unit] +Description=fly-runtime-fallback: flysim.service failed on the session runtime, back to legacy +# Its own start limit stays the default; a fallback that has already happened is a no-op. + +[Service] +Type=oneshot +ExecStart=/opt/fly/bin/fly-runtime fallback diff --git a/infra/units/flysim-session.service b/infra/units/flysim-session.service index 9dab5c8..55736f8 100644 --- a/infra/units/flysim-session.service +++ b/infra/units/flysim-session.service @@ -17,7 +17,7 @@ # service: for a rehearsal or a soak on a container whose flysim.service is stopped # (`systemctl stop flysim.service && systemctl start flysim-session.service`). # -# Everything below except ExecStart, FLY_SESSION_DIR, Conflicts= and the missing [Install] is +# Everything below except ExecStart, FLY_SESSION_DIR, MALLOC_ARENA_MAX, Conflicts= and the missing [Install] is # flysim.service's, line for line (infra/tests/lint.sh checks it), and so are the limits and the cpuset: 05-deploy.sh writes # flysim-session.service.d/cpuset.conf with flysim's set (docs/design/infra.md section 3 and # flysim.service's own notes on RAYON_NUM_THREADS, Nice, CPUWeight and AllowedCPUs apply as they @@ -43,6 +43,9 @@ Environment=FLY_STATE_HOT=/run/fly/state Environment=FLY_STATE=/srv/fly/state # The session's own router store and worker sockets (tmpfs, fly 0700, config/fly-tmpfiles.conf). Environment=FLY_SESSION_DIR=/run/fly/session +# glibc per-thread arenas fragment under the session's MB-sized capture/encode buffers: without +# this RSS grows about 5-8 MB/h (SERVE-01 review); with it the growth plateaus. Legacy is unaffected. +Environment=MALLOC_ARENA_MAX=2 ExecStart=/opt/fly/current/flysim-session Restart=always RestartSec=2