fly-feed: status exits quietly on a closed stdout (no broken-pipe error on stderr)

When stdout is closed early (e.g. `fly-feed status | head -1`), the second and later
status_report writes fail with EPIPE; bash's builtins report "write error: Broken pipe"
to stderr even with `trap '' PIPE`, because the failed write is reported by the
runtime, not the signal handler. Route every status_report write through a `say`
helper that checks printf's exit status and exits 0 on the first EPIPE: the reader
is gone, nothing useful is left to do, and `set -e` must not trip on the failing
write.

The change is scoped to status_report: bus/direct's stderr log/record writes are
unaffected, the output text is unchanged, and the switching behaviour and exit
codes are untouched. A test in infra/tests/lint.sh runs `fly-feed status` with
the existing stubbed state into `| head -1` and asserts nothing is written to
stderr.
This commit is contained in:
acamilo 2026-10-02 13:04:20 +00:00
parent 02afc659a4
commit 75e231c707
2 changed files with 38 additions and 9 deletions

View file

@ -129,6 +129,15 @@ page_on_feed() {
page_unit_active() { systemctl is-active --quiet "$FLY_PAGE_SERVICE" 2>/dev/null; } page_unit_active() { systemctl is-active --quiet "$FLY_PAGE_SERVICE" 2>/dev/null; }
old_invocation="" 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. # Wait for a NEW flysim invocation to be healthy and the feed to be flowing in `via` mode.
healthy() { healthy() {
local via="$1" deadline local via="$1" deadline
@ -195,24 +204,24 @@ status_report() {
local conf run edge_state url local conf run edge_state url
conf="$(configured)" conf="$(configured)"
run="$(running)" run="$(running)"
echo "configured: ${conf}" say "configured: ${conf}"
echo "running: ${run}" say "running: ${run}"
if [ "$run" != stopped ] && [ "$run" != unknown ] && [ "$conf" != "$run" ] && [[ "$conf" != invalid:* ]]; then 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 fi
edge_state="$(systemctl is-active "$FLY_EDGE_SERVICE" 2>/dev/null || true)" 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")" url="$(metrics_url "$run")"
if [ "$run" = bus ]; then if [ "$run" = bus ]; then
if curl -fsS -o /dev/null --max-time 5 "${FLY_EDGE_METRICS_URL}/healthz" 2>/dev/null; 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 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 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 fi
echo "feed clients: $(metric "$url" fly_feed_clients) (counters of the ${run} server); frames sent: $(metric "$url" fly_frames_sent_total)" 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 echo "last switch: $(tail -n1 "$FEED_LOG")"; fi if [ -f "$FEED_LOG" ]; then say "last switch: $(tail -n1 "$FEED_LOG")"; fi
} }
[ $# -ge 1 ] || usage [ $# -ge 1 ] || usage

View file

@ -1720,6 +1720,26 @@ else
fail "fly-feed status wrong: $ff_out" fail "fly-feed status wrong: $ff_out"
fi 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_reset direct direct
FF_ARGS=(bus) FF_ARGS=(bus)
if fly_feed && [ "$(grep -c '^FLY_FEED_VIA=' "$ff_env")" = 1 ] && grep -qx 'FLY_FEED_VIA=bus' "$ff_env" \ if fly_feed && [ "$(grep -c '^FLY_FEED_VIA=' "$ff_env")" = 1 ] && grep -qx 'FLY_FEED_VIA=bus' "$ff_env" \