Tokio Child::wait never returning

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();
    }
});
1 Like

what is your child process and how it communicates with parent process? the only difference between wait and try_wait, is that wait will drop the stdin of the child process while try_wait won't. otherwise, they should have the same behavior (beside async vs periodically poll try_xxx).

After doing more debugging, it seems the child process is exiting in a way that tokio can't see because even with my "workaround", it doesn't get an ExitStatus. My child process is doing ffi which is probably the cause of the strange exiting, but I'm seeing a segfault or any kind of error reporting. I've gotta figure out whats going on with the emulator before I can draw conclusions on the code above.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.