From 272a40f4e14c13120897a503079b671f9c5f9dbd Mon Sep 17 00:00:00 2001 From: catborise Date: Fri, 13 Nov 2026 08:14:06 +0300 Subject: [PATCH] fix(instances): power off does not wait a minute for a guest agent that hangs libvirt shuts a VM down through its guest agent when one is connected, and waits 60 s for an agent that hangs before it presses the ACPI power button. The Power Off request hung for that minute, holding the VM's lock. A running VM's connected agent is now pinged first; one that does not answer in 10 s is passed over and the power button is pressed. --- vrtManager/instance.py | 14 +++++++++++++- vrtManager/tests/test_agent_access.py | 28 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/vrtManager/instance.py b/vrtManager/instance.py index 51e078a4..360ed02e 100644 --- a/vrtManager/instance.py +++ b/vrtManager/instance.py @@ -19,6 +19,7 @@ from libvirt import ( VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP, VIR_DOMAIN_PAUSED, VIR_DOMAIN_RUNNING, + VIR_DOMAIN_SHUTDOWN_ACPI_POWER_BTN, VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_START_PAUSED, VIR_DOMAIN_XML_SECURE, @@ -694,7 +695,18 @@ class wvmInstance(wvmConnect): check_hostdevs(self, dom.name(), [etree.fromstring(xml)], restore) def shutdown(self): - self.instance.shutdown() + """libvirt asks a connected guest agent first and waits 60 s for one + that hangs before it presses the ACPI power button: an agent that + does not answer a ping in AGENT_TIMEOUT is passed over. One that + answers with an error (guest-ping disabled) may still shut it down.""" + flags = 0 + if self.instance.state()[0] == VIR_DOMAIN_RUNNING and self.is_agent_ready(): + try: + self.ping_agent() + except libvirtError as err: + if err.get_error_code() != VIR_ERR_AGENT_COMMAND_FAILED: + flags = VIR_DOMAIN_SHUTDOWN_ACPI_POWER_BTN + self.instance.shutdownFlags(flags) def force_shutdown(self): self.instance.destroy() diff --git a/vrtManager/tests/test_agent_access.py b/vrtManager/tests/test_agent_access.py index 5bb58b18..a0427833 100644 --- a/vrtManager/tests/test_agent_access.py +++ b/vrtManager/tests/test_agent_access.py @@ -81,3 +81,31 @@ class AgentAccessTestCase(unittest.TestCase): self.vm.set_user_password("root", "$6$salt$hash") self.assertEqual(self.agent.call_count, 1) + +class ShutdownTestCase(unittest.TestCase): + """Power off: libvirt waits 60 s for a hung agent before ACPI, so one that + does not answer a ping is passed over.""" + + def shutdown(self, state=1, ready=True, ping=None): + vm = wvmInstance.__new__(wvmInstance) + vm.instance = MagicMock() + vm.instance.state.return_value = [state, 1] + with patch.object(wvmInstance, "is_agent_ready", return_value=ready), \ + patch.object(module, "qemuAgentCommand", side_effect=ping, return_value='{"return":{}}') as agent: + vm.shutdown() + return vm.instance.shutdownFlags.call_args.args[0], agent.call_count + + def test_an_answering_agent_is_left_to_libvirt(self): + self.assertEqual(self.shutdown(), (0, 1)) + + def test_a_hung_agent_is_passed_over_for_acpi(self): + hung = coded(module.VIR_ERR_AGENT_COMMAND_TIMEOUT, "guest agent didn't respond to command within '10' seconds") + self.assertEqual(self.shutdown(ping=hung), (module.VIR_DOMAIN_SHUTDOWN_ACPI_POWER_BTN, 1)) + + def test_an_agent_with_ping_disabled_is_left_to_libvirt(self): + disabled = coded(module.VIR_ERR_AGENT_COMMAND_FAILED, "Command guest-ping has been disabled") + self.assertEqual(self.shutdown(ping=disabled), (0, 1)) + + def test_without_a_connected_agent_or_when_paused_there_is_no_ping(self): + self.assertEqual(self.shutdown(ready=False), (0, 0)) + self.assertEqual(self.shutdown(state=3), (0, 0))