Don't print output on the binder thread
So far, the vm tool has been printing the output from the VM on the
binder thread executing the callback. This prevented it from receiving
other callback events from the virtualization service before the console
output is closed.
Fixing this by spawning a new thread and outputting there.
Bug: N/A
Test: run microdroid using the vm tool, do a force crash using `echo c >
/proc/sysrq-trigger`. The `vm` tool exits in a finite amount of time.
(previously it kept saying "error reading from virtual machine:
Connection reset by peer (os error 104)")
Change-Id: I043fd051198f1a923f98b868cd7d3480c21677ee
diff --git a/vm/src/run.rs b/vm/src/run.rs
index 05a9390..058405e 100644
--- a/vm/src/run.rs
+++ b/vm/src/run.rs
@@ -253,15 +253,15 @@
fn on_payload_started(&self, _cid: i32, stream: Option<&File>) {
// Show the output of the payload
if let Some(stream) = stream {
- let mut reader = BufReader::new(stream);
- loop {
+ let mut reader = BufReader::new(stream.try_clone().unwrap());
+ std::thread::spawn(move || loop {
let mut s = String::new();
match reader.read_line(&mut s) {
Ok(0) => break,
Ok(_) => print!("{}", s),
Err(e) => eprintln!("error reading from virtual machine: {}", e),
};
- }
+ });
}
}