From dab0c5d9b05fd3293baa764efabca0965c5ed4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Wed, 11 Mar 2026 13:58:34 -0700 Subject: [PATCH] devices: Make sure to drain virtio TX port on stop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I've encountered a race-condition where guest writes data to stderr shortly before exiting and that data never manifests on the host. The reason seems to be in libkrun's pop_head_blocking() (in the virtio console TX thread), when the stop flag is set the thread returns None without checking the queue one final time. Data already sitting in the virtio transmit queue is abandoned. Reorder the thread::park() call to make sure we check the queue one last time before stopping, which will ensure flushing of any remaining data on the port. I've verified the fix with several thousand iterations of a test case that earlier reproduced the problem in typically 10-50. Signed-off-by: Daniel Müller --- src/devices/src/virtio/console/process_tx.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/devices/src/virtio/console/process_tx.rs b/src/devices/src/virtio/console/process_tx.rs index c2c996554..e34340dcc 100644 --- a/src/devices/src/virtio/console/process_tx.rs +++ b/src/devices/src/virtio/console/process_tx.rs @@ -61,10 +61,10 @@ fn pop_head_blocking<'mem>( Some(descriptor) => break Some(descriptor), None => { interrupt.signal_used_queue(); - thread::park(); if stop.load(Ordering::Acquire) { break None; } + thread::park(); log::trace!("tx unparked, queue len {}", queue.len(mem)) } }