From ad6ee7f67c381bc142128d7a070f5101b2fbbf0c Mon Sep 17 00:00:00 2001 From: catborise Date: Tue, 10 Nov 2026 18:57:20 +0300 Subject: [PATCH] fix(console): a serial console waits for its VM's start instead of closing While a VM starts, libvirt shows it paused, reason "starting up", before its devices are set up. The worker took that for a running VM, asked for its console before the terminal existed ("PTY device is not yet assigned") and closed, although it had just said that the console opens again when the VM starts. A VM paused while starting up now counts as not running. --- conf/daemon/consolecallback | 7 ++++++- console/tests/test_consolecallback.py | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/conf/daemon/consolecallback b/conf/daemon/consolecallback index dfb66130..0f396fb0 100755 --- a/conf/daemon/consolecallback +++ b/conf/daemon/consolecallback @@ -47,7 +47,12 @@ class Console: def check(self): """Open the stream while the VM runs, drop it while it does not. The stream may have ended first: the VM's stop is still reported.""" - active = self.domain.state()[0] in (libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_PAUSED) + state, reason = self.domain.state() + # a VM being started is paused until its devices are set up: its + # console has no terminal yet + active = state == libvirt.VIR_DOMAIN_RUNNING or ( + state == libvirt.VIR_DOMAIN_PAUSED and reason != libvirt.VIR_DOMAIN_PAUSED_STARTING_UP + ) if active and self.stream is None: stream = self.connection.newStream(libvirt.VIR_STREAM_NONBLOCK) try: diff --git a/console/tests/test_consolecallback.py b/console/tests/test_consolecallback.py index 5e6b62ad..e6b7238e 100644 --- a/console/tests/test_consolecallback.py +++ b/console/tests/test_consolecallback.py @@ -66,7 +66,7 @@ class ConsoleWorkerTests(TestCase): console.domain = MagicMock() console.stream = None # from_vm dropped it at the stream's end console.active = True - console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_SHUTOFF] + console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_SHUTOFF, 0] with patch.object(self.cb, "say") as say: console.check() console.check() @@ -77,7 +77,7 @@ class ConsoleWorkerTests(TestCase): console.domain, console.connection = MagicMock(), MagicMock() console.stream = None console.active = True - console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_RUNNING] + console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_RUNNING, 0] console.domain.openConsole.side_effect = self.cb.libvirt.libvirtError("domain is not running") console.domain.isActive.return_value = 0 with patch.object(self.cb, "say") as say: @@ -85,6 +85,21 @@ class ConsoleWorkerTests(TestCase): say.assert_called_once_with("The VM is not running: its console opens again when it starts") self.assertIsNone(console.stream) + def test_a_vm_being_started_is_waited_for(self): + # its terminal is not assigned yet: opening its console then fails + console = self.cb.Console.__new__(self.cb.Console) + console.domain, console.connection = MagicMock(), MagicMock() + console.stream = None + console.active = False + console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_PAUSED, + self.cb.libvirt.VIR_DOMAIN_PAUSED_STARTING_UP] + console.check() + console.domain.openConsole.assert_not_called() + console.domain.state.return_value = [self.cb.libvirt.VIR_DOMAIN_PAUSED, self.cb.libvirt.VIR_DOMAIN_PAUSED_USER] + console.check() + console.domain.openConsole.assert_called_once() + self.assertTrue(console.active) + def test_a_closed_client_ends_the_worker(self): with patch.object(self.cb.os, "read", return_value=b""): self.cb.from_client(None, 0, None, self.console)