cosca 0.9.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! A failed spawn's answer to whether the program could have started.
//!
//! Every spawn, sync and tokio, fails with a [`SpawnFailure`], which the public entry points turn
//! into an [`Error`]: [`Error::MayHaveStarted`] around the cause, or the cause alone.
//!
//! There is deliberately no `From<Error>` (nor `From<io::Error>`) for [`SpawnFailure`], so a `?`
//! on a plain error does not compile in a function that returns one. Every failure point in a
//! spawn names its answer: [`Classify::not_started`] or [`SpawnFailure::started`].

use std::sync::Arc;

use crate::child::drop_teardown::Torn;
use crate::error::{ChildFate, Error, Failures};
use crate::Unreaped;

/// A failed spawn's error, and whether the program could have started.
#[derive(Debug)]
pub(crate) enum SpawnFailure {
    /// The program did not start, and cosca has proved it.
    NotStarted(Error),
    /// As [`NotStarted`](Self::NotStarted), after the spawn created a child it held and dealt with:
    /// `fate` is what became of it. Linux (a child ended before `exec`) and Windows (a child still
    /// suspended) produce it.
    #[cfg_attr(
        not(any(target_os = "linux", windows)),
        allow(
            dead_code,
            reason = "only the Linux and Windows spawns hold a child they prove never ran"
        )
    )]
    NotStartedWithChild {
        /// What failed.
        cause: Error,
        /// What the failed spawn did with the child it held.
        #[cfg_attr(
            windows,
            allow(dead_code, reason = "read by `map`, which no Windows spawn calls, and by tests")
        )]
        fate: ChildFate,
    },
    /// The program may have started: the child reached `exec` (on Windows, `CreateProcess` or
    /// `ShellExecuteEx` succeeded), or cosca cannot prove it did not.
    MayHaveStarted {
        /// What failed. Never itself an [`Error::MayHaveStarted`]. Boxed so that a `Result` of a
        /// `SpawnFailure` stays small (`clippy::result_large_err`).
        cause: Box<Error>,
        /// What the failed spawn did with the child.
        fate: ChildFate,
        /// Whether the child is an elevation front (see [`Error::MayHaveStarted`]).
        wrapper_elevated: bool,
        /// What the spawn did with the child, beyond `fate` (see [`Error::MayHaveStarted`]).
        detail: Option<String>,
        /// The child, when the spawn hands it to the caller (see [`Error::MayHaveStarted`]).
        unreaped: Option<Box<Unreaped>>,
        /// What failed in the teardown the spawn ran on the child (see [`Error::MayHaveStarted`]).
        cleanup: Option<Failures>,
    },
}

impl SpawnFailure {
    /// A failure after which the program may have started, of a child that is not an elevation
    /// front. `unreaped` is the child the spawn hands back, if it does; then `fate` is `Running`
    /// with its identity, or `Unknown` (the look at it failed), or `Killed` (the wait for it failed).
    pub(crate) fn started(cause: Error, fate: ChildFate, unreaped: Option<Unreaped>) -> SpawnFailure {
        SpawnFailure::MayHaveStarted {
            cause: Box::new(cause),
            fate,
            wrapper_elevated: false,
            detail: None,
            unreaped: unreaped.map(Box::new),
            cleanup: None,
        }
    }

    /// This failure with `detail`, what the spawn did with its child beyond its fate, if there is
    /// any. A failure that says the program did not start has no child to say it of.
    pub(crate) fn with_detail(self, detail: Option<String>) -> SpawnFailure {
        let Some(detail) = detail else {
            return self;
        };
        match self {
            SpawnFailure::MayHaveStarted {
                cause,
                fate,
                wrapper_elevated,
                detail: earlier,
                unreaped,
                cleanup,
            } => {
                debug_assert!(
                    earlier.is_none(),
                    "a failure's detail is set once: {earlier:?}, then {detail:?}"
                );
                SpawnFailure::MayHaveStarted {
                    cause,
                    fate,
                    wrapper_elevated,
                    detail: Some(detail),
                    unreaped,
                    cleanup,
                }
            }
            other => {
                debug_assert!(
                    false,
                    "a spawn that did not start has no detail of its child: {other:?}"
                );
                other
            }
        }
    }

    /// This failure with `cleanup`, what failed in the teardown it ran on its child, after any it
    /// has, beside its cause. A failure that says the program did not start has no teardown to
    /// report.
    pub(crate) fn with_cleanup(self, cleanup: Option<Failures>) -> SpawnFailure {
        let Some(cleanup) = cleanup else {
            return self;
        };
        match self {
            SpawnFailure::MayHaveStarted {
                cause,
                fate,
                wrapper_elevated,
                detail,
                unreaped,
                cleanup: earlier,
            } => SpawnFailure::MayHaveStarted {
                cause,
                fate,
                wrapper_elevated,
                detail,
                unreaped,
                cleanup: Failures::merged(earlier, Some(cleanup)),
            },
            other => {
                debug_assert!(false, "a spawn that did not start has no cleanup: {other:?}");
                other
            }
        }
    }

    /// Applies `f` to the error, keeping the answer.
    #[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::NotStartedWithChild { cause, fate } => {
                SpawnFailure::NotStartedWithChild { cause: f(cause), fate }
            }
            SpawnFailure::MayHaveStarted {
                cause,
                fate,
                wrapper_elevated,
                detail,
                unreaped,
                cleanup,
            } => SpawnFailure::MayHaveStarted {
                cause: Box::new(f(*cause)),
                fate,
                wrapper_elevated,
                detail,
                unreaped,
                cleanup,
            },
        }
    }

    /// This failure, if it says the program may have started, marked as that of an elevation
    /// front, whose elevated program may outlive its fate (see [`Error::MayHaveStarted`]).
    pub(crate) fn behind_front(self) -> SpawnFailure {
        match self {
            SpawnFailure::MayHaveStarted {
                cause,
                fate,
                detail,
                unreaped,
                cleanup,
                ..
            } => SpawnFailure::MayHaveStarted {
                cause,
                fate,
                wrapper_elevated: true,
                detail,
                unreaped,
                cleanup,
            },
            not_started => not_started,
        }
    }
}

/// The error of a run-to-completion helper (`output`, `status`, `read`) that failed after its spawn
/// succeeded: the program may have started, and `torn` says what its teardown left of the child: its
/// fate, the child itself if the teardown could not end it (handed back), and what failed in the
/// teardown, which goes beside the cause. `behind_front` is whether the child is an elevation front.
pub(crate) fn after_the_spawn(cause: Error, torn: Torn, behind_front: bool) -> Error {
    Error::from(failed_after_the_spawn(cause, torn, behind_front))
}

/// [`after_the_spawn`], as the [`SpawnFailure`] a spawn returns.
pub(crate) fn failed_after_the_spawn(cause: Error, torn: Torn, behind_front: bool) -> SpawnFailure {
    let Torn {
        fate,
        unreaped,
        cleanup,
    } = torn;
    let failure = SpawnFailure::started(cause, fate, unreaped).with_cleanup(cleanup);
    if behind_front {
        failure.behind_front()
    } else {
        failure
    }
}

/// [`failed_after_the_spawn`] for a `cause` that called for a teardown of the child, whose result is
/// `torn_down`. `Err(cancelled)` is a teardown that did not run, because the task that was to run it
/// was cancelled and dropped the child instead: the `cause` stays, `cancelled` goes beside it as the
/// teardown's failure, and nothing reports what the drop did, so the fate is [`ChildFate::Unknown`].
/// (`Running { id: None }` would say cosca left the child alone before its identity was read.)
pub(crate) fn failed_after_teardown(cause: Error, behind_front: bool, torn_down: Result<Torn, Error>) -> SpawnFailure {
    let torn = torn_down.unwrap_or_else(|cancelled| Torn {
        fate: ChildFate::Unknown,
        unreaped: None,
        cleanup: Failures::collect([Arc::new(cancelled)]),
    });
    failed_after_the_spawn(cause, torn, behind_front)
}

/// The failure of a spawn whose attach failed with `cause` after its child was torn down as `torn`
/// says, which `detail` and `attach_cleanup` add to (see [`Error::MayHaveStarted`]). `not_started`:
/// the attach says the program never ran.
///
/// A not-started attach failure is always [`NotStartedWithChild`]: nothing is handed back, and the
/// teardown's failures ([`tear_down_unexecuted`](super::held_teardown::tear_down_unexecuted)) were
/// said in the spawn's warn, which that variant has no place for.
///
/// [`NotStartedWithChild`]: SpawnFailure::NotStartedWithChild
pub(crate) fn failed_attach(
    not_started: bool,
    cause: Error,
    detail: Option<String>,
    attach_cleanup: Option<Failures>,
    torn: Torn,
) -> SpawnFailure {
    let Torn {
        fate,
        unreaped,
        cleanup,
    } = torn;
    if not_started {
        debug_assert!(
            unreaped.is_none() && detail.is_none() && attach_cleanup.is_none() && cleanup.is_none(),
            "a child that never ran has nothing to hand back, detail or clean up after the attach, and \
             its teardown's failures were said in the spawn's warn"
        );
        return SpawnFailure::NotStartedWithChild { cause, fate };
    }
    SpawnFailure::started(cause, fate, unreaped)
        .with_detail(detail)
        .with_cleanup(attach_cleanup)
        .with_cleanup(cleanup)
}

// Only the Unix tests take a `SpawnFailure` apart; the rest read the public `Error`.
#[cfg(all(test, unix))]
impl SpawnFailure {
    /// The error and fate of a failure that answered that the program may have started; panics
    /// otherwise.
    #[track_caller]
    pub(crate) fn expect_may_have_started_with(self) -> (Error, ChildFate) {
        let (cause, fate, unreaped) = self.parts();
        // Dropping a handed-back child would wait for it; a test that gets one takes it apart with
        // `test_child::SpawnFailure::expect_may_have_started_parts`.
        if let Some(unreaped) = unreaped {
            unreaped.leak();
            panic!("the spawn handed a child back ({fate:?}): {cause:?}");
        }
        (cause, fate)
    }

    /// The error, fate and handed-back child (if any) of a failure that answered that the program
    /// may have started; panics otherwise. For `test_child`, which wraps the child in its
    /// `HandedBack`.
    #[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:?}"),
            SpawnFailure::NotStartedWithChild { cause, .. } => {
                panic!("expected `may have started`, got `not started`: {cause:?}")
            }
        }
    }

    /// The error and fate of a failure that answered that the program did not start and that the
    /// spawn dealt with a child it held; panics otherwise.
    #[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:?}"),
        }
    }

    /// The error of a failure that answered that the program did not start; panics otherwise.
    #[cfg_attr(
        not(target_os = "linux"),
        allow(dead_code, reason = "only the Linux spawn'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::NotStartedWithChild { cause, .. } => cause,
            SpawnFailure::MayHaveStarted { cause, fate, .. } => {
                panic!("expected `not started`, got `may have started` ({fate:?}): {cause:?}")
            }
        }
    }
}

/// The cause and fate inside an error a public spawn returned, which must say the program may have
/// started.
#[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)
}

/// The cause, fate and handed-back child (if any) inside an error a public spawn returned, which
/// must say the program may have started. For `test_child`, which wraps the child in its
/// `HandedBack`.
#[cfg(test)]
#[track_caller]
pub(crate) fn error_parts(error: Error) -> (Error, ChildFate, Option<Unreaped>) {
    match error {
        Error::MayHaveStarted {
            fate, source, unreaped, ..
        } => (
            std::sync::Arc::try_unwrap(source).expect("a spawn's cause has one owner"),
            fate,
            unreaped.map(|unreaped| *unreaped),
        ),
        other => panic!("expected Error::MayHaveStarted, got {other:?}"),
    }
}

/// Whether `error` is an `Unsupported` refusal.
#[cfg(all(test, windows))]
pub(crate) fn unsupported(error: &Error) -> bool {
    matches!(error, Error::Unsupported { .. })
}

/// Whether `error` is an `Io(InvalidInput)` refusal.
#[cfg(all(test, windows))]
pub(crate) fn invalid_input(error: &Error) -> bool {
    matches!(error, Error::Io(e) if e.kind() == std::io::ErrorKind::InvalidInput)
}

/// An error a public spawn returned, which must say the program did not start.
#[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`: {}",
                    crate::error::Report(&error)
                );
                error
            }
            SpawnFailure::NotStartedWithChild { cause: error, .. } => {
                debug_assert!(
                    !matches!(error, Error::MayHaveStarted { .. }),
                    "a spawn answered both `not started` and `may have started`: {}",
                    crate::error::Report(&error)
                );
                error
            }
            SpawnFailure::MayHaveStarted {
                cause,
                fate,
                wrapper_elevated,
                detail,
                unreaped,
                cleanup,
            } => {
                debug_assert!(
                    !matches!(*cause, Error::MayHaveStarted { .. }),
                    "a `may have started` failure's cause is itself `may have started`: {}",
                    crate::error::Report(&*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,
                    detail,
                    source: std::sync::Arc::new(*cause),
                    unreaped,
                    cleanup,
                }
            }
        }
    }
}

/// The plain errors a spawn's failure points produce. Sealed: a [`SpawnFailure`] is not one, so an
/// answered failure cannot be answered again.
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 {}
}

/// Answers, for a failure point in a spawn, that its error leaves the program not started. A failure
/// that may have started is built with [`SpawnFailure::started`], which takes the fate of the child.
pub(crate) trait Classify<T> {
    /// This failure happens before the program can start.
    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()))
    }
}

/// Test seams that fail a run-to-completion helper (`output`, `status`, `read`) after its spawn
/// succeeded.
#[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) };
    }

    /// The message of the error the seams fail with.
    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)
    }

    /// The next `Child::wait` on this thread fails, before it waits.
    pub(crate) fn fail_the_next_wait() -> Armed {
        arm(&WAIT)
    }

    /// The next `Child::communicate` on this thread fails, before it pumps.
    pub(crate) fn fail_the_next_pump() -> Armed {
        arm(&PUMP)
    }

    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)
    }
}

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