Skip to content

Commit 074c59e

Browse files
authored
Check QEMU process status before periodically checking socket files. (#114)
Sometimes, the QEMU process exits early due to various reasons, e.g., the missing of kernel binary, providing incorrect qemu commandline arguments, ... and vmtest didn't check the exit status of QEMU and it will wait for 5 seconds to detect the error. This patch teaches vmtest exit earlier when the QEMU process exits unexpectedly.
1 parent 9b6098a commit 074c59e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/qemu.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,11 +758,26 @@ impl Qemu {
758758
}
759759

760760
/// Waits for QMP and QGA sockets to appear
761-
fn wait_for_qemu(&self) -> Result<()> {
761+
fn wait_for_qemu(&self, child: &mut Child) -> Result<()> {
762762
let now = time::Instant::now();
763763
let timeout = Duration::from_secs(5);
764764

765765
while now.elapsed() < timeout {
766+
// Before checking socket files, let's check if the child process fails to start.
767+
match child
768+
.try_wait()
769+
.with_context(|| "Failed to inspect QEMU status".to_string())?
770+
{
771+
None => { /* QEMU is still running. */ }
772+
Some(ec) => {
773+
if ec.success() {
774+
bail!("QEMU exits normally which is not expected")
775+
} else {
776+
bail!("QEMU fails to start: {}", Self::extract_child_stderr(child))
777+
}
778+
}
779+
}
780+
766781
let qga_ok = self
767782
.qga_sock
768783
.try_exists()
@@ -1035,7 +1050,7 @@ impl Qemu {
10351050
// Ensure child is cleaned up even if we bail early
10361051
let mut child = scopeguard::guard(child, Self::child_cleanup);
10371052

1038-
if let Err(e) = self.wait_for_qemu() {
1053+
if let Err(e) = self.wait_for_qemu(&mut child) {
10391054
return Err(e).context("Failed waiting for QEMU to be ready");
10401055
}
10411056

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ fn test_qemu_error_shown() {
344344
vmtest.run_one(0, send);
345345

346346
let err = assert_get_err!(recv, Output::BootEnd);
347-
let msg = err.to_string();
347+
let msg = format!("{:?}", err);
348348
assert!(msg.contains("qemu: could not open kernel file"));
349349
}
350350

0 commit comments

Comments
 (0)