From b94fd156b4a5d736299933981d5db69b7eb37afb Mon Sep 17 00:00:00 2001 From: BFE Engineering Date: Wed, 21 Oct 2026 00:32:37 -0600 Subject: [PATCH] qemu: control bridge waits out a slow UI and never hands on a late reply The bridge waited 5 s for warden-ui's reply. On the lenovo QEMU host a page build outran it: the bridge answered an empty reply, and the UI's late reply then landed after the next command's remove and was read as that command's answer (flare-edge#1448: "page is ''", a nav answered with the previous hit). The loop moves to warden_ctl_bridge in etc/warden-lib.sh so a host test reaches it. The wait is 30 s (WARDEN_CTL_WAIT_S); a timeout answers "bridge: no reply from warden-ui ...", which qmp.py raises as a rig fault, and the next command first drains the late reply. Replies slower than 5 s are logged on the console with their time. qmp.py's control socket timeout is 90 s so it outlasts the bridge's wait. Tests: qemu/tests/ctl-bridge-test.sh (new, in CI), test_qmp_drive.py. Refs flare-edge#1448 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KerYPVh72hhNgKi3wS5ijG --- .github/workflows/ci.yml | 2 + qemu/README.md | 1 + qemu/rootfs/etc/warden-lib.sh | 79 ++++++++++++++++++++++++ qemu/rootfs/sbin/init | 51 +--------------- qemu/tests/ctl-bridge-test.sh | 109 ++++++++++++++++++++++++++++++++++ qemu/tests/qmp.py | 27 +++++++-- qemu/tests/test_qmp_drive.py | 44 ++++++++++++++ 7 files changed, 260 insertions(+), 53 deletions(-) create mode 100755 qemu/tests/ctl-bridge-test.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6bfeb16..9421ac4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -155,6 +155,8 @@ jobs: run: bash qemu/tests/init-power-signals-test.sh - name: rig init daemon order matches the panel (flare-edge#348) run: bash qemu/tests/init-daemon-order-test.sh + - name: rig control bridge waits out a slow UI (flare-edge#1448) + run: bash qemu/tests/ctl-bridge-test.sh - name: mkimage.sh SEED_DIR regression test # Only ui-drive.sh --seed (a real VM boot) exercises this hook # otherwise; this builds the same unprivileged mkfs.ext4 image and diff --git a/qemu/README.md b/qemu/README.md index 3b6e72a..71443fc 100644 --- a/qemu/README.md +++ b/qemu/README.md @@ -106,6 +106,7 @@ All take the virt-fragment ``; `FLARE_EDGE=` where noted. | `real-image-boot.sh` | matched `rootfs.img` + `oem.img` | an ACTUAL flare-edge build (placed by `mkimage.sh --rootfs-image/--oem-image`) boots its own init chain to getty; binaries predating known fixes reproduce their bugs faithfully, a time machine for field issues | | `test-ui-drive-rs485.sh` | - | offline, no VM: a fake `run.sh` and (for one case) a fake `socat` stand in so `ui-drive.sh --rs485-devices`'s startup fails closed instead of printing a false "== rs485 simulator: ..." over a bus nothing is serving, for each of a qemu rs.sock that never appears, a socat that never links rs.pty, and an mbsim.py that dies before opening rs.ctl; also proves `cleanup()` escalates to SIGKILL for a sim process that ignores SIGTERM | | `run-sh-args-test.sh` | - | offline, no VM: a fake `qemu-system-arm` captures run.sh's own argv and pins the ctl/rs485 pci-serial ordering contract rootfs/sbin/init's ttyS0-vs-ttyS1 alias depends on -- the ctl device always enumerates before rs485, warden.ctl only lands on the cmdline when --ctl is given, and an rs485 pci-serial device (real or null-backed) is always present so the port count never shifts | +| `ctl-bridge-test.sh` | - | offline, no VM: runs the control bridge (`warden_ctl_bridge` in rootfs/etc/warden-lib.sh) against a fake warden-ui that answers some commands late. A reply slower than the old 5 s bound still arrives (the bound is now 30 s, `WARDEN_CTL_WAIT_S`), a timeout answers `bridge: no reply from warden-ui ...` (qmp.py raises it as a rig fault), and a timed-out command's late reply is drained instead of being read as the next command's answer (flare-edge #1448) | | watchdog (`run.sh --watchdog`) | - | arm `/dev/watchdog`, don't pet: the VM resets ~30 s later (verified) | Scenario fine print: diff --git a/qemu/rootfs/etc/warden-lib.sh b/qemu/rootfs/etc/warden-lib.sh index cb66226..a5d2759 100644 --- a/qemu/rootfs/etc/warden-lib.sh +++ b/qemu/rootfs/etc/warden-lib.sh @@ -62,3 +62,82 @@ warden_idle_forever() { wait $! done } + +# The control bridge's command loop (/sbin/init runs it on the ctl serial +# port): one command per line on stdin, the reply then the sentinel line +# <> on stdout. qmp.py's Ctl matches the sentinel byte for byte. +# +# `@cat PATH` is answered here and never forwarded: PATH's contents (or one +# "bridge: no such file: PATH" line), which is how the json flow channel +# reads webstatus.c's snapshot from outside the VM. Everything else goes to +# warden-ui's FIFO, and the bridge waits for the reply file the UI renames +# into place whole. The previous reply is removed BEFORE sending: a fixed +# settle handed back the previous command's reply (flare-edge #152). +# +# The wait is bounded by WARDEN_CTL_WAIT_S (default 30 s of polling). It was +# 5 s, and a slow QEMU host (lenovo-x3650, two TCG guests) let a page build +# outrun it: the bridge answered an empty reply, and the UI's late reply +# then landed after the NEXT command's remove and was read as that +# command's answer (flare-edge #1448). Now a timeout answers one +# "bridge: no reply from warden-ui ..." line (qmp.py treats it as a rig +# fault), and the next command first waits, on the same bound, for the late +# reply and discards it. A reply slower than 5 s is logged on stderr (the +# console) with its time, for the host-speed baselines. +# +# WARDEN_CTL_FIFO and WARDEN_CTL_REPLY exist for the host test only. +warden_ctl_bridge() { + _wcb_fifo="${WARDEN_CTL_FIFO:-/tmp/warden-ui.ctl}" + _wcb_reply="${WARDEN_CTL_REPLY:-/tmp/warden-ui.dbg}" + _wcb_limit=$(( ${WARDEN_CTL_WAIT_S:-30} * 20 )) + _wcb_stale=0 + while IFS= read -r cmd; do + [ -n "$cmd" ] || continue + case "$cmd" in + "@cat "*) + path="${cmd#@cat }" + if [ -f "$path" ]; then + cat "$path" + # The status json has no trailing newline; without this + # the sentinel would share its line and the line-based + # reader would never see it. + echo + else + echo "bridge: no such file: $path" + fi + ;; + *) + if [ -p "$_wcb_fifo" ]; then + if [ "$_wcb_stale" = 1 ]; then + _wcb_wait_reply + _wcb_stale=0 + fi + rm -f "$_wcb_reply" + printf '%s\n' "$cmd" > "$_wcb_fifo" + if _wcb_wait_reply; then + cat "$_wcb_reply" 2>/dev/null + else + echo "bridge: no reply from warden-ui within $((_wcb_limit / 20)) s: $cmd" + _wcb_stale=1 + fi + if [ "$_wcb_n" -gt 100 ]; then + echo "ctl bridge: slow reply, $((_wcb_n / 20)) s of polling: $cmd" >&2 + fi + else + echo "bridge: warden-ui control FIFO not present" + fi + ;; + esac + echo "<>" + done +} + +# Polls for a non-empty reply file every 0.05 s, up to the bridge's bound; +# leaves the poll count in _wcb_n. +_wcb_wait_reply() { + _wcb_n=0 + while [ ! -s "$_wcb_reply" ]; do + [ "$_wcb_n" -ge "$_wcb_limit" ] && return 1 + sleep 0.05 + _wcb_n=$((_wcb_n + 1)) + done +} diff --git a/qemu/rootfs/sbin/init b/qemu/rootfs/sbin/init index 98f05db..6030248 100755 --- a/qemu/rootfs/sbin/init +++ b/qemu/rootfs/sbin/init @@ -192,54 +192,9 @@ if grep -qw warden.ctl /proc/cmdline && [ -c /dev/ttyS0 ]; then # mode: the host discards echoed input, and a whole line arrives per # read. exec 3<> "$ctl" - while IFS= read -r cmd <&3; do - [ -n "$cmd" ] || continue - case "$cmd" in - "@cat "*) - # A bridge-local command, never forwarded to warden-ui's - # FIFO: `@cat PATH` reads PATH directly off the GUEST's - # own filesystem and answers with it, which is how the - # json flow channel gets webstatus.c's snapshot out to - # the host driving the VM from outside. `-f` so a - # directory or device node reports as missing rather than - # cat hanging or erroring oddly. - path="${cmd#@cat }" - if [ -f "$path" ]; then - cat "$path" >&3 - # Force a newline after the file's own bytes: the - # status json (webstatus.c) is written with NO - # trailing newline, and without this the sentinel - # below would land on the SAME line as the content - # and the reader (qmp.py Ctl.send, line-based) would - # block forever waiting for a line that never comes. - echo >&3 - else - echo "bridge: no such file: $path" >&3 - fi - ;; - *) - if [ -p /tmp/warden-ui.ctl ]; then - # Remove the previous reply BEFORE sending, then wait - # for the new one to appear (the UI renames it into - # place whole). A fixed settle used to hand back the - # previous command's reply whenever a page took longer - # than 0.3 s to build (flare-edge #152); this is the - # same recipe tools/warden-ctl uses over SSH. - rm -f /tmp/warden-ui.dbg - printf '%s\n' "$cmd" > /tmp/warden-ui.ctl - n=0 - while [ ! -s /tmp/warden-ui.dbg ] && [ "$n" -lt 100 ]; do - sleep 0.05 - n=$((n + 1)) - done - cat /tmp/warden-ui.dbg 2>/dev/null >&3 - else - echo "bridge: warden-ui control FIFO not present" >&3 - fi - ;; - esac - echo "<>" >&3 - done + # The loop itself is warden_ctl_bridge (/etc/warden-lib.sh), where + # the host test reaches it. + warden_ctl_bridge <&3 >&3 ) & fi diff --git a/qemu/tests/ctl-bridge-test.sh b/qemu/tests/ctl-bridge-test.sh new file mode 100755 index 0000000..8664665 --- /dev/null +++ b/qemu/tests/ctl-bridge-test.sh @@ -0,0 +1,109 @@ +#!/usr/bin/env bash +# Host test for the rig's control bridge, warden_ctl_bridge in +# qemu/rootfs/etc/warden-lib.sh (flare-edge #1448). A fake warden-ui reads +# the FIFO and renames each reply into place, as warden_debug.c does; some +# commands answer late. Checks: a reply slower than the old 5 s bound still +# arrives, a timeout answers the explicit no-reply line, and the late reply +# of a timed-out command is never handed to the next command. +set -uo pipefail +# Job control: the fake UI leads its own process group, so one kill stops +# its pending sleep too. +set -m + +HERE="$(cd "$(dirname "$0")" && pwd)" +LIB="$HERE/../rootfs/etc/warden-lib.sh" +SH="${WARDEN_TEST_SH:-sh}" +WORK="$(mktemp -d)" || exit 1 +UI_PID= +stop_ui() { + [ -n "$UI_PID" ] || return 0 + { kill -KILL -- "-$UI_PID"; wait "$UI_PID"; } 2>/dev/null + UI_PID= +} +cleanup() { + stop_ui + rm -rf "$WORK" +} +trap cleanup EXIT +FAIL=0 +CHECKS=0 + +check() { # check