cosca 0.8.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! The Windows async frozen spawn: the ordinary async spawn with a suspended request.

use super::{Child, SuspendedChild};
use crate::child::spawn::spawn_uncommitted;
use crate::child::suspended::platform::{refusal, spawn_frozen};
use crate::containment::windows::{resume_initial_threads, SuspendCount};
use crate::error::Error;
use crate::tokio::child::async_policy;
use crate::tokio::spawn::{on_pool, spawn_failure};
use crate::tokio::Command;

/// Test-only accessors, `pub` so integration tests can call them.
impl SuspendedChild {
    /// Whether the child is inside its containment job.
    #[doc(hidden)]
    pub fn test_job_handle_contains_self(&self) -> bool {
        self.child.test_job_handle_contains_self()
    }

    /// Test-only: whether the child's containment job kills its members when its last handle closes.
    /// `false` if the child is not job-contained; panics if the job's limits cannot be queried.
    #[doc(hidden)]
    pub fn test_job_kills_on_close(&self) -> bool {
        self.child.inner.tree.job_kills_on_close_for_test()
    }

    /// The containment the child has (and keeps after `resume`).
    #[doc(hidden)]
    pub fn test_containment(&self) -> crate::Containment {
        self.child.containment()
    }
}

pub(super) async fn spawn(cmd: &mut Command) -> Result<SuspendedChild, Error> {
    // Before the runtime is asked for: a refusal is the same verdict inside and outside one.
    if let Some(refusal) = refusal(cmd.as_sync_mut()) {
        return Err(refusal);
    }
    let policy = async_policy();
    let (child, wanted_kill_on_drop) = on_pool(
        cmd.as_sync_mut(),
        "cosca::tokio::Command::spawn_suspended",
        move |cmd| spawn_frozen(cmd, |cmd| spawn_uncommitted(cmd, policy)),
    )
    .await
    .map_err(spawn_failure)??;
    Ok(SuspendedChild {
        child: Child::from_sync(child),
        wanted_kill_on_drop,
    })
}

/// Resume the child's initial thread, which must have been suspended exactly once.
pub(super) fn release(child: &Child) -> std::io::Result<()> {
    resume_initial_threads(child.inner.process_handle(), SuspendCount::ExactlyOne)
}