I want to run a program and kill it when it times out, while capturing its stdout and stderr. Here's the code I wrote using Tokio:
use tokio::process::Command;
use tokio::time;
let mut child = Command::new("...").spawn().unwrap();
let output = match time::timeout(timeout, child.wait() /* takes &mut self */).await {
Ok(_) => child
.wait_with_output() // takes self
.await
.unwrap(),
Err(_) => {
error!("...");
child
.kill()
.await
.unwrap();
return Err("..");
}
};
But I found the stdout/stderr is always empty. It seems like the pipes were closed before data was collected. Could tokio consider to provide something to solve this issue, or what's the right way to handle these?