diff --git a/infra/bin/fly-feed b/infra/bin/fly-feed index c78b5ce..6938e19 100755 --- a/infra/bin/fly-feed +++ b/infra/bin/fly-feed @@ -129,6 +129,15 @@ page_on_feed() { page_unit_active() { systemctl is-active --quiet "$FLY_PAGE_SERVICE" 2>/dev/null; } old_invocation="" +# Print to stdout, exiting quietly if the pipe is closed (e.g. `fly-feed status | head -1`). +# bash's builtins report "write error: Broken pipe" on a failed write even with `trap '' PIPE`, +# so we check the write result ourselves and exit 0 on EPIPE: the reader is gone, nothing +# useful is left to do, and `set -e` must not trip on the failing printf. +say() { + if ! printf '%s\n' "$*"; then + exit 0 + fi +} # Wait for a NEW flysim invocation to be healthy and the feed to be flowing in `via` mode. healthy() { local via="$1" deadline @@ -195,24 +204,24 @@ status_report() { local conf run edge_state url conf="$(configured)" run="$(running)" - echo "configured: ${conf}" - echo "running: ${run}" + say "configured: ${conf}" + say "running: ${run}" if [ "$run" != stopped ] && [ "$run" != unknown ] && [ "$conf" != "$run" ] && [[ "$conf" != invalid:* ]]; then - echo " fly.env and the running flysim disagree: the next start of ${FLY_SERVICE} applies ${conf} (fly-feed ${conf} does it now)" + say " fly.env and the running flysim disagree: the next start of ${FLY_SERVICE} applies ${conf} (fly-feed ${conf} does it now)" fi edge_state="$(systemctl is-active "$FLY_EDGE_SERVICE" 2>/dev/null || true)" - echo "${FLY_EDGE_SERVICE}: ${edge_state:-unknown}" + say "${FLY_EDGE_SERVICE}: ${edge_state:-unknown}" url="$(metrics_url "$run")" if [ "$run" = bus ]; then if curl -fsS -o /dev/null --max-time 5 "${FLY_EDGE_METRICS_URL}/healthz" 2>/dev/null; then - echo "edge healthz: ok" + say "edge healthz: ok" else - echo "edge healthz: NOT OK ($(curl -sS --max-time 5 "${FLY_EDGE_METRICS_URL}/healthz" 2>&1 | head -c 100))" + say "edge healthz: NOT OK ($(curl -sS --max-time 5 "${FLY_EDGE_METRICS_URL}/healthz" 2>&1 | head -c 100))" fi - echo "edge snapshot age: $(metric "$url" fly_edge_snapshot_age_seconds) s; bus lost: $(metric "$url" fly_edge_bus_lost_total); bind failures: $(metric "$url" fly_edge_bind_failures_total); decode failures: $(metric "$url" fly_edge_decode_failures_total)" + say "edge snapshot age: $(metric "$url" fly_edge_snapshot_age_seconds) s; bus lost: $(metric "$url" fly_edge_bus_lost_total); bind failures: $(metric "$url" fly_edge_bind_failures_total); decode failures: $(metric "$url" fly_edge_decode_failures_total)" fi - echo "feed clients: $(metric "$url" fly_feed_clients) (counters of the ${run} server); frames sent: $(metric "$url" fly_frames_sent_total)" - if [ -f "$FEED_LOG" ]; then echo "last switch: $(tail -n1 "$FEED_LOG")"; fi + say "feed clients: $(metric "$url" fly_feed_clients) (counters of the ${run} server); frames sent: $(metric "$url" fly_frames_sent_total)" + if [ -f "$FEED_LOG" ]; then say "last switch: $(tail -n1 "$FEED_LOG")"; fi } [ $# -ge 1 ] || usage diff --git a/infra/tests/lint.sh b/infra/tests/lint.sh index 10aa844..5f9d6f0 100755 --- a/infra/tests/lint.sh +++ b/infra/tests/lint.sh @@ -1720,6 +1720,26 @@ else fail "fly-feed status wrong: $ff_out" fi +# `fly-feed status | head -1` (a human reading a single line) closes stdout after the first +# line. Subsequent writes would fail with EPIPE; bash's builtins report "write error: +# Broken pipe" to stderr even with `trap '' PIPE`, so fly-feed routes every status_report +# write through a helper that exits 0 on the first EPIPE. The test runs the real fly-feed +# under the same stubbed state, captures fly-feed's stderr to a file, and asserts that +# nothing is written there. The test's stdout is not the script's stdout (it goes to a +# pipe closed by head -1), so suppress the SC2260 "this redirection overrides the +# output pipe" warning: the override is the point of the test. +ff_reset direct direct +FF_ARGS=(status) +: > /tmp/ff-status-pipe.err +# shellcheck disable=SC2260 +fly_feed 2>/tmp/ff-status-pipe.err >/dev/null | head -1 || true +if [ ! -s /tmp/ff-status-pipe.err ]; then + pass "fly-feed status | head -1: nothing written to stderr on a closed stdout" +else + fail "fly-feed status | head -1 wrote to stderr: $(cat /tmp/ff-status-pipe.err)" +fi +rm -f /tmp/ff-status-pipe.err + ff_reset direct direct FF_ARGS=(bus) if fly_feed && [ "$(grep -c '^FLY_FEED_VIA=' "$ff_env")" = 1 ] && grep -qx 'FLY_FEED_VIA=bus' "$ff_env" \