1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00

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.
This commit is contained in:
catborise 2026-11-13 08:14:06 +03:00
parent 3412c63417
commit 272a40f4e1
2 changed files with 41 additions and 1 deletions

View file

@ -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()

View file

@ -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))