use crate::error::{ChildFate, Error};
use crate::Unreaped;
#[derive(Debug)]
pub(crate) enum SpawnFailure {
NotStarted(Error),
#[cfg(target_os = "linux")]
NotStartedWithChild {
cause: Error,
#[cfg_attr(
not(feature = "tokio"),
allow(dead_code, reason = "only the tokio spawn's abandonment reads it")
)]
fate: ChildFate,
},
#[cfg(target_os = "linux")]
NotStartedUnreaped(Error),
MayHaveStarted {
cause: Error,
fate: ChildFate,
wrapper_elevated: bool,
unreaped: Option<Box<Unreaped>>,
},
}
impl SpawnFailure {
pub(crate) fn started(cause: Error, fate: ChildFate, unreaped: Option<Unreaped>) -> SpawnFailure {
SpawnFailure::MayHaveStarted {
cause,
fate,
wrapper_elevated: false,
unreaped: unreaped.map(Box::new),
}
}
#[cfg(any(test, unix))]
pub(crate) fn map(self, f: impl FnOnce(Error) -> Error) -> SpawnFailure {
match self {
SpawnFailure::NotStarted(e) => SpawnFailure::NotStarted(f(e)),
#[cfg(target_os = "linux")]
SpawnFailure::NotStartedUnreaped(e) => SpawnFailure::NotStartedUnreaped(f(e)),
#[cfg(target_os = "linux")]
SpawnFailure::NotStartedWithChild { cause, fate } => {
SpawnFailure::NotStartedWithChild { cause: f(cause), fate }
}
SpawnFailure::MayHaveStarted {
cause,
fate,
wrapper_elevated,
unreaped,
} => SpawnFailure::MayHaveStarted {
cause: f(cause),
fate,
wrapper_elevated,
unreaped,
},
}
}
pub(crate) fn behind_wrapper(self) -> SpawnFailure {
match self {
SpawnFailure::MayHaveStarted {
cause, fate, unreaped, ..
} => SpawnFailure::MayHaveStarted {
cause,
fate,
wrapper_elevated: true,
unreaped,
},
not_started => not_started,
}
}
#[cfg(all(feature = "tokio", target_os = "linux"))]
pub(crate) fn answered_by(self, f: impl FnOnce(Error) -> (Error, ChildFate)) -> SpawnFailure {
match self {
SpawnFailure::MayHaveStarted {
cause,
wrapper_elevated,
unreaped,
..
} => {
let (cause, fate) = f(cause);
SpawnFailure::MayHaveStarted {
cause,
fate,
wrapper_elevated,
unreaped,
}
}
not_started => not_started,
}
}
#[cfg(all(feature = "tokio", target_os = "linux"))]
pub(crate) fn error(&self) -> &Error {
match self {
SpawnFailure::NotStarted(e)
| SpawnFailure::NotStartedUnreaped(e)
| SpawnFailure::MayHaveStarted { cause: e, .. } => e,
SpawnFailure::NotStartedWithChild { cause, .. } => cause,
}
}
#[cfg(all(feature = "tokio", target_os = "linux"))]
pub(crate) fn left_child_unreaped(&self) -> bool {
matches!(self, SpawnFailure::NotStartedUnreaped(_))
}
#[cfg(all(feature = "tokio", target_os = "linux"))]
pub(crate) fn ended_child(&self) -> Option<ChildFate> {
match self {
SpawnFailure::NotStartedWithChild { fate, .. } | SpawnFailure::MayHaveStarted { fate, .. }
if matches!(fate, ChildFate::Reaped | ChildFate::Killed | ChildFate::Gone) =>
{
Some(*fate)
}
_ => None,
}
}
}
pub(crate) fn after_the_spawn(
cause: Error,
fate: ChildFate,
unreaped: Option<Unreaped>,
behind_wrapper: bool,
) -> Error {
Error::from(failed_after_the_spawn(cause, fate, unreaped, behind_wrapper))
}
pub(crate) fn failed_after_the_spawn(
cause: Error,
fate: ChildFate,
unreaped: Option<Unreaped>,
behind_wrapper: bool,
) -> SpawnFailure {
let failure = SpawnFailure::started(cause, fate, unreaped);
if behind_wrapper {
failure.behind_wrapper()
} else {
failure
}
}
#[cfg(all(test, unix))]
impl SpawnFailure {
#[track_caller]
pub(crate) fn expect_may_have_started_with(self) -> (Error, ChildFate) {
let (cause, fate, unreaped) = self.parts();
if let Some(unreaped) = unreaped {
unreaped.leak();
panic!("the spawn handed a child back ({fate:?}): {cause:?}");
}
(cause, fate)
}
#[track_caller]
pub(crate) fn parts(self) -> (Error, ChildFate, Option<Unreaped>) {
match self {
SpawnFailure::MayHaveStarted {
cause, fate, unreaped, ..
} => (cause, fate, unreaped.map(|unreaped| *unreaped)),
SpawnFailure::NotStarted(e) => panic!("expected `may have started`, got `not started`: {e:?}"),
#[cfg(target_os = "linux")]
SpawnFailure::NotStartedUnreaped(e) => panic!("expected `may have started`, got `not started`: {e:?}"),
#[cfg(target_os = "linux")]
SpawnFailure::NotStartedWithChild { cause, .. } => {
panic!("expected `may have started`, got `not started`: {cause:?}")
}
}
}
#[cfg(target_os = "linux")]
#[track_caller]
pub(crate) fn expect_not_started_with_child(self) -> (Error, ChildFate) {
match self {
SpawnFailure::NotStartedWithChild { cause, fate } => (cause, fate),
other => panic!("expected `not started` with a child's fate, got {other:?}"),
}
}
#[cfg_attr(
not(target_os = "linux"),
allow(
dead_code,
reason = "only the Linux handshake's tests take a not-started failure apart"
)
)]
#[track_caller]
pub(crate) fn expect_not_started(self) -> Error {
match self {
SpawnFailure::NotStarted(e) => e,
#[cfg(target_os = "linux")]
SpawnFailure::NotStartedUnreaped(e) => e,
#[cfg(target_os = "linux")]
SpawnFailure::NotStartedWithChild { cause, .. } => cause,
SpawnFailure::MayHaveStarted { cause, fate, .. } => {
panic!("expected `not started`, got `may have started` ({fate:?}): {cause:?}")
}
}
}
}
#[cfg(test)]
#[track_caller]
pub(crate) fn expect_may_have_started_with(error: Error) -> (Error, ChildFate) {
let (cause, fate, unreaped) = error_parts(error);
if let Some(unreaped) = unreaped {
unreaped.leak();
panic!("the spawn handed a child back ({fate:?}): {cause:?}");
}
(cause, fate)
}
#[cfg(test)]
#[track_caller]
pub(crate) fn error_parts(error: Error) -> (Error, ChildFate, Option<Unreaped>) {
match error {
Error::MayHaveStarted {
fate, source, unreaped, ..
} => (*source, fate, unreaped.map(|unreaped| *unreaped)),
other => panic!("expected Error::MayHaveStarted, got {other:?}"),
}
}
#[cfg(all(test, windows))]
pub(crate) fn unsupported(error: &Error) -> bool {
matches!(error, Error::Unsupported { .. })
}
#[cfg(all(test, windows))]
pub(crate) fn invalid_input(error: &Error) -> bool {
matches!(error, Error::Io(e) if e.kind() == std::io::ErrorKind::InvalidInput)
}
#[cfg(test)]
#[track_caller]
pub(crate) fn expect_not_started(error: Error) -> Error {
assert!(
!matches!(error, Error::MayHaveStarted { .. }),
"expected an error that says the program did not start, got {error:?}"
);
error
}
impl From<SpawnFailure> for Error {
fn from(failure: SpawnFailure) -> Error {
match failure {
SpawnFailure::NotStarted(error) => {
debug_assert!(
!matches!(error, Error::MayHaveStarted { .. }),
"a spawn answered both `not started` and `may have started`: {error}"
);
error
}
#[cfg(target_os = "linux")]
SpawnFailure::NotStartedUnreaped(error) => {
debug_assert!(
!matches!(error, Error::MayHaveStarted { .. }),
"a spawn answered both `not started` and `may have started`: {error}"
);
error
}
#[cfg(target_os = "linux")]
SpawnFailure::NotStartedWithChild { cause: error, .. } => {
debug_assert!(
!matches!(error, Error::MayHaveStarted { .. }),
"a spawn answered both `not started` and `may have started`: {error}"
);
error
}
SpawnFailure::MayHaveStarted {
cause,
fate,
wrapper_elevated,
unreaped,
} => {
debug_assert!(
!matches!(cause, Error::MayHaveStarted { .. }),
"a `may have started` failure's cause is itself `may have started`: {cause}"
);
debug_assert!(
unreaped.as_ref().is_none_or(|child| match fate {
ChildFate::Running { id } => id == child.id(),
ChildFate::Unknown | ChildFate::Killed => true,
_ => false,
}),
"a child handed back is `Running` with its identity, `Unknown` or `Killed`, got {fate:?}"
);
Error::MayHaveStarted {
fate,
wrapper_elevated,
source: Box::new(cause),
unreaped,
}
}
}
}
}
pub(crate) trait PlainError: Into<Error> + sealed::Sealed {}
impl PlainError for Error {}
impl PlainError for std::io::Error {}
mod sealed {
pub trait Sealed {}
impl Sealed for crate::error::Error {}
impl Sealed for std::io::Error {}
}
pub(crate) trait Classify<T> {
fn not_started(self) -> Result<T, SpawnFailure>;
}
impl<T, E: PlainError> Classify<T> for Result<T, E> {
fn not_started(self) -> Result<T, SpawnFailure> {
self.map_err(|e| SpawnFailure::NotStarted(e.into()))
}
}
#[cfg(test)]
pub(crate) mod seams {
use std::cell::Cell;
use std::thread::LocalKey;
use crate::error::Error;
thread_local! {
static WAIT: Cell<bool> = const { Cell::new(false) };
static PUMP: Cell<bool> = const { Cell::new(false) };
static WRAPPER: Cell<bool> = const { Cell::new(false) };
}
pub(crate) const FAILURE: &str = "forced failure after the spawn (test seam)";
#[must_use = "the seam is cleared as soon as the guard is dropped"]
pub(crate) struct Armed(&'static LocalKey<Cell<bool>>);
impl Drop for Armed {
fn drop(&mut self) {
self.0.with(|f| f.set(false));
}
}
fn arm(flag: &'static LocalKey<Cell<bool>>) -> Armed {
flag.with(|f| f.set(true));
Armed(flag)
}
pub(crate) fn fail_the_next_wait() -> Armed {
arm(&WAIT)
}
pub(crate) fn fail_the_next_pump() -> Armed {
arm(&PUMP)
}
pub(crate) fn pretend_wrapper() -> Armed {
arm(&WRAPPER)
}
fn take(flag: &'static LocalKey<Cell<bool>>) -> Option<Error> {
flag.with(|f| f.replace(false))
.then(|| Error::Io(std::io::Error::other(FAILURE)))
}
pub(crate) fn take_wait_failure() -> Option<Error> {
take(&WAIT)
}
pub(crate) fn take_pump_failure() -> Option<Error> {
take(&PUMP)
}
pub(crate) fn wrapper() -> bool {
WRAPPER.with(Cell::get)
}
}
#[cfg(test)]
#[path = "failure_tests.rs"]
mod failure_tests;