cosca 0.10.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! `terminate`, `graceful_shutdown`, `terminate_tree` and `graceful_shutdown_tree`, each written once for the
//! sync and the async `Child`; a [`Waits`] supplies the stop action's run and each wait ([`BlockingDriver`]
//! blocks, `tokio::Child` awaits).

use std::future::Future;
use std::pin::pin;
use std::process::ExitStatus;
use std::task::{Context, Poll, Waker};
use std::time::Duration;

#[cfg(test)]
use super::fault;
use super::{take_forced_sweep, take_forced_watch_error, Child, SignalTreeCooperativelyOutcome};
use crate::child::owned::ChildWaitOutcome;
use crate::containment::dispatch::{SweepFailure, Swept};
use crate::containment::TreeDrain;
use crate::error::Error;
#[cfg(target_os = "macos")]
use crate::os_process::child::Foreign;
use crate::stop::{StopTarget, StopWaits, TreeCall};

/// What the four stop calls need of a driver: the stop action's run, and the waits [`shutdown_lone`] and
/// [`shutdown_tree`] make, beside the root's exit look of [`StopWaits`]. A deadline is a `Duration` from the
/// wait's start, which each driver turns into an instant on its own clock.
pub(crate) trait Waits: StopWaits {
    /// The child the call acts on: its gates, signals, tree and pid.
    fn child(&self) -> &Child;

    /// Runs the child's stop action on `target`: the sync driver calls it, the async driver awaits it.
    async fn stop(&self, target: &StopTarget<'_>) -> Result<(), Error>;

    /// Until every member of the tree has exited (not reaped), or `grace` is over: the public
    /// `wait_tree_timeout`, through the tree's drain edge, or `Unsupported` where the mechanism has none.
    async fn tree_drained(&self, grace: Duration) -> Result<TreeDrain, Error>;

    /// The sweep's answer once every member its kill reached has exited.
    async fn sweep(&self, swept: Swept<'_>) -> Result<(), SweepFailure>;

    /// The root's reap, as the wait ended: the exit record or the foreign verdict, unfolded and with no
    /// warn ([`graceful_status_of_reap`] answers for it). The async driver closes an untaken stdin
    /// first, as its `wait` does.
    async fn reap(&mut self) -> Result<ChildWaitOutcome, Error>;

    /// The error of a holders-only shutdown, once the holders its kill reached have exited.
    #[cfg(unix)]
    async fn holders_finish(&self, shutdown: super::HoldersShutdown<'_>) -> Error;
}

/// The sync `Child`'s waits.
pub(crate) struct BlockingDriver<'a>(pub(crate) &'a Child);

impl StopWaits for BlockingDriver<'_> {
    async fn root_exited(&self, within: Duration) -> Result<bool, Error> {
        self.0.proc.exited_by(crate::wait::deadline_from(within))
    }

    #[cfg(windows)]
    type Proxy = Child;

    #[cfg(windows)]
    async fn start_proxy(
        &self,
        exe: &std::path::Path,
        args: &crate::stop::proxy::protocol::ProxyArgs,
    ) -> Result<(u32, Child), Error> {
        let mut command = crate::Command::new();
        crate::stop::proxy::configure_start(&mut command, exe, args)?;
        let proxy = command.spawn()?;
        Ok((proxy.pid(), proxy))
    }

    #[cfg(windows)]
    async fn wait_proxy(
        &self,
        proxy: &mut Child,
        after: Duration,
        late: impl FnOnce(Duration) + Send,
    ) -> Result<std::process::ExitStatus, Error> {
        proxy.wait_saying(after, late)
    }
}

impl Waits for BlockingDriver<'_> {
    fn child(&self) -> &Child {
        self.0
    }

    async fn stop(&self, target: &StopTarget<'_>) -> Result<(), Error> {
        self.0.stop.run(target)
    }

    async fn tree_drained(&self, grace: Duration) -> Result<TreeDrain, Error> {
        self.0.wait_tree_timeout(grace)
    }

    async fn sweep(&self, swept: Swept<'_>) -> Result<(), SweepFailure> {
        swept.wait()
    }

    async fn reap(&mut self) -> Result<ChildWaitOutcome, Error> {
        self.0.reap_outcome()
    }

    #[cfg(unix)]
    async fn holders_finish(&self, shutdown: super::HoldersShutdown<'_>) -> Error {
        shutdown.finish()
    }
}

/// Polls `f` once with a no-op waker and returns its output. `f` must await only a [`BlockingDriver`],
/// whose waits are all ready.
///
/// # Panics
///
/// If `f` is pending: a hang here would hide the broken contract.
pub(crate) fn run_ready<F: Future>(f: F) -> F::Output {
    match pin!(f).poll(&mut Context::from_waker(Waker::noop())) {
        Poll::Ready(output) => output,
        Poll::Pending => panic!("run_ready: the future is pending, but a blocking driver's waits are all ready"),
    }
}

/// What `op` answers for the root's reap, `outcome`, once its signals went out: the status, or the
/// reap's failure, or, for a root this process cannot collect, [`Error::Unassessable`] saying so. A
/// zombie launchd holds is the macOS refusal ([`unpinned_status`](crate::signal::unpinned_status));
/// a root reaped elsewhere (or whose pid names another process now) is the same, with the `ECHILD`
/// every wait answers for it as its `source()`. Neither warns: the caller's answer is the report.
fn graceful_status_of_reap(pid: u32, op: &str, outcome: Result<ChildWaitOutcome, Error>) -> Result<ExitStatus, Error> {
    use crate::child::lifecycle::{echild, unbounded_status, Orphaned};

    match outcome? {
        ChildWaitOutcome::Foreign(foreign) => {
            #[cfg(target_os = "macos")]
            if foreign == Foreign::Orphaned {
                return Err(crate::signal::unpinned_status(pid, op));
            }
            let _ = foreign;
            log::debug!("{op}({pid}): the root was reaped elsewhere");
            Err(Error::Unassessable {
                detail: format!(
                    "pid {pid}: {op} sent its signals, but its exit status cannot be collected: the root was \
                     reaped elsewhere"
                ),
                source: Some(std::sync::Arc::new(Error::Io(echild()))),
            })
        }
        reaped => unbounded_status(Some(pid), reaped, Orphaned::Quiet),
    }
}

/// [`Child::terminate`](crate::Child::terminate), for any [`Waits`].
pub(crate) async fn terminate<W: Waits>(w: &W) -> Result<(), Error> {
    w.stop(&StopTarget::root(w.child())).await
}

/// [`Child::terminate_tree`](crate::Child::terminate_tree), for any [`Waits`].
pub(crate) async fn terminate_tree<W: Waits>(w: &W) -> Result<(), Error> {
    w.child().require_contained()?;
    // A separate `terminate_tree` is no call that sweeps: it holds its members for the next kill.
    w.stop(&StopTarget::tree(w.child(), TreeCall::SignalOnly)).await
}

/// [`terminate`] with what the action said it sent, for the kill after the grace; see
/// [`Child::after_root_stop`].
pub(crate) async fn stop_root<W: Waits>(w: &W) -> (Result<(), Error>, String) {
    let target = StopTarget::root(w.child());
    let stopped = w.stop(&target).await;
    w.child().after_root_stop(target, stopped)
}

/// `graceful_shutdown_tree`'s cooperative stop: the child's stop action, run on a target that stands for the
/// tree, and what the call goes on from. Only [`Child::after_tree_stop`] reads the record, once the action
/// has returned, awaited or not: the root a prepared send holds is dropped with the send, before the
/// grace, and never lives in an async caller's state.
pub(crate) async fn signal_tree_cooperatively<W: Waits>(w: &W) -> SignalTreeCooperativelyOutcome {
    #[cfg(test)]
    if let Some(forced) = w.child().forced_in_place_of_the_stop() {
        return forced;
    }
    let call = crate::containment::treewalk::WalkToken::new();
    let target = StopTarget::tree(w.child(), TreeCall::Shutdown(&call));
    let acted = w.stop(&target).await;
    w.child().after_tree_stop(target, acted)
}

/// [`Child::graceful_shutdown`](crate::Child::graceful_shutdown), for any [`Waits`]: the signal, the
/// grace, the kill unless the root exited, and the reap.
pub(crate) async fn shutdown_lone<W: Waits>(w: &mut W, grace: Duration) -> Result<ExitStatus, Error> {
    // What the stop sent is what a refused kill after it says was sent.
    let (stopped, earlier) = stop_root(&*w).await;
    stopped?;
    #[cfg(test)]
    fault::run_hook(fault::HookPoint::AfterTerminate);

    let pid = w.child().pid();
    let call = || format!("graceful_shutdown({pid})");
    // A watch failure must not strand the child between the soft signal and the escalation: the
    // kill and the reap still run, then the failure surfaces.
    let watch = w.root_exited(grace).await;
    let watch = crate::graceful::after_grace(call, watch, || {
        #[cfg(test)]
        fault::run_hook(fault::HookPoint::BeforeEscalation);
        w.child().kill_after(Some(&earlier))
    })?;
    #[cfg(test)]
    fault::run_hook(fault::HookPoint::BeforeReap);
    let waited = graceful_status_of_reap(pid, "graceful_shutdown", w.reap().await);
    crate::graceful::after_reap(call, waited, None, watch)
}

/// [`Child::graceful_shutdown_tree`](crate::Child::graceful_shutdown_tree), for any [`Waits`]: the signal,
/// the grace, the sweep unless the tree drained, and the reap.
pub(crate) async fn shutdown_tree<W: Waits>(w: &mut W, grace: Duration) -> Result<ExitStatus, Error> {
    // Fail fast before sending any signal. terminate_tree/kill_tree re-check this guard
    // internally; the redundancy is intentional so an uncontained child errors up front.
    w.child().require_contained()?;

    // The mechanism's look at the root decides the cooperative signal (see `terminate_tree`), and
    // what the sweep may say was sent before it.
    let (term, earlier, walk) = match signal_tree_cooperatively(&*w).await {
        SignalTreeCooperativelyOutcome::Sent { result, earlier, walk } => (result, earlier, walk),
        SignalTreeCooperativelyOutcome::End(e) => return Err(e),
        #[cfg(target_os = "linux")]
        SignalTreeCooperativelyOutcome::RootThroughPidfd(skip, started, sig, acted) => {
            let ended = root_through_pidfd(w, &skip, started, sig, grace).await;
            return Err(ended_with(w, ended, acted));
        }
        #[cfg(unix)]
        SignalTreeCooperativelyOutcome::HoldersOnly(skip, sig, acted) => {
            let ended = holders_only(w, skip, sig, grace).await;
            return Err(ended_with(w, ended, acted));
        }
    };

    // The signal went out, so every error of the check after it holds, whatever its variant: the
    // grace is waited and the sweep can still reach what the signal did not. A refusal is dropped
    // when the sweep proves the tree clear.
    #[cfg(test)]
    fault::run_hook(fault::HookPoint::AfterTerminate);

    let pid = w.child().pid();
    let call = || format!("graceful_shutdown_tree({pid})");
    // Watch-Err ordering: sweep + reap first, then surface (see `Child::graceful_shutdown`).
    //
    // `drained`: the tree's drain edge proved every member exited (`permits_skipping_sweep`), so the
    // sweep is skipped. `AllMarkersClosed` is advisory (a live process can close its marker fd) and
    // does not count.
    // `root_exited`: the root specifically exited; it comes from the drain edge or a zero-duration
    // root probe, since a tree verdict that does not clear the tree says nothing about the root. Only
    // used to decide whether a best-effort reap after a sweep failure is safe.
    let (drained, root_exited, watch_err) = if let Some(e) = take_forced_watch_error() {
        (false, false, Some(e))
    } else if w.child().tree.drain_edge().is_some() {
        match w.tree_drained(grace).await {
            Ok(verdict) if verdict.permits_skipping_sweep() => (true, true, None),
            // A zero grace is one look.
            Ok(_) => match w.root_exited(Duration::ZERO).await {
                Ok(exited) => (false, exited, None),
                Err(e) => (false, false, Some(e)),
            },
            Err(e) => (false, false, Some(e)),
        }
    } else {
        match w.root_exited(grace).await {
            Ok(exited) => (false, exited, None),
            Err(e) => (false, false, Some(e)),
        }
    };
    // A sweep Err is the answer, with the terminate and watch failures below it; it must propagate
    // before the reap on a LIVE root (waiting unswept would hang), but once the root is dead (its
    // exit was observed, or the sweep's backstop killed it), the reap runs first.
    if !drained {
        // A test forces this sweep to fail to prove it is skipped when `drained`.
        let sweep_result = match take_forced_sweep() {
            Some(failure) => Err(failure),
            None => {
                w.sweep(
                    w.child()
                        .kill_tree_after("graceful_shutdown_tree", earlier.as_deref(), walk.as_ref()),
                )
                .await
            }
        };
        if let Err(sweep) = sweep_result {
            if root_exited || sweep.root_ended {
                // Best-effort: a dead root must not stay a zombie; `sweep` is the reported error. One
                // attempt that does not wait, through the handle: a zombie a tracer holds, or a root
                // that is not ours to reap, stays with the handle for its `wait` or `Drop`.
                if let Err(e) = w.child().proc.wait(Some(crate::wait::now())) {
                    log::warn!(
                        "graceful_shutdown_tree({pid}): the best-effort reap after a swept, exited root also failed: {report}",
                        report = crate::error::Report(&e)
                    );
                }
            }
            return Err(crate::graceful::joined(
                &call(),
                sweep.error,
                term.err().into_iter().chain(watch_err),
            ));
        }
    }
    #[cfg(test)]
    fault::run_hook(fault::HookPoint::BeforeReap);
    let waited = graceful_status_of_reap(pid, "graceful_shutdown_tree", w.reap().await);
    // The tree is confirmed clear (drained, or the sweep returned Ok), which disproves a `term`
    // refusal: a successful reap discards it; a failed reap keeps it as a peer.
    if let (Ok(_), Err(e)) = (&waited, &term) {
        log::debug!(
            "graceful_shutdown_tree({pid}): tree confirmed clear; discarding the superseded terminate_tree refusal: {report}",
            report = crate::error::Report(e)
        );
    }
    crate::graceful::after_reap(call, waited, term.err(), watch_err)
}

/// The error of a shutdown that ended on a skipped outcome, with the error of the action that sent
/// (`acted`) joined to it.
#[cfg(unix)]
fn ended_with<W: Waits>(w: &W, ended: Error, acted: Option<Error>) -> Error {
    crate::graceful::joined(&format!("graceful_shutdown_tree({})", w.child().pid()), ended, acted)
}

/// [`TerminateOutcome::RootThroughPidfd`](crate::containment::TerminateOutcome::RootThroughPidfd),
/// `skip` what it skipped: the signal `sig` went to the root through its handle (`shutdown`, the
/// send a prepared function made); the grace, then a kill through it if the root outlived it. Nothing
/// goes out by the number.
#[cfg(target_os = "linux")]
pub(crate) async fn root_through_pidfd<W: Waits>(
    w: &W,
    skip: &crate::containment::Skip,
    shutdown: super::PidfdShutdown,
    sig: crate::signal::Sig,
    grace: Duration,
) -> Error {
    let child = w.child();
    let call = child.skip_call("graceful_shutdown_tree", sig, None);
    #[cfg(test)]
    fault::run_hook(fault::HookPoint::AfterTerminate);
    let exited = if shutdown.needs_grace() {
        Some(w.root_exited(grace).await)
    } else {
        None
    };
    shutdown.finish(&call, skip, exited, || child.proc.kill_root_only(Some(&child.tree)))
}

/// [`TerminateOutcome::HoldersOnly`](crate::containment::TerminateOutcome::HoldersOnly), `skip` what it
/// skipped: the holders were sent the signal `sig`; the grace, then the mechanism's own kill of any left.
#[cfg(unix)]
pub(crate) async fn holders_only<W: Waits>(
    w: &W,
    skip: crate::containment::Skip,
    sig: crate::signal::Sig,
    grace: Duration,
) -> Error {
    let child = w.child();
    let call = child.skip_call("graceful_shutdown_tree", sig, None);
    // A failed grace wait means the holders are killed now.
    let watch = w.tree_drained(grace).await.err();
    let label = format!("graceful_shutdown_tree({})", child.pid());
    crate::graceful::joined(
        &label,
        w.holders_finish(child.holders_shutdown(&call, skip)).await,
        watch,
    )
}

#[cfg(test)]
#[path = "shutdown_tests.rs"]
mod shutdown_tests;