Skip to content

Commit e9281c6

Browse files
mtjhrcslp
authored andcommitted
devices/console: Fix a bug which would cause libkrun to hang on exit
Make PortInputEmpty and PortInputSigInt `wait_until_readable` implementations poll the stopfd EventFd to ensure the thread eventually exits. (The thread is joined in Port::shutdown method causing the hang). To replicate the hang, the stdin must not be a terminal. For example you can use a pipe - `echo hello | ./chroot_vm rootfs_fedora /bin/cat` would hang without this fix. This seems to be a regresion introduced by the commit: 4076b7: devices/console: implement reset method Signed-off-by: Matej Hrica <[email protected]>
1 parent 57a5a6b commit e9281c6

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/devices/src/virtio/console/port_io.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,13 @@ impl PortInput for PortInputSigInt {
226226
Ok(1)
227227
}
228228

229-
fn wait_until_readable(&self, _stopfd: Option<&EventFd>) {
230-
let mut poll_fds = [PollFd::new(self.sigint_evt.as_raw_fd(), PollFlags::POLLIN)];
229+
fn wait_until_readable(&self, stopfd: Option<&EventFd>) {
230+
let mut poll_fds = Vec::with_capacity(2);
231+
poll_fds.push(PollFd::new(self.sigint_evt.as_raw_fd(), PollFlags::POLLIN));
232+
if let Some(stopfd) = stopfd {
233+
poll_fds.push(PollFd::new(stopfd.as_raw_fd(), PollFlags::POLLIN));
234+
}
235+
231236
poll(&mut poll_fds, -1).expect("Failed to poll");
232237
}
233238
}
@@ -251,7 +256,12 @@ impl PortInput for PortInputEmpty {
251256
Ok(0)
252257
}
253258

254-
fn wait_until_readable(&self, _stopfd: Option<&EventFd>) {
255-
std::thread::sleep(std::time::Duration::MAX);
259+
fn wait_until_readable(&self, stopfd: Option<&EventFd>) {
260+
if let Some(stopfd) = stopfd {
261+
let mut poll_fds = [PollFd::new(stopfd.as_raw_fd(), PollFlags::POLLIN)];
262+
poll(&mut poll_fds, -1).expect("Failed to poll");
263+
} else {
264+
std::thread::sleep(std::time::Duration::MAX);
265+
}
256266
}
257267
}

0 commit comments

Comments
 (0)