From af5492e773b43919ca30ee72c6374cdc4f22d6db Mon Sep 17 00:00:00 2001 From: Pavel Mores Date: Tue, 18 Jun 2024 12:05:36 +0200 Subject: [PATCH] runtime-rs: made Qemu::stop_vm() idempotent Since Hypervisor::stop_vm() is called from the WaitProcess request handling which appears to be per-container, it can be called multiple times during kata pod shutdown. Currently the function errors out on any subsequent call after the initial one since there's no VM to stop anymore. This commit makes the function tolerate that condition. While it seems conceivable that sandbox shouldn't be stopped by WaitProcess handling, and the right fix would then have to happen elsewhere, this commit at least makes qemu driver's behaviour consistent with other hypervisor drivers in runtime-rs. We also slightly improve the error message in case there's no QemuInner::qemu_process instance. Signed-off-by: Pavel Mores --- .../crates/hypervisor/src/qemu/inner.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs b/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs index fb2179a6f1..d87e22b366 100644 --- a/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs +++ b/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs @@ -167,10 +167,19 @@ impl QemuInner { pub(crate) async fn stop_vm(&mut self) -> Result<()> { info!(sl!(), "Stopping QEMU VM"); if let Some(ref mut qemu_process) = &mut self.qemu_process { - info!(sl!(), "QemuInner::stop_vm(): kill()'ing qemu"); - qemu_process.kill().await.map_err(anyhow::Error::from) + let is_qemu_running = qemu_process.id().is_some(); + if is_qemu_running { + info!(sl!(), "QemuInner::stop_vm(): kill()'ing qemu"); + qemu_process.kill().await.map_err(anyhow::Error::from) + } else { + info!( + sl!(), + "QemuInner::stop_vm(): qemu process isn't running (likely stopped already)" + ); + Ok(()) + } } else { - Err(anyhow!("qemu process not running")) + Err(anyhow!("qemu process has not been started yet")) } }