/// cleanup tries to give the daemon a chance to exit but eventually runs pkill to ensure nothing
/// is left running.
cleanup tries to give the daemon a chance to exit but eventually runs pkill to ensure nothing
is left running.
cleanup tries to give the daemon a chance to exit but eventually runs pkill to ensure nothing is left running .
async fn cleanup() -> Result<()> {
    let max_attempts = 4;
    let daemon_start_arg = "(^|/)ffx (-.* )?daemon start$";
    for attempt in 0..max_attempts {
        let did_find_daemon = fuchsia_async::unblock(move || {
            let mut cmd = Command::new("pgrep");
            cmd.arg("-f")
                .arg(daemon_start_arg)
                .stdin(Stdio::null())
                .stdout(Stdio::null())
                .stderr(Stdio::null());
            let status = cmd.status()?;
            Ok::<_, anyhow::Error>(status.success())
        })
        .await?;
        if did_find_daemon {
            // Daemon stop waits 20ms before exiting so we try to avoid a race by waiting here.
            // Based on max_attempts of 4 this will wait a maximum of 500ms.
            fuchsia_async::Timer::new(Duration::from_millis(50 * (1 + attempt))).await;
            continue;
        } else {
            return Ok(());
        }
    }
    // Success here means that pkill was able to find something to kill so that means a daemon
    // was still running that we did not expect to be running. We return an error here to make
    // this a failure of the test suite as a whole to find out when this is happening.
    let mut cmd = Command::new("pkill");
    cmd.arg("-f")
        .arg(daemon_start_arg)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    let did_kill_daemon = cmd.status()?.success();
    if did_kill_daemon {
        // We do not return an error here because that was causing flakes.
        // I think we should still clean up here but we need a less racey shutdown path before we
        // can return an error here to keep the flake rate down.
        eprintln!("A daemon was killed that was not supposed to be running");
    }
    Ok(())
}
async fn cleanup ( ) -> Result < ( ) > { let max_attempts = 4 ; let daemon_start_arg = " " ; for attempt in 0 .. max_attempts { let did_find_daemon = fuchsia_async :: unblock ( move | | { let mut cmd = Command :: new ( " " ) ; cmd . arg ( " " ) . arg ( daemon_start_arg ) . stdin ( Stdio :: null ( ) ) . stdout ( Stdio :: null ( ) ) . stderr ( Stdio :: null ( ) ) ; let status = cmd . status ( ) ? ; Ok :: < _ , anyhow :: Error > ( status . success ( ) ) } ) . await ? ; if did_find_daemon { // Daemon stop waits 20ms before exiting so we try to avoid a race by waiting here. // Based on max_attempts of 4 this will wait a maximum of 500ms. fuchsia_async :: Timer :: new ( Duration :: from_millis ( 50 * ( 1 + attempt ) ) ) . await ; continue ; } else { return Ok ( ( ) ) ; } } // Success here means that pkill was able to find something to kill so that means a daemon // was still running that we did not expect to be running. We return an error here to make // this a failure of the test suite as a whole to find out when this is happening. let mut cmd = Command :: new ( " " ) ; cmd . arg ( " " ) . arg ( daemon_start_arg ) . stdin ( Stdio :: null ( ) ) . stdout ( Stdio :: null ( ) ) . stderr ( Stdio :: null ( ) ) ; let did_kill_daemon = cmd . status ( ) ? . success ( ) ; if did_kill_daemon { // We do not return an error here because that was causing flakes. // I think we should still clean up here but we need a less racey shutdown path before we // can return an error here to keep the flake rate down. eprintln ! ( " " ) ; } Ok ( ( ) ) }
cleanup tries to give the daemon a chance to exit but eventually runs pkill to ensure nothing
is left running.
cleanup tries to give the daemon a chance to exit but eventually runs pkill to ensure nothing is left running .