qemu: control bridge waits out a slow UI and never hands on a late reply #25
@@ -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
|
||||
|
||||
@@ -106,6 +106,7 @@ All take the virt-fragment `<zImage>`; `FLARE_EDGE=<checkout>` 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:
|
||||
|
||||
@@ -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
|
||||
# <<END>> 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 "<<END>>"
|
||||
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
|
||||
}
|
||||
|
||||
+3
-48
@@ -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 "<<END>>" >&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
|
||||
|
||||
|
||||
Executable
+109
@@ -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 <label> <command...>
|
||||
CHECKS=$((CHECKS + 1))
|
||||
if "${@:2}"; then
|
||||
echo "[PASS] $1"
|
||||
else
|
||||
echo "[FAIL] $1"
|
||||
FAIL=1
|
||||
fi
|
||||
}
|
||||
|
||||
# The fake UI: "slowN" answers after N tenths of a second, anything else at
|
||||
# once. Replies are "reply:<command>", written to a temp file and renamed.
|
||||
start_ui() {
|
||||
mkfifo "$WORK/ctl"
|
||||
(
|
||||
while :; do
|
||||
IFS= read -r cmd < "$WORK/ctl" || continue
|
||||
case "$cmd" in
|
||||
slow*) sleep "$(awk -v t="${cmd#slow}" 'BEGIN { print t / 10 }')" ;;
|
||||
esac
|
||||
printf 'reply:%s\n' "$cmd" > "$WORK/dbg.tmp"
|
||||
mv "$WORK/dbg.tmp" "$WORK/dbg"
|
||||
done
|
||||
) &
|
||||
UI_PID=$!
|
||||
}
|
||||
|
||||
run_bridge() { # run_bridge <name> <wait s or ""> <lines...>
|
||||
local name="$1" wait="$2"
|
||||
shift 2
|
||||
printf '%s\n' "$@" |
|
||||
env WARDEN_CTL_FIFO="$WORK/ctl" WARDEN_CTL_REPLY="$WORK/dbg" \
|
||||
${wait:+WARDEN_CTL_WAIT_S="$wait"} \
|
||||
"$SH" -c ". '$LIB'; warden_ctl_bridge" > "$WORK/$name.out" 2> "$WORK/$name.err"
|
||||
}
|
||||
|
||||
same() { # same <file> <expected lines...>
|
||||
local file="$1"
|
||||
shift
|
||||
diff <(printf '%s\n' "$@") "$file" > "$WORK/diff" || { cat "$WORK/diff"; return 1; }
|
||||
}
|
||||
|
||||
start_ui
|
||||
|
||||
# A 1 s bound, a 1.5 s reply: the explicit no-reply line, then the next
|
||||
# command gets ITS reply, not the late one (the desync behind #1448).
|
||||
run_bridge timeout 1 fast1 slow15 fast2
|
||||
check "timeout answers no-reply; the late reply is not the next answer" \
|
||||
same "$WORK/timeout.out" reply:fast1 '<<END>>' \
|
||||
'bridge: no reply from warden-ui within 1 s: slow15' '<<END>>' \
|
||||
reply:fast2 '<<END>>'
|
||||
|
||||
# The default bound: a 6 s reply (over the old 5 s) still arrives, and the
|
||||
# console note gives its time.
|
||||
run_bridge slow "" slow60 after
|
||||
check "default bound: a 6 s reply arrives" \
|
||||
same "$WORK/slow.out" reply:slow60 '<<END>>' reply:after '<<END>>'
|
||||
check "slow reply logged on stderr" grep -Eq "^ctl bridge: slow reply, [5-7] s of polling: slow60\$" "$WORK/slow.err"
|
||||
check "fast reply not logged" bash -c "! grep -q after '$WORK/slow.err'"
|
||||
|
||||
# @cat is answered by the bridge itself, with a newline after the file.
|
||||
printf '{"a":1}' > "$WORK/status.json"
|
||||
run_bridge cat 1 "@cat $WORK/status.json" "@cat $WORK/missing"
|
||||
check "@cat answers the file and a missing-file line" \
|
||||
same "$WORK/cat.out" '{"a":1}' '<<END>>' "bridge: no such file: $WORK/missing" '<<END>>'
|
||||
|
||||
# No FIFO: one line, no wait.
|
||||
stop_ui
|
||||
rm -f "$WORK/ctl"
|
||||
run_bridge nofifo 1 page
|
||||
check "missing FIFO answers at once" \
|
||||
same "$WORK/nofifo.out" 'bridge: warden-ui control FIFO not present' '<<END>>'
|
||||
|
||||
if [ "$FAIL" = 0 ]; then
|
||||
echo "ctl-bridge tests: OK ($CHECKS checks)"
|
||||
else
|
||||
echo "ctl-bridge tests: FAILED"
|
||||
exit 1
|
||||
fi
|
||||
+22
-5
@@ -172,6 +172,12 @@ INPUT_SLOW_NOTE_S = 2.0
|
||||
# wedged qemu-system-arm cannot outlive the script.
|
||||
QMP_TIMEOUT_S = 20.0
|
||||
|
||||
# Bounds each recv() on the control channel. It must outlast the bridge's
|
||||
# own reply wait (WARDEN_CTL_WAIT_S, 30 s of 0.05 s polls, which take longer
|
||||
# in wall time on a slow TCG guest), so a slow UI reaches the driver as the
|
||||
# bridge's explicit "no reply" line rather than a bare socket timeout.
|
||||
CTL_TIMEOUT_S = 90.0
|
||||
|
||||
# Where webstatus.c (ui-src/src/warden/webstatus.c) publishes its atomic
|
||||
# snapshot inside the guest. assert_json/wait_json `@cat` this path over the
|
||||
# control channel; see the bridge in rootfs/sbin/init.
|
||||
@@ -350,12 +356,12 @@ class Ctl:
|
||||
# rootfs/sbin/init's ctl bridge (the FIFO-to-socket relay this class
|
||||
# talks to). Nothing enforces the two staying in sync -- a sentinel
|
||||
# changed on one side and not the other means send() below blocks until
|
||||
# its own 15s timeout on every ctl-dependent step, with nothing at that
|
||||
# its own CTL_TIMEOUT_S on every ctl-dependent step, with nothing at that
|
||||
# point pointing back at this mismatch as the cause.
|
||||
SENTINEL = "<<END>>"
|
||||
|
||||
# A real reply (a hit's box, the status JSON) is at most a few KB. The
|
||||
# 15s socket timeout below bounds each individual recv(), not the total
|
||||
# socket timeout below bounds each individual recv(), not the total
|
||||
# bytes accepted, so a peer that keeps streaming data fast enough to beat
|
||||
# that per-call timeout, but never emits a newline or SENTINEL, would
|
||||
# otherwise grow self.buf without limit and exhaust host memory before
|
||||
@@ -363,7 +369,13 @@ class Ctl:
|
||||
# silent or closes outright already does.
|
||||
MAX_BUF = 256 * 1024
|
||||
|
||||
def __init__(self, path, timeout=15.0):
|
||||
# The bridge's own answer when warden-ui did not reply within its bound
|
||||
# (warden_ctl_bridge in rootfs/etc/warden-lib.sh, flare-edge #1448).
|
||||
NO_REPLY = "bridge: no reply from warden-ui"
|
||||
|
||||
def __init__(self, path, timeout=None):
|
||||
if timeout is None:
|
||||
timeout = CTL_TIMEOUT_S
|
||||
self.sock = socket.socket(socket.AF_UNIX)
|
||||
self.sock.settimeout(timeout)
|
||||
self.sock.connect(path)
|
||||
@@ -392,7 +404,12 @@ class Ctl:
|
||||
if line.strip() == cmd.strip():
|
||||
continue
|
||||
lines.append(line)
|
||||
return "\n".join(lines).strip()
|
||||
reply = "\n".join(lines).strip()
|
||||
# A UI that did not answer in time is a rig fault, not evidence: an
|
||||
# empty reply judged as "page is ''" hid it (flare-edge #1448).
|
||||
if reply.startswith(self.NO_REPLY):
|
||||
raise RuntimeError(f"control channel: {reply}")
|
||||
return reply
|
||||
|
||||
|
||||
HIT_RE = re.compile(r'^hit \d+,\d+: (\S+)(?: text="(.*)" box=(-?\d+),(-?\d+),(\d+)x(\d+))?$')
|
||||
@@ -425,7 +442,7 @@ def poll_until(check, timeout_s, period=0.5):
|
||||
they all mean the same thing by a timeout.
|
||||
|
||||
A CHECK that raises RuntimeError or OSError -- the control channel
|
||||
dying mid-poll, e.g. a socket.timeout past Ctl's own 15s per-call
|
||||
dying mid-poll, e.g. a socket.timeout past Ctl's own CTL_TIMEOUT_S per-call
|
||||
timeout -- stops polling right there instead of retrying a channel
|
||||
that is probably gone, and comes back with FATAL true so the caller
|
||||
records a `fatal` row (the same idea as ConsoleWatch.check() turning a
|
||||
|
||||
@@ -15,6 +15,7 @@ was never compared with the one the reference was captured under.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -1159,6 +1160,49 @@ class CtlSocketProtocol(unittest.TestCase):
|
||||
th.join(timeout=2)
|
||||
self.assertEqual(got, "the actual reply")
|
||||
|
||||
def test_bridge_no_reply_line_raises_as_a_rig_fault(self):
|
||||
# flare-edge #1448: a UI slower than the bridge's wait came back as
|
||||
# an empty reply and was judged "page is ''". The bridge now says
|
||||
# so, and that line must stop the flow as a rig fault.
|
||||
def server():
|
||||
self.server_sock.recv(4096)
|
||||
self.server_sock.sendall(
|
||||
b"bridge: no reply from warden-ui within 30 s: page\n<<END>>\n")
|
||||
|
||||
th = threading.Thread(target=server, daemon=True)
|
||||
th.start()
|
||||
try:
|
||||
with self.assertRaises(RuntimeError) as cm:
|
||||
self.ctl.send("page")
|
||||
finally:
|
||||
th.join(timeout=2)
|
||||
self.assertIn("no reply from warden-ui", str(cm.exception))
|
||||
|
||||
def test_bridge_no_such_file_stays_an_ordinary_reply(self):
|
||||
# Only the no-reply line is a fault; @cat's missing-file answer is
|
||||
# a detail the json channel handles itself.
|
||||
def server():
|
||||
self.server_sock.recv(4096)
|
||||
self.server_sock.sendall(b"bridge: no such file: /x\n<<END>>\n")
|
||||
|
||||
th = threading.Thread(target=server, daemon=True)
|
||||
th.start()
|
||||
try:
|
||||
got = self.ctl.send("@cat /x")
|
||||
finally:
|
||||
th.join(timeout=2)
|
||||
self.assertEqual(got, "bridge: no such file: /x")
|
||||
|
||||
def test_ctl_timeout_outlasts_the_bridge_wait(self):
|
||||
# The bridge polls up to 30 s (more wall time on a slow guest); a
|
||||
# shorter socket timeout would turn its explicit answer into a bare
|
||||
# socket.timeout. Read the bridge default from the rootfs itself.
|
||||
lib = os.path.join(HERE, "..", "rootfs", "etc", "warden-lib.sh")
|
||||
with open(lib) as f:
|
||||
m = re.search(r"WARDEN_CTL_WAIT_S:-(\d+)", f.read())
|
||||
self.assertIsNotNone(m, "warden_ctl_bridge default wait not found")
|
||||
self.assertGreaterEqual(qmp.CTL_TIMEOUT_S, 2 * int(m.group(1)))
|
||||
|
||||
def test_leftover_bytes_after_sentinel_carry_over_to_the_next_send(self):
|
||||
# One write carries this reply's SENTINEL immediately followed by
|
||||
# bytes belonging to the NEXT command's reply -- proving self.buf
|
||||
|
||||
Reference in New Issue
Block a user