use crate::error::{ChildFate, Error};
#[derive(Debug)]
pub(crate) enum SpawnFailure {
NotStarted(Error),
MayHaveStarted {
cause: Error,
fate: ChildFate,
wrapper_elevated: bool,
},
}
impl SpawnFailure {
pub(crate) fn started(cause: Error, fate: ChildFate) -> SpawnFailure {
SpawnFailure::MayHaveStarted {
cause,
fate,
wrapper_elevated: false,
}
}
#[cfg(any(test, unix))]
pub(crate) fn map(self, f: impl FnOnce(Error) -> Error) -> SpawnFailure {
match self {
SpawnFailure::NotStarted(e) => SpawnFailure::NotStarted(f(e)),
SpawnFailure::MayHaveStarted {
cause,
fate,
wrapper_elevated,
} => SpawnFailure::MayHaveStarted {
cause: f(cause),
fate,
wrapper_elevated,
},
}
}
pub(crate) fn behind_wrapper(self) -> SpawnFailure {
match self {
SpawnFailure::MayHaveStarted { cause, fate, .. } => SpawnFailure::MayHaveStarted {
cause,
fate,
wrapper_elevated: true,
},
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,
..
} => {
let (cause, fate) = f(cause);
SpawnFailure::MayHaveStarted {
cause,
fate,
wrapper_elevated,
}
}
not_started => not_started,
}
}
#[cfg(all(feature = "tokio", target_os = "linux"))]
pub(crate) fn error(&self) -> &Error {
match self {
SpawnFailure::NotStarted(e) | SpawnFailure::MayHaveStarted { cause: e, .. } => e,
}
}
}
pub(crate) fn after_the_spawn(cause: Error, fate: ChildFate, behind_wrapper: bool) -> Error {
let failure = SpawnFailure::started(cause, fate);
Error::from(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) {
match self {
SpawnFailure::MayHaveStarted { cause, fate, .. } => (cause, fate),
SpawnFailure::NotStarted(e) => panic!("expected `may have started`, got `not started`: {e:?}"),
}
}
#[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,
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) {
match error {
Error::MayHaveStarted { fate, source, .. } => (*source, fate),
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
}
SpawnFailure::MayHaveStarted {
cause,
fate,
wrapper_elevated,
} => {
debug_assert!(
!matches!(cause, Error::MayHaveStarted { .. }),
"a `may have started` failure's cause is itself `may have started`: {cause}"
);
Error::MayHaveStarted {
fate,
wrapper_elevated,
source: Box::new(cause),
}
}
}
}
}
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;