I'm using tokio::process::Command::spawn
to start a child process and then spawning a task to child.wait().await
for it to complete asynchronously. When using the code below, when the process ended by exiting or by signal, the wait wouldn't return. I have worked around this by using child.try_wait()
in a loop with tokio::task::yield_now().await
when there is no ExitStatus
. I would really appreciate it if someone could help me figure this out because it really hurts battery life to be polling a future in a loop like I am now.
tracing::debug!("Starting an emulator proc");
let event_sender = event_sender.clone();
proc_id = proc.id();
// Monitor the status of the emulator process
tokio::spawn(async move {
tracing::debug!("Started emu watch task.");
let status = proc.wait().await;
let status = status.unwrap();
PLAYING.store(false, Ordering::Relaxed);
if let Some(code) = status.code() {
// It was only a crash if there is an exit code
// Otherwise it was killed by a signal which is intended
tracing::debug!("Emulator proc crashed.");
event_sender
.send(SystemMessage::Error(format!(
"Emulator crashed with status: {}",
code
)))
.await
.unwrap();
}
});