cosca 0.7.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#[cfg(target_os = "linux")]
use super::TornDown;
use super::{refused_kill_answer, refused_kill_warning, Forgetting, Refused};
#[cfg(any(target_os = "linux", windows))]
use crate::child::owned::owned_tests::teardown_lines;
use crate::child::owned::owned_tests::teardown_lines::PID;
#[cfg(target_os = "linux")]
use crate::child::owned::teardown::{Disposition, Expect, HandleTeardown, Kill, Past, TeardownOutcome};
#[cfg(target_os = "linux")]
use crate::child::owned::OwnedChild;
use crate::error::{ChildFate, Error};
use crate::identity::ProcessId;
#[cfg(target_os = "linux")]
use crate::os_process::child::{ExitRecord, KillRefusal};
#[cfg(target_os = "linux")]
use crate::os_process::fake::{FakeProcess, ReapAnswer};
#[cfg(target_os = "linux")]
use crate::signal::Sent;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use crate::test_child::Console;
#[cfg(target_os = "linux")]
use crate::test_groups::{tracer_group, Group};

/// The second teardown of a refused child, as `OwnedChild` runs it for a caller that leaves the child
/// (`tear_down_pidfd`'s), over a child 7 whose kill answers `kill` and whose wait or look answers
/// `reap`, for a spawn that read the identity `id`. Its outcome is the one the refused-kill warn is
/// built from.
#[cfg(target_os = "linux")]
fn second_teardown(kill: Result<Sent, std::io::Error>, reap: ReapAnswer, id: Option<ProcessId>) -> TeardownOutcome {
    let fake = FakeProcess::new([]).with_pid(7).reaps([reap]).signals([kill]);
    OwnedChild::new(Box::new(fake)).tear_down(
        id,
        Kill {
            disposition: Disposition::Leave,
            tree: None,
        },
        None,
    )
}

#[cfg(target_os = "linux")]
fn torn_down(outcome: TeardownOutcome) -> Refused {
    Refused::TornDown(TornDown {
        torn: HandleTeardown::new(
            outcome,
            Expect {
                refusal: KillRefusal::Permission,
                past: Past::Clean,
            },
        ),
        leak: Some("a pidfd"),
        why: None,
    })
}

/// What a refused teardown kill answers: the second teardown's fate, or the forgetting's.
///
/// Mutant: one state answers another's fate; a child left running answers without its identity.
#[skuld::test]
fn a_refused_kill_answers_from_what_became_of_the_child() {
    let id = Some(ProcessId::current());
    #[cfg(target_os = "linux")]
    {
        let exit = || ReapAnswer::Reaped(ExitRecord::Status(Default::default()));
        let failed = || ReapAnswer::Err(std::io::ErrorKind::Other);
        assert_eq!(
            refused_kill_answer(&torn_down(second_teardown(Ok(Sent::Delivered), exit(), id)), id),
            ChildFate::Reaped
        );
        assert_eq!(
            refused_kill_answer(&torn_down(second_teardown(Ok(Sent::Delivered), failed(), id)), id),
            ChildFate::Killed
        );
        // Refused again, and still running at the look: left, with the identity the spawn read.
        let refused = || Err(std::io::Error::from(std::io::ErrorKind::PermissionDenied));
        assert_eq!(
            refused_kill_answer(&torn_down(second_teardown(refused(), ReapAnswer::Running, id)), id),
            ChildFate::Running { id }
        );
    }
    let forgotten = Refused::Forgotten(Forgetting {
        leak: "a zombie",
        why: String::new(),
        fate: ChildFate::Gone,
    });
    assert_eq!(refused_kill_answer(&forgotten, id), ChildFate::Gone);
}

/// A child whose handle was released, with nothing else done to it, may still run: the answer is
/// `Running` with the identity the spawn had read.
///
/// Mutant: the release drops the identity or answers `Unknown`.
#[cfg(not(target_os = "linux"))]
#[skuld::test]
fn a_released_child_answers_running_with_its_identity() {
    let id = Some(ProcessId::current());
    assert_eq!(refused_kill_answer(&Refused::Released, id), ChildFate::Running { id });
    assert_eq!(
        refused_kill_answer(&Refused::Released, None),
        ChildFate::Running { id: None }
    );
}

/// macOS: a child released to tokio's orphan queue is worded as the late teardown words it, from the
/// one text.
///
/// Mutant: the release is worded apart from `HANDED_TO_ORPHAN_REAPER`, or not at all.
#[cfg(target_os = "macos")]
#[skuld::test]
fn a_released_child_is_worded_as_handed_to_tokios_orphan_reaper() {
    let e = Error::Io(std::io::Error::from(std::io::ErrorKind::PermissionDenied));
    assert_eq!(
        refused_kill_warning(7, &e, &Refused::Released),
        format!(
            "teardown kill of child 7 failed ({e}); it is {}",
            super::HANDED_TO_ORPHAN_REAPER
        )
    );
}

/// Windows: there is no orphan queue and nothing waits for the released child, and the warn says
/// that in the words of a child a caller leaves, not that anything will reap it.
///
/// Mutant: the release is worded as handed to an orphan reaper, or apart from the table's `LEFT`.
#[cfg(windows)]
#[skuld::test]
fn a_released_child_is_worded_as_a_child_left() {
    let e = Error::Io(std::io::Error::from(std::io::ErrorKind::PermissionDenied));
    let text = refused_kill_warning(7, &e, &Refused::Released);
    assert_eq!(
        text,
        format!("teardown kill of child 7 failed ({e}){}", teardown_lines::LEFT)
    );
    assert!(!text.contains("orphan"), "{text}");
}

/// A refused kill of a backend already forgotten as not shown to be ours still holds the child's
/// pidfd, so Linux tears the child down through it, as for one it forgets now: killed, reaped, and
/// said in the one warn, which leaks nothing of its own. The child is a real one, ours.
///
/// Mutant: the case answers without a teardown, leaving the child running and its pidfd closed
/// unused; or panics.
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_refused_kill_of_a_backend_already_forgotten_is_torn_down_through_its_pidfd() {
    let mut child =
        crate::test_spawn::spawn(&mut crate::test_child::held_std_killable_blocker()).expect("spawn the child");
    let guard = crate::containment::cgroup::test_support::KillOnDrop::of(&child);
    let pid = child.id();
    let pidfd = rustix::process::pidfd_open(
        rustix::process::Pid::from_raw(pid as i32).expect("a positive pid"),
        rustix::process::PidfdFlags::empty(),
    )
    .expect("pidfd_open");
    let proc = super::ProcSource::Foreign {
        fate: super::Forgotten::Unproven,
        pid,
        pidfd: super::PinnedPidfd(Some(pidfd)),
    };
    crate::log_capture::install();
    let mark = crate::log_capture::mark();
    let reaps = crate::child::spawn::fault::record_teardown_reaps();
    let e = Error::Io(std::io::Error::from(std::io::ErrorKind::PermissionDenied));
    let fate = proc.after_refused_kill(
        pid,
        Some(ProcessId::current()),
        &e,
        &mut crate::child::spawn::Diagnosis::default(),
    );
    assert_eq!(fate, ChildFate::Reaped);
    reaps.assert_killed();
    let already_reaped = child
        .wait()
        .expect_err("the teardown reaped the child through its pidfd");
    assert_eq!(already_reaped.raw_os_error(), Some(libc::ECHILD));
    // Reaped through the pidfd: the guard's pidfd would refuse a kill of it.
    _ = guard.defuse();
    let warns = crate::log_capture::warns_since(mark, "");
    assert_eq!(
        warns,
        vec![format!(
            "teardown after a failed spawn: teardown kill of child {pid} failed ({e}); tokio's handle for it was \
             already forgotten; teardown of child {pid}: reaped it after the kill"
        )]
    );
}

/// The warn for a refused teardown kill carries every row's clause of the table, whatever its level.
///
/// Mutant: the warn words the fate or `uncollected` itself, beside or instead of the clause, or
/// leaves out a clause below warn.
#[skuld::test]
fn the_refused_kill_warning_carries_every_clause_of_the_table() {
    let e = Error::Io(std::io::Error::from(std::io::ErrorKind::PermissionDenied));
    // The warn up to what the outcome adds.
    #[cfg(target_os = "linux")]
    {
        let bare = format!("teardown kill of child {PID} failed ({e}); forgetting tokio's handle for it leaks a pidfd");
        for row in teardown_lines::rows() {
            let outcome = teardown_lines::outcome(&row, Disposition::Leave);
            let text = refused_kill_warning(PID, &e, &torn_down(outcome));
            assert_eq!(text, format!("{bare}; {}", row.leave), "{:?} {:?}", row.kill, row.found);
        }
    }

    // No second teardown ran: the child was forgotten.
    let forgotten = Refused::Forgotten(Forgetting {
        leak: "a zombie",
        why: "was reaped by someone else".to_owned(),
        fate: ChildFate::Gone,
    });
    let text = refused_kill_warning(PID, &e, &forgotten);
    assert_eq!(
        text,
        format!(
            "teardown kill of child {PID} failed ({e}); the child was reaped by someone else; forgetting tokio's \
             handle for it leaks a zombie"
        )
    );
}

/// `exit_visible_by` waits, so it refuses to run inside an async drop's bounded section, before it
/// looks at the backend.
///
/// Mutant: drop the assert from `exit_visible_by`.
#[cfg(unix)]
#[skuld::test]
#[should_panic(expected = "would block inside an async Drop")]
fn exit_visible_by_refuses_inside_a_section() {
    let proc = super::ProcSource::Foreign {
        fate: super::Forgotten::Unproven,
        pid: 0,
        // Never read: the refusal comes before the backend is looked at.
        #[cfg(target_os = "linux")]
        pidfd: super::PinnedPidfd(Some(std::fs::File::open("/dev/null").expect("open /dev/null").into())),
    };
    let _section = crate::bounded::Section::enter();
    proc.exit_visible_by(1, std::time::Instant::now());
}

/// `ECHILD` from the reap right after a look that showed the child's exit means something else
/// reaped it in between: the awaited reap says so, not that it cannot show the child is ours. The
/// child is killed (by the test's own kill: the signal routes are not the subject) and reaped behind
/// tokio's back, and the look is made to show the exit.
///
/// Mutant: a failed `try_wait` is `Waited::Unverified` whatever its errno.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn an_echild_after_a_look_that_showed_the_exit_is_a_foreign_reap() {
    use std::os::unix::process::ExitStatusExt as _;

    use crate::os_process::child::{ExitRecord, PeekOutcome};
    use crate::wait::exit_only::seams::force_peeks;

    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
    cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
    let mut child = cmd.spawn().expect("spawn");
    let pid = child.pid();
    let proc = child.proc_mut();
    crate::test_child::reaped_elsewhere::kill_own_child(pid);
    let raw = rustix::process::Pid::from_raw(pid as i32).expect("a positive pid");
    rustix::process::waitid(
        rustix::process::WaitId::Pid(raw),
        rustix::process::WaitIdOptions::EXITED,
    )
    .expect("reap behind tokio's back")
    .expect("an exit record");
    let _shows_exit = force_peeks([Ok(PeekOutcome::Exit(ExitRecord::Status(
        std::process::ExitStatus::from_raw(9),
    )))]);

    let (waited, cause) = proc.wait_and_reap_awaiting().await.expect("the exit can be watched");

    assert_eq!(waited, super::Waited::Foreign, "{cause:?}");
    drop(child);
}

/// The async reap's twin of `child::owned_tests::macos::a_wait_on_a_child_whose_tracer_dies_during_its_exit_is_woken_by_the_tracers_exit`:
/// XNU sends a traced child's `NOTE_EXIT` and `SIGCHLD` to its tracer alone, and a tracer that dies
/// while the child is exiting leaves it to launchd. The reap watches the tracer too, and its
/// `NOTE_EXIT` makes it look again, which finds launchd holding the child: `Unpinned`. This
/// process's own pid stands in for the child (`waitid` answers `ECHILD`, and its id resolves), and a
/// blocker for the tracer; the parent read is forced: the stand-in while it runs, launchd once it
/// has exited. The stand-in is this test's child, so its `SIGCHLD` wakes the reap too, and the
/// assertion is on the tracer's `NOTE_EXIT` among the reap's events.
///
/// Mutant: the async reap does not watch the tracer.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn an_async_reap_of_a_child_whose_tracer_dies_during_its_exit_is_woken_by_the_tracers_exit() {
    use std::cell::Cell;
    use std::rc::Rc;

    use crate::identity::{ppid_fault, uniq_info, ReadPurpose, UniqRead, LAUNCHD};
    use crate::os_process::child::PeekOutcome;
    use crate::os_process::macos::kqueue::test_hooks;
    use crate::signal::RootState;

    let pid = std::process::id();
    let UniqRead::Found(info) = uniq_info(pid, ReadPurpose::Adopt) else {
        panic!("this process's unique id must read");
    };
    let mut cmd = crate::test_child::held_std_blocker(std::process::Stdio::null());
    let mut tracer = crate::test_spawn::spawn(&mut cmd).expect("spawn the stand-in tracer");
    let tracer_stdin = tracer.stdin.take().expect("piped stdin");
    let tracer_pid = tracer.id();
    let exited = Rc::new(Cell::new(false));
    let _parent = ppid_fault::force_ppid_with({
        let exited = Rc::clone(&exited);
        move || Ok(if exited.get() { LAUNCHD } else { tracer_pid })
    });
    let _end = crate::tokio::child::fault::set_reap_parked(move || {
        drop(tracer_stdin);
        crate::test_child::wait_until_zombie(tracer_pid);
        exited.set(true);
    });
    let _ = test_hooks::take_events();

    let looked = super::look_on_events(pid, info.unique_id, |looked| match looked {
        Ok(PeekOutcome::Running) => Ok(None::<()>),
        other => Err(super::Halt::State(RootState::of_peek(other))),
    })
    .await;

    let events = test_hooks::take_events();
    assert!(
        events
            .iter()
            .any(|&(filter, fflags)| filter == libc::EVFILT_PROC && fflags & libc::NOTE_EXIT != 0),
        "the tracer's exit must wake the reap: {events:?}"
    );
    assert!(
        matches!(looked, Err(super::Halt::State(RootState::Unpinned))),
        "launchd holds the child once its tracer is gone"
    );
    tracer.wait().expect("reap the stand-in tracer");
}

/// This process stands in for a traced child, as above, with a live blocker its parent read is forced
/// to name as its tracer: the child's pid and unique id, the blocker, and the forced read's guard.
#[cfg(target_os = "macos")]
fn stand_in_traced_by_a_blocker() -> (u32, u64, std::process::Child, crate::identity::ppid_fault::ForcedReads) {
    use crate::identity::{ppid_fault, uniq_info, ReadPurpose, UniqRead};

    let pid = std::process::id();
    let UniqRead::Found(info) = uniq_info(pid, ReadPurpose::Adopt) else {
        panic!("this process's unique id must read");
    };
    let mut cmd = crate::test_child::held_std_blocker(std::process::Stdio::null());
    let tracer = crate::test_spawn::spawn(&mut cmd).expect("spawn the stand-in tracer");
    let tracer_pid = tracer.id();
    let parent = ppid_fault::force_ppid_with(move || Ok(tracer_pid));
    (pid, info.unique_id, tracer, parent)
}

/// Ends and reaps a stand-in tracer.
#[cfg(target_os = "macos")]
fn end_stand_in(mut tracer: std::process::Child) {
    drop(tracer.stdin.take());
    tracer.wait().expect("reap the stand-in tracer");
}

/// A look that finds the child running asks for another; any other look ends the reap.
#[cfg(target_os = "macos")]
fn still_running(looked: std::io::Result<crate::os_process::child::PeekOutcome>) -> Result<Option<()>, super::Halt> {
    match looked {
        Ok(crate::os_process::child::PeekOutcome::Running) => Ok(None),
        other => Err(super::Halt::State(crate::signal::RootState::of_peek(other))),
    }
}

/// A refused registration on the tracer leaves the child unwatched, not unknown: the reap halts so
/// that its caller waits in place (principle 2), rather than forgetting a child that is ours.
///
/// Mutant: every walk error is `Halt::State(Unknown)`.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn an_async_reap_whose_tracer_registration_is_refused_cannot_watch() {
    use crate::identity::Link;
    use crate::wait::macos::tracer_seams::{force_links, refuse_next_registration};

    let (pid, unique, tracer, _parent) = stand_in_traced_by_a_blocker();
    let _links = force_links([Link::Found {
        unique: 1,
        parent: std::process::id(),
        at_attach: 0,
    }]);
    let _refused = refuse_next_registration(libc::EINVAL);

    let looked = super::look_on_events(pid, unique, still_running).await;

    assert!(matches!(looked, Err(super::Halt::Watch(_))), "{looked:?}");
    end_stand_in(tracer);
}

/// A tracer whose identity cannot be read leaves the child's state unknown, as a refused peek does.
///
/// Mutant: every walk error is `Halt::Watch`.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn an_async_reap_whose_tracer_cannot_be_read_finds_the_child_unknown() {
    use crate::identity::Link;
    use crate::signal::RootState;
    use crate::wait::macos::tracer_seams::force_links;

    let (pid, unique, tracer, _parent) = stand_in_traced_by_a_blocker();
    let _links = force_links([Link::Refused(libc::EPERM)]);

    let looked = super::look_on_events(pid, unique, still_running).await;

    assert!(
        matches!(looked, Err(super::Halt::State(RootState::Unknown(_)))),
        "{looked:?}"
    );
    end_stand_in(tracer);
}

/// A tracer that is gone sends this process no event, so only the re-check timer wakes the reap to
/// look again. The walks find the tracer gone twice: at once, then after 1 ms, which the reap's
/// timer must wait for. The third look ends the reap. A backstop polled only while the reap waits,
/// by which time it has armed what it waits on, ends the test if no timer was armed.
///
/// Mutant: `KqueueWatch::next` ignores the tracer watch's re-check.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn an_async_reap_whose_tracer_is_gone_looks_again_when_the_recheck_timer_fires() {
    use crate::identity::Link;
    use crate::os_process::child::PeekOutcome;
    use crate::os_process::macos::kqueue::test_hooks;
    use crate::wait::macos::tracer_seams::force_links;

    let (pid, unique, tracer, _parent) = stand_in_traced_by_a_blocker();
    let _links = force_links([Link::Gone, Link::Gone]);
    let _ = test_hooks::take_backoff_timers();
    let mut looks = 0;
    let look = super::look_on_events(pid, unique, |looked| match looked {
        Ok(PeekOutcome::Running) => {
            looks += 1;
            Ok((looks == 3).then_some(()))
        }
        other => Err(super::Halt::State(crate::signal::RootState::of_peek(other))),
    });
    let armed = std::cell::RefCell::new(Vec::new());
    let backstop = async {
        *armed.borrow_mut() = test_hooks::take_backoff_timers();
        if armed.borrow().is_empty() {
            return;
        }
        std::future::pending::<()>().await;
    };

    let looked = ::tokio::select! {
        biased;
        looked = look => Some(looked),
        () = backstop => None,
    };

    let looked = looked.expect("the walk asked for a re-check, but the reap armed no timer to wake it");
    assert!(looked.is_ok(), "{looked:?}");
    assert_eq!(
        *armed.borrow(),
        [std::time::Duration::from_millis(1)],
        "the timer waits the re-check's 1 ms"
    );
    end_stand_in(tracer);
}

/// `held_os_process` reads a live child as a process whose look says running, and a child tokio has
/// reaped as nothing left to watch.
///
/// Mutant: a reaped tokio child still yields a process; a live child yields `None` (exited).
#[skuld::test]
async fn the_held_handle_reads_as_a_running_process_until_tokio_reaps_the_child() {
    use crate::os_process::Exit;

    let (mut child, _stdin) = crate::test_child::held_contained_blocker_on_console_async(crate::Stdio::pipe());

    let held = child
        .proc()
        .held_os_process()
        .expect("a live child's handle can be read")
        .expect("a live child has a process to watch");
    assert!(
        matches!(held.exited().expect("a look"), Exit::Running(_)),
        "a live child looks running"
    );
    drop(held);

    child.kill_tree().expect("kill the blocker");
    child.wait().await.expect("reap the blocker");
    let reaped = child
        .proc()
        .held_os_process()
        .expect("a reaped child's handle can be read");
    assert!(reaped.is_none(), "a reaped child has nothing left to watch");
}

/// The look at a child on macOS requires the unique id read at spawn after the watch is armed: a pid
/// another process holds by then is exited, not watched as the child, and a refused read cannot be
/// assessed (`Unassessable`, with the errno).
///
/// Mutant: the look never reads the unique id; a mismatch is `Some`; a refused read is `None`.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn the_held_handle_look_checks_the_unique_id_after_arming_on_macos() {
    use crate::child::spawn::identity_macos_tests::other_unique_id;
    use crate::identity::uniq_fault::force_uniq_read_once;
    use crate::identity::{ReadPurpose, UniqRead};

    let (mut child, _stdin) = crate::test_child::held_contained_blocker_on_console_async(crate::Stdio::pipe());
    let pid = child.pid();

    {
        let _other = force_uniq_read_once(ReadPurpose::Open, other_unique_id(pid));
        let held = child.proc().held_os_process().expect("a mismatch is not an error");
        assert!(held.is_none(), "a pid another process holds is exited, not watched");
    }
    {
        let _refused = force_uniq_read_once(ReadPurpose::Open, UniqRead::Refused(libc::EPERM));
        let Err(Error::Unassessable {
            detail,
            source: Some(source),
        }) = child.proc().held_os_process()
        else {
            panic!("a refused read is Unassessable with its errno");
        };
        assert_eq!(source.raw_os_error(), Some(libc::EPERM));
        assert!(
            detail.contains("the graceful signal was sent") && !detail.contains("no signal was sent"),
            "the look follows a sent SIGTERM: {detail}"
        );
    }
    assert!(
        child.proc().held_os_process().expect("a matching read").is_some(),
        "the child's own unique id is watched"
    );

    child.kill_tree().expect("kill the blocker");
    child.wait().await.expect("reap the blocker");
}

/// A macOS child forgotten as not shown to be ours is sent no `SIGTERM` (`Sent::Unverified`) and its
/// look is `Unassessable`, so a graceful shutdown escalates at once.
///
/// Mutant: the unproven child is sent the signal by number, or its look watches the number.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn an_unproven_child_is_sent_no_sigterm_and_cannot_be_looked_at_on_macos() {
    use crate::signal::{Sent, Sig};

    let (stdin, writer) = crate::test_child::held_writer_stdin();
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
    cmd.stdin(stdin).expect("set stdin");
    let mut child = cmd.spawn().expect("spawn");
    let pid = child.pid();
    child
        .proc_mut()
        .forget_as("was forgotten by a test", super::Forgotten::Unproven);

    assert_eq!(child.proc().signal(Sig::Term).expect("no error"), Sent::Unverified);
    assert!(
        matches!(child.proc().held_os_process(), Err(Error::Unassessable { .. })),
        "nothing proves the number is still the child's"
    );
    // Forgotten, the child is out of tokio's reach (a kill by number is refused, a wait is `ECHILD`):
    // end and reap the blocker by pid.
    drop(writer);
    crate::test_child::reaped_elsewhere::kill_own_child(pid);
    crate::test_child::reaped_elsewhere::reap_behind(pid);
}

/// `graceful_shutdown` of a macOS child forgotten as not shown to be ours sends it nothing, neither
/// the `SIGTERM` nor the kill, and returns `ECHILD` at once, with no grace waited.
///
/// Mutants: the child is sent a signal by number; the call waits out the grace or kills.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn graceful_shutdown_of_an_unproven_child_sends_nothing_and_is_echild_on_macos() {
    use crate::os_process::child::PeekOutcome;
    use crate::wait::exit_only::{peek, Target};

    let (stdin, writer) = crate::test_child::held_writer_stdin();
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
    cmd.stdin(stdin).expect("set stdin");
    let mut child = cmd.spawn().expect("spawn");
    let pid = child.pid();
    child
        .proc_mut()
        .forget_as("was forgotten by a test", super::Forgotten::Unproven);

    let outcome = child.graceful_shutdown(std::time::Duration::from_secs(3600)).await;
    let peeked = peek(&Target::pid(pid, None));

    // Forgotten, the child is out of tokio's reach: end and reap the blocker by pid, whatever the
    // assertions below say.
    drop(writer);
    crate::test_child::reaped_elsewhere::kill_own_child(pid);
    crate::test_child::reaped_elsewhere::reap_behind(pid);
    assert!(
        matches!(&outcome, Err(Error::Io(e)) if e.raw_os_error() == Some(libc::ECHILD)),
        "{outcome:?}"
    );
    assert!(
        matches!(peeked, Ok(PeekOutcome::Running)),
        "nothing was sent, so the child still runs: {peeked:?}"
    );
}

/// A zombie a foreign tracer holds has exited, so the child reads `Dead` from its pidfd's edge,
/// though the parent's peek finds no record to show for it. The `SIGKILL` makes the exit: left to
/// exit by itself, the tracee stops at a signal-delivery stop (`SIGPIPE` on its closed stdout) and
/// waits for the tracer, so it never exits.
///
/// Mutant: liveness answered from the peek, which reads the held zombie as `Running`.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_zombie_a_foreign_tracer_holds_is_dead(#[fixture(tracer_group)] _group: &Group) {
    use crate::identity::Liveness;
    use crate::test_support::foreign_tracer::{self, REAP};

    let (child, stdin) = foreign_tracer::spawn_tokio_tracee().await;
    let mut tracer = foreign_tracer::spawn_helper(child.pid());
    assert_eq!(child.is_alive(), Liveness::Alive, "a running child");

    crate::test_child::reaped_elsewhere::kill_own_child(child.pid());
    let pidfd = child.proc().pidfd().expect("a Linux backend has its pidfd");
    let mut fds = [rustix::event::PollFd::new(&pidfd, rustix::event::PollFlags::IN)];
    crate::wait::poll_ready(&mut fds).expect("poll the pidfd");
    let peeked_running = child.proc().is_running().expect("the peek answers");
    let held = child.is_alive();

    // Asserted only after the tracer lets go: the child's drop waits for a zombie the tracer holds.
    tracer.send(REAP);
    tracer.expect("reaped");
    let handed_back = child.is_alive();
    drop(child);
    drop(stdin);
    tracer.finish();
    assert!(peeked_running, "the tracer holds it, so the peek finds no record");
    assert_eq!(held, Liveness::Dead);
    assert_eq!(handed_back, Liveness::Dead, "and once handed back");
}