Verification done on focal-proposed, following comments 23, 24, 25, 26. Including in this comment a few key snippets from each test/comment. --- Environment --- LXD virtual machine lxc launch --vm ubuntu:focal lp2059272-focal lxc exec lp2059272-focal -- su - ubuntu Enable proposed & debug symbols cat < for SLEEP in $(seq 0.1 0.1 2.0); do <...> All VMs are still managed by libvirt: $ virsh list Id Name State ---------------------------- 1 test-vm-1 running 2 test-vm-2 running 3 test-vm-3 running 4 test-vm-4 running 5 test-vm-5 running 6 test-vm-6 running 7 test-vm-7 running 8 test-vm-8 running 9 test-vm-9 running 10 test-vm-10 running --- Steps with test packages on Focal (shutdown-on-init) --- Scenario 1) Shutdown wins race against XML update (ie, shutdown happens first) <...> Now, let the qemuProcessReconnect thread continue, it will not update the XML file, because 'quit' is set (ie, shutdown in progress) (gdb) t 20 (gdb) p ((virNetDaemonPtr)anyobj)->quit $2 = true $ ls -l /run/libvirt/qemu/test-vm.xml -rw------- 1 root root 10189 Apr 24 12:02 /run/libvirt/qemu/test-vm.xml (gdb) c & $ ls -l /run/libvirt/qemu/test-vm.xml -rw------- 1 root root 10189 Apr 24 12:02 /run/libvirt/qemu/test-vm.xml <...> $ sudo grep 'Leaving the update of .* domain status XML' /var/log/libvirt/libvirtd-debug.log 2024-04-24 12:08:40.054+0000: 3770: info : qemuProcessReconnect:8157 : Leaving the update of 'test-vm' domain status XML for the next initialization (shutdown detected on this initialization). <...> $ sudo grep -e ' Scenario 2) Shutdown loses race against XML update (ie, update happens first) <...> Instead, let the qemuProcessReconnect thread take the lock, and update the XML file, but not unlock yet <...> $ ls -l /run/libvirt/qemu/test-vm.xml -rw------- 1 root root 10189 Apr 24 12:02 /run/libvirt/qemu/test-vm.xml (gdb) b virObjectUnlock thread 20 if anyobj == $ptr (gdb) c $ ls -l /run/libvirt/qemu/test-vm.xml -rw------- 1 root root 10189 Apr 24 12:14 /run/libvirt/qemu/test-vm.xml <...> $ sudo grep -e ' Scenario 3) Shutdown happens along QEMU monitor calls (ie, calls don't finish) <...> The XML was not updated, as expected: $ ls -l /run/libvirt/qemu/test-vm.xml -rw------- 1 root root 10189 Apr 24 12:14 /run/libvirt/qemu/test-vm.xml $ sudo grep -e ' <...> Now, the next time libvirtd starts, it correctly parses that XML: $ sudo systemctl start libvirtd.service $ journalctl -b -u libvirtd.service | grep -A1 error $ And libvirt is aware of the domain, and can manage it: $ virsh list Id Name State ------------------------- 1 test-vm running $ virsh destroy test-vm Domain test-vm destroyed $ virsh undefine test-vm Domain test-vm has been undefined --- Steps with test packages on Focal (shutdown-on-runtime) --- <...> Check the formatter/options again; it is *STILL* referenced, not 0x0 anymore: (gdb) t 20 (gdb) p xmlopt.privateData.format $3 = (virDomainXMLPrivateDataFormatFunc) 0x7fd08c3437c0 (gdb) p/x xmlopt.parent $4 = {u = {dummy_align1 = 0x1cafe0026, dummy_align2 = 0x1cafe0026, s = {magic = 0xcafe0026, refs = 0x1}}, klass = 0x7fd080043170} Let the save function continue, and libvirt finishes shutting down: <...> Check the VM status XML *after*: $ ls -l /run/libvirt/qemu/test-vm.xml -rw------- 1 root root 10251 Apr 24 12:28 /run/libvirt/qemu/test-vm.xml $ sudo grep -e ' Now, the next time libvirtd starts, it correctly parses that XML: $ sudo systemctl start libvirtd.service $ journalctl -b -u libvirtd.service | grep -A1 error $ And libvirt is aware of the domain, and can manage it: $ virsh list Id Name State ------------------------- 1 test-vm running $ virsh destroy test-vm Domain test-vm destroyed $ virsh undefine test-vm Domain test-vm has been undefined