From 6576a4a506405b1c53b2c63b9aa2d75a3dcc584c Mon Sep 17 00:00:00 2001 From: Noah Date: Mon, 28 Sep 2026 20:30:57 -0600 Subject: [PATCH] qemu: a reboot tap may lose the control channel; expect_reboot looks back one step A tap whose outcome is a reboot races init tearing warden-ui down: the control channel can close while the tap's press/release counts are still polled, which made the tap FATAL, and a fast guest can print its restart line before expect_reboot starts, which expect_reboot then ignored. flare-edge's Reboot flow passed about half its rig runs (flare-edge#233). drive() now tells each step the next command and the console size when the previous step began. A tap followed by expect_reboot that loses the channel after its press went out is ok; expect_reboot reads the console from the start of the step before it. Anything earlier still does not count. Tests: RebootTapRace (3 cases) in test_qmp_drive.py; the old driver fails the main one. Rig: settings/system-reboot-restarts 4/4 sequential passes, each through the channel-close path (next: 2/4). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01441BzdGcvQmXkiN2qDjfZg --- qemu/tests/qmp.py | 44 +++++++++++++++++++++---- qemu/tests/test_qmp_drive.py | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/qemu/tests/qmp.py b/qemu/tests/qmp.py index 03ec292..7f4c51d 100755 --- a/qemu/tests/qmp.py +++ b/qemu/tests/qmp.py @@ -244,6 +244,7 @@ def do_observed_tap(ctx, ax, ay): return True send_events(ctx.s, ctx.f, [abs_ev("x", ax), abs_ev("y", ay), btn_ev(True)]) + ctx.tap_sent = True pressed = False try: pressed = wait_input_count(ctx, "presses", before.get("presses")) @@ -728,6 +729,10 @@ class Ctx: self.rs485_control = rs485_control self.outdir = outdir self.script_path = script_path + # Set by drive() before each step: the command after this one, and the + # console size when the step before this one began (flare-edge#233). + self.next_cmd = None + self.prev_step_console = 0 def need_ctl(self, lineno, cmd): if self.ctl is None: @@ -786,7 +791,19 @@ def verb_shot(ctx, lineno, cmd, args, line): def verb_tap(ctx, lineno, cmd, args, line): x, y = int(args[0]), int(args[1]) - observed = do_observed_tap(ctx, to_axis(x, ctx.size), to_axis(y, ctx.size)) + ctx.tap_sent = False + try: + observed = do_observed_tap(ctx, to_axis(x, ctx.size), to_axis(y, ctx.size)) + except (RuntimeError, OSError): + # A tap whose correct outcome is a reboot (the next step is + # expect_reboot) can lose the control channel while its press/release + # counts are still being polled: init tears warden-ui down first. Once + # the press went out that is the tap working, not failing; the reboot + # step judges the outcome (flare-edge#233). Anywhere else, or before + # anything was sent, it stays a fatal step. + if ctx.next_cmd == "expect_reboot" and ctx.tap_sent: + return "ok", "tap sent; the control channel closed as the guest went down" + raise return (("ok", "") if observed else ("fail", "input press or release was not consumed within 2 seconds")) @@ -829,10 +846,11 @@ def verb_expect_reboot(ctx, lineno, cmd, args, line): timeout = float(args[0]) if not ctx.console_path: sys.exit(f"FATAL: {ctx.script_path}:{lineno}: '{cmd}' needs --console") - try: - start = os.path.getsize(ctx.console_path) - except OSError: - start = 0 + # From the start of the step BEFORE this one, not this one: the action + # that reboots (a tap, usually) runs in that step, and a fast guest can + # print its restart line before expect_reboot even begins (flare-edge#233). + # A line from any earlier step still does not count. + start = ctx.prev_step_console deadline = time.monotonic() + timeout def console_since_start(): @@ -1182,12 +1200,24 @@ def drive(s, f, script_path, outdir, size, ctl_path=None, console_path=None, ref console_path) console = ConsoleWatch(console_path) + def console_size(): + try: + return os.path.getsize(console_path) if console_path else 0 + except OSError: + return 0 + + steps = [] for lineno, raw in enumerate(lines, 1): line = raw.split("#", 1)[0].strip() - if not line: - continue + if line: + steps.append((lineno, line)) + step_console = console_size() + + for i, (lineno, line) in enumerate(steps): parts = line.split() cmd, args = parts[0], parts[1:] + ctx.next_cmd = steps[i + 1][1].split()[0] if i + 1 < len(steps) else None + ctx.prev_step_console, step_console = step_console, console_size() handler = VERBS.get(cmd) if handler is None: diff --git a/qemu/tests/test_qmp_drive.py b/qemu/tests/test_qmp_drive.py index c5a9c8f..ae2fafa 100755 --- a/qemu/tests/test_qmp_drive.py +++ b/qemu/tests/test_qmp_drive.py @@ -924,6 +924,68 @@ class ExpectReboot(unittest.TestCase): self.assertIn("without the kernel's orderly restart line", rows[-1]["detail"]) +class RebootTapRace(unittest.TestCase): + """flare-edge#233: a tap whose outcome is a reboot races init tearing + warden-ui down. The control channel can close while the tap's press and + release counts are still being polled, and the guest can print its restart + line before expect_reboot starts. Both are the tap working.""" + + CONSOLE_LINE = b"init: reboot requested\n[ 17.8] reboot: Restarting system\n" + + def run_race(self, script, console_before=b""): + d = tempfile.mkdtemp(prefix="console.") + console = os.path.join(d, "console.log") + with open(console, "wb") as fh: + fh.write(console_before) + line = self.CONSOLE_LINE + + class RebootingCtl(FakeCtl): + """First stats answers (the tap's baseline); the next one, the + press poll, finds the guest already going down: the restart line + is on the console and the channel is closed.""" + def send(self, cmd): + if cmd == "stats" and self.stats_calls >= 1: + with open(console, "ab") as fh: + fh.write(line) + raise RuntimeError("control channel closed") + return super().send(cmd) + + def rpc(sock, sock_file, obj): + if obj.get("execute") == "query-status": + raise RuntimeError("QMP connection closed") + return fake_rpc(sock, sock_file, obj) + + return run_script(script, ctl_cls=RebootingCtl, rpc_fn=rpc, console_path=console) + + def test_reboot_tap_losing_the_channel_then_expect_reboot_passes(self): + rc, by, rows = self.run_race("tap 10 10\nexpect_reboot 2\n") + self.assertIsNone(rc, rows) + self.assertEqual([r["status"] for r in rows], ["ok", "ok"], rows) + self.assertIn("control channel closed", rows[0]["detail"]) + + def test_the_same_channel_loss_before_any_other_step_is_still_fatal(self): + rc, by, rows = self.run_race("tap 10 10\nsleep 0\n") + self.assertEqual(rows[0]["status"], "fatal", rows) + + def test_a_restart_line_from_two_steps_back_still_does_not_count(self): + # The restart line is already on the console before the script; the + # reboot step may look back one step, not to the start of the run. + d = tempfile.mkdtemp(prefix="console.") + console = os.path.join(d, "console.log") + with open(console, "wb") as fh: + fh.write(b"reboot: Restarting system\n") + + def rpc(sock, sock_file, obj): + if obj.get("execute") == "query-status": + raise RuntimeError("QMP connection closed") + return fake_rpc(sock, sock_file, obj) + + rc, by, rows = run_script("sleep 0\nexpect_reboot 1\n", rpc_fn=rpc, + console_path=console) + self.assertEqual(rc, 1) + self.assertIn("without the kernel's orderly restart line", rows[-1]["detail"]) + + class FullscreenToggleTracksRealState(unittest.TestCase): """qemu/tests/scripts/fullscreen-toggle-tracks-real-state.txt, offline: `home` clears the dashboard's real fullscreen state directly, and a -- 2.54.0