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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! `Child::drop`'s signals and its bounded arms: everything but the armed drop's reap and drain
//! (`armed_drop_tests.rs`). A real delegated cgroup needs root, so these use
//! `CgroupLeaf::for_test_at` (real errnos on any Linux host) and `FakeLeaf` (answers `rmdir` as
//! cgroupfs does).

#[cfg(unix)]
use crate::child::spawn::fault::{release_between_kill_and_wait, TeardownBlocker};
use crate::test_child::Console;
#[cfg(target_os = "linux")]
use crate::test_groups::{cgroup, Group};
use crate::tokio::child::{fault, Child};

/// A real root that only a kill or a closed stdin ends, in no containment. The write end of its
/// stdin is returned: dropping it lets the root exit. On Windows the root is `more.com`, which
/// exits `0` on EOF, where the `findstr x` of `BLOCKER_ARGV` exits `1`, the code a kill gives.
fn blocker() -> (Child, crate::tokio::ChildStdin) {
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    #[cfg(unix)]
    cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
    #[cfg(windows)]
    cmd.args([crate::test_child::windows_more()]);
    cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
    cmd.stdout(crate::Stdio::null()).expect("set stdout");
    let mut child = cmd.spawn().expect("spawn");
    let stdin = child.stdin().expect("piped stdin");
    (child, stdin)
}

/// Releases the root's stdin once the drop's kill returned; `assert_killed` fails if the root exited
/// `0`. Keep the guard through the assertion.
#[cfg(unix)]
pub(crate) fn released_blocker() -> (Child, TeardownBlocker) {
    let (child, stdin) = blocker();
    (child, release_between_kill_and_wait(stdin))
}

/// What [`released_blocker`] gives on Windows, where the drop waits for nothing and so has no point
/// to release at. It holds the root's stdin and a handle of its own to the root, opened before the
/// drop. [`assert_killed`](Self::assert_killed) releases the stdin after the drop returned, then
/// reads the exit code: `TerminateProcess(_, 1)` set `1` before the drop returned, so a root the
/// drop left alone exits `0`.
#[cfg(windows)]
pub(crate) struct TerminatedBlocker {
    stdin: std::cell::Cell<Option<crate::tokio::ChildStdin>>,
    process: std::os::windows::io::OwnedHandle,
}

#[cfg(windows)]
impl TerminatedBlocker {
    /// The root was ended by the drop's `TerminateProcess` (exit code 1), not by its own exit.
    pub(crate) fn assert_killed(&self) {
        use std::os::windows::io::AsRawHandle as _;
        use windows::Win32::Foundation::{HANDLE, WAIT_OBJECT_0};
        use windows::Win32::System::Threading::{GetExitCodeProcess, WaitForSingleObject, INFINITE};

        drop(self.stdin.take());
        let process = HANDLE(self.process.as_raw_handle());
        // SAFETY: `process` is a live handle this struct owns, opened with `SYNCHRONIZE`.
        let waited = unsafe { WaitForSingleObject(process, INFINITE) };
        assert_eq!(waited, WAIT_OBJECT_0, "wait for the root's exit");
        let mut code = 0u32;
        // SAFETY: as above; the handle was opened with `PROCESS_QUERY_LIMITED_INFORMATION`.
        unsafe { GetExitCodeProcess(process, &mut code) }.expect("read the root's exit code");
        assert_eq!(
            code, 1,
            "the drop must terminate the root (`TerminateProcess` exits 1), not leave it to exit on its own (0)"
        );
    }
}

#[cfg(windows)]
pub(crate) fn released_blocker() -> (Child, TerminatedBlocker) {
    use std::os::windows::io::{FromRawHandle as _, OwnedHandle};
    use windows::Win32::System::Threading::{OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_SYNCHRONIZE};

    let (child, stdin) = blocker();
    // SAFETY: opens the live root by its pid; the handle then pins that process.
    let process = unsafe {
        OpenProcess(
            PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION,
            false,
            child.pid(),
        )
    }
    .expect("open the root");
    // SAFETY: `OpenProcess` returned a handle nothing else owns.
    let process = unsafe { OwnedHandle::from_raw_handle(process.0) };
    (
        child,
        TerminatedBlocker {
            stdin: std::cell::Cell::new(Some(stdin)),
            process,
        },
    )
}

#[cfg(target_os = "linux")]
use crate::containment::cgroup::test_support::alone;
#[cfg(target_os = "linux")]
use crate::test_child::fixture_path;

#[cfg(target_os = "linux")]
pub(crate) mod linux {
    use std::os::fd::{AsFd, OwnedFd};

    use super::Child;
    use crate::containment::cgroup::fault as leaf_fault;
    use crate::containment::cgroup::test_support::{entered_leaf_at, FakeLeaf};
    use crate::containment::{Attached, Containment};

    /// `child`, contained by `leaf` instead of what it spawned with.
    pub(in crate::tokio::child) fn contained_by(
        mut child: Child,
        leaf: crate::containment::cgroup::CgroupLeaf,
    ) -> Child {
        child.os.attached = Attached::Cgroup(leaf);
        child.containment = Containment::CgroupV2;
        child
    }

    /// A `FakeLeaf` whose `rmdir` answers as cgroupfs does, and an entered leaf on it.
    pub(in crate::tokio::child) fn fake_leaf(
        name: &str,
        populated: bool,
    ) -> (FakeLeaf, crate::containment::cgroup::CgroupLeaf) {
        crate::log_capture::install();
        let fake = FakeLeaf::new(name, populated);
        let (path, events) = (fake.leaf.clone(), fake.events.clone());
        leaf_fault::set_rmdir_hook(move |_| FakeLeaf::rmdir(&path, &events));
        let leaf = entered_leaf_at(fake.leaf.clone());
        (fake, leaf)
    }

    /// How a root ended.
    #[derive(Debug, PartialEq, Eq)]
    pub(crate) enum Ended {
        Exited(i32),
        Signalled(i32),
        /// Reaped before its stdin closed, by tokio's own `Child` drop (its `try_wait` ran after
        /// something had already ended the root), so no status is left to read. It proves only
        /// that the root died with its stdin open. For [`ignoring_blocker`](super::ignoring_blocker),
        /// which ignores every signal but the ones it lists there, that leaves `SIGKILL`, one of
        /// those exceptions, a crash or a foreign kill: never another signal from the drop.
        ReapedWhileStdinOpen,
    }

    /// The root's exit, read through a pidfd opened while the caller still holds the unreaped
    /// child, so it names that process exactly. It never consumes the exit: the root's number
    /// belongs to tokio's reaper once the child is dropped, and a skuld test runtime that
    /// this thread never yields to has not run it. That holds only in a process running this one
    /// test (tokio's orphan queue is process-global, and any runtime that parks drains it), so
    /// [`ended_after_closing`](Self::ended_after_closing) requires `alone()`.
    pub(crate) struct Pidfd(OwnedFd);

    impl Pidfd {
        pub(crate) fn of(child: &Child) -> Pidfd {
            let pid = rustix::process::Pid::from_raw(child.pid() as i32).expect("a positive pid");
            Pidfd(rustix::process::pidfd_open(pid, rustix::process::PidfdFlags::empty()).expect("pidfd_open"))
        }

        /// Whether the root has been reaped, by anyone: `waitid` on the pidfd no longer finds a
        /// child. Consumes nothing, and a zombie counts as not reaped.
        pub(super) fn reaped(&self) -> bool {
            loop {
                match rustix::process::waitid(
                    rustix::process::WaitId::PidFd(self.0.as_fd()),
                    rustix::process::WaitIdOptions::EXITED
                        | rustix::process::WaitIdOptions::NOHANG
                        | rustix::process::WaitIdOptions::NOWAIT,
                ) {
                    Ok(_) => return false,
                    Err(rustix::io::Errno::CHILD) => return true,
                    Err(rustix::io::Errno::INTR) => {}
                    Err(e) => panic!("waitid on the root's pidfd: {e}"),
                }
            }
        }

        /// Drop `release` (the root's stdin writer), so a root nothing signalled exits on its own, wait for its
        /// exit, and say how it ended. A root a drop killed died of `SIGKILL` before its stdin
        /// closed; one nothing killed exits `0`. The drop's own `try_wait` may reap a root that
        /// something already ended, which leaves no status to read: that is
        /// [`Ended::ReapedWhileStdinOpen`].
        /// The read is exact only under `alone()`: nothing else reaps before this thread yields to
        /// a runtime.
        pub(crate) fn ended_after_closing(&self, release: impl Sized) -> Ended {
            debug_assert!(
                std::env::var_os("COSCA_TEST_ALONE").is_some(),
                "`ended_after_closing` is exact only in a process running this test alone (`alone()`): \
                 tokio's orphan queue is process-global"
            );
            let reaped_while_open = self.reaped();
            drop(release);
            if reaped_while_open {
                return Ended::ReapedWhileStdinOpen;
            }
            loop {
                let status = rustix::process::waitid(
                    rustix::process::WaitId::PidFd(self.0.as_fd()),
                    rustix::process::WaitIdOptions::EXITED | rustix::process::WaitIdOptions::NOWAIT,
                );
                match status {
                    Ok(Some(status)) => {
                        return match status.terminating_signal() {
                            Some(signal) => Ended::Signalled(signal),
                            None => Ended::Exited(status.exit_status().expect("exited")),
                        }
                    }
                    Ok(None) => unreachable!("a blocking waitid returns a status"),
                    Err(rustix::io::Errno::INTR) => {}
                    Err(e) => panic!("waitid on the root's pidfd: {e}"),
                }
            }
        }
    }

    pub(super) fn kill_file(fake: &FakeLeaf) -> Vec<u8> {
        std::fs::read(fake.leaf.join("cgroup.kill")).expect("read cgroup.kill")
    }

    /// The steps `drop` took and the levels of the records naming `needle`.
    pub(in crate::tokio::child) fn dropped(child: Child, needle: &str) -> (Vec<String>, Vec<log::Level>) {
        let mark = crate::log_capture::mark();
        leaf_fault::record_leaf_steps();
        drop(child);
        let steps = leaf_fault::take_leaf_steps();
        leaf_fault::take_rmdir_hook();
        (steps, crate::log_capture::levels_since(mark, needle))
    }
}

/// The tids of this process's threads.
#[cfg(target_os = "linux")]
fn thread_ids() -> std::collections::BTreeSet<String> {
    std::fs::read_dir("/proc/self/task")
        .expect("list /proc/self/task")
        .map(|entry| entry.expect("a task entry").file_name().to_string_lossy().into_owned())
        .collect()
}

/// A kill-on-drop drop starts no thread (principle 1): the drop's tree kill, root kill and leaf
/// release run on the dropping thread, and hand nothing to a pool.
///
/// Compares thread ids, not names: `Builder::name` is applied in the new thread's prologue, after
/// `spawn` returns, so a name check can pass spuriously. Runs alone, so no other test's threads
/// come and go between the two reads.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn an_async_drop_starts_no_thread() {
    if !alone(fixture_path!(an_async_drop_starts_no_thread)) {
        return;
    }
    let (child, teardown) = released_blocker();
    let before = thread_ids();
    drop(child);
    let started: Vec<_> = thread_ids().difference(&before).cloned().collect();
    assert!(started.is_empty(), "a drop started threads: {started:?}");
    teardown.assert_killed();
}

/// The parent's end of a piped stdout nobody took is closed by the time `drop` returns. Runs alone,
/// so no other thread can take the descriptor number in between.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn an_async_drop_closes_the_childs_untaken_stdout() {
    use std::os::fd::AsRawFd as _;

    if !alone(fixture_path!(an_async_drop_closes_the_childs_untaken_stdout)) {
        return;
    }
    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");
    cmd.stdout(crate::Stdio::pipe()).expect("set stdout pipe");
    let mut child = cmd.spawn().expect("spawn");
    let teardown = release_between_kill_and_wait(child.stdin().expect("piped stdin"));
    let Some(crate::pipe::ParentEnd::Reader(stdout)) = child.os.pipes.get(&crate::stdio::Fd::STDOUT) else {
        panic!("piped stdout");
    };
    let stdout = stdout.as_raw_fd();
    // SAFETY: `fcntl(F_GETFD)` reads a flag and changes nothing.
    assert_ne!(
        unsafe { libc::fcntl(stdout, libc::F_GETFD) },
        -1,
        "open while the child lives"
    );
    drop(child);
    // SAFETY: as above.
    let after = unsafe { libc::fcntl(stdout, libc::F_GETFD) };
    assert_eq!(after, -1, "the drop must close the child's untaken stdout");
    teardown.assert_killed();
}

/// Dropping a live, kill-on-drop root returns, on every platform: its kill bounds the drop's
/// wait. A wait added to `signal_on_drop`, for the root's exit or for the tree to drain, trips
/// the bounded-section contract here at once.
///
/// Mutants: `exited_opening(.., None)` or `os.attached.wait_drained(None)` added to
/// `signal_on_drop`.
#[skuld::test]
async fn a_kill_on_drop_drop_of_a_live_blocker_returns() {
    let (child, teardown) = released_blocker();
    drop(child);
    teardown.assert_killed();
}

/// `child`, contained by a process group led by its own pid, as a `ProcessGroup` spawn leaves it.
#[cfg(unix)]
fn in_its_own_group(mut child: Child) -> Child {
    child.os.attached = crate::containment::Attached::ProcessGroup(child.pid() as i32);
    child
}

/// A live, kill-on-drop root in a process group has its group killed by the drop.
#[cfg(unix)]
#[skuld::test]
async fn a_drop_of_a_live_group_root_kills_its_group() {
    crate::tokio::test_runtime::assert_current_thread();
    let (child, teardown) = released_blocker();
    let child = in_its_own_group(child);
    let pgid = child.pid() as i32;
    let killed = crate::containment::unix::fault::record_kill_group();
    drop(child);
    assert_eq!(killed.killed(), [pgid]);
    teardown.assert_killed();
}

/// The drop releases the resources once, on the dropping thread: a release handed to another
/// thread bumps that thread's count, not this one's.
///
/// Mutant: `OsResources::release_without_waiting` moves `self` into a `std::thread::spawn`.
#[skuld::test]
async fn a_drop_releases_its_resources_once_on_the_dropping_thread() {
    let releases = fault::count_releases();
    let backend_drops = fault::count_backend_drops();
    let (child, teardown) = released_blocker();
    assert_eq!(releases.get(), 0, "nothing is released before the drop");
    assert_eq!(backend_drops.get(), 0, "nothing is dropped before the drop");
    drop(child);
    assert_eq!(releases.get(), 1, "the dropping thread releases exactly once");
    assert_eq!(
        backend_drops.get(),
        1,
        "the drop hands the backend, and tokio's Child with it, its own drop, on this thread"
    );
    teardown.assert_killed();
}

/// A root that ignores signals 1-8, 10-18 and 20-64 (`trap '' $(seq 1 8) $(seq 10 18) $(seq 20
/// 64)`), blocked on its stdin, and its stdin's write end. Not ignored: `SIGKILL` (9) and
/// `SIGSTOP` (19), which cannot be, and 32 and 33, glibc's reserved real-time signals, which dash
/// accepts in a `trap` without ignoring (`SIGCHLD`, 17, is ignored by default anyway). It says `r`
/// once the `trap` is in place, and the caller reads that before dropping the child: otherwise the
/// drop races the `trap`, and a `SIGTERM` would still kill the root.
#[cfg(target_os = "linux")]
async fn ignoring_blocker() -> (Child, crate::tokio::ChildStdin, crate::tokio::ChildStdout) {
    use ::tokio::io::AsyncReadExt as _;

    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args([
        "sh",
        "-c",
        "trap '' $(seq 1 8) $(seq 10 18) $(seq 20 64); echo r; exec cat >/dev/null",
    ]);
    cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
    cmd.stdout(crate::Stdio::pipe()).expect("set stdout pipe");
    let mut child = cmd.spawn().expect("spawn");
    let stdin = child.stdin().expect("piped stdin");
    let mut stdout = child.stdout().expect("piped stdout");
    let mut ready = [0u8; 2];
    stdout.read_exact(&mut ready).await.expect("the root's `r` handshake");
    assert_eq!(&ready, b"r\n");
    (child, stdin, stdout)
}

/// The drop kills the root itself, and with `SIGKILL`: the root ignores every signal but `SIGKILL`,
/// `SIGSTOP` and 32 and 33 (see [`ignoring_blocker`]), so a drop that sent any other would leave it
/// running until its stdin, released once the kill returned, closes (`Exited(0)`). Mutants: the drop
/// skips the kill, or sends `SIGTERM` or `SIGPROF`.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_kill_on_drop_drop_kills_the_root() {
    let (child, stdin, _stdout) = ignoring_blocker().await;
    let teardown = release_between_kill_and_wait(stdin);
    drop(child);
    teardown.assert_killed();
}

/// The drop of a session tree kills its root and the group's member. The root is released once the
/// drop's kill returned, so a drop whose kills sent nothing fails by assertion instead of waiting.
/// - The group's signal lost: the member survives and fails `member.assert_killed()`. Below Linux
///   6.9 a lost `killpg` alone is masked by the member's own pidfd signals after it.
/// - The root's own kill lost: masked, the group's signal ends the root too.
/// - Both lost: the root exits `0` on the release and fails `teardown.assert_killed()`.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_drop_kills_a_session_root_and_the_members_of_its_group() {
    crate::tokio::test_runtime::assert_current_thread();
    let (child, member, release) = super::group_exact_tests::releasable_root_with_a_member();
    assert_eq!(child.containment(), crate::Containment::Session);
    let teardown = release_between_kill_and_wait(release);
    drop(child);
    teardown.assert_killed();
    member.assert_killed();
}

/// The drop of a cgroup tree kills its root and the leaf's member, waits for the leaf to drain, and
/// removes it. The root is released once the drop's kill returned and the member when the drain is
/// about to block (`Member::release_when_the_drain_blocks`), so a drop whose kills sent nothing
/// fails by assertion instead of waiting.
/// - The `cgroup.kill` lost: the member survives and fails `member.assert_killed()`.
/// - The root's own kill lost: masked, the `cgroup.kill` ends the root too.
/// - Both lost: the root exits `0` on the release and fails `teardown.assert_killed()`.
/// - The leaf left behind: fails `assert_leaf_removed`.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_drop_kills_a_cgroup_root_and_its_members_and_removes_the_leaf(#[fixture(cgroup)] _group: &Group) {
    use crate::test_groups::cgroup_leaf::LeafGuard;

    crate::tokio::test_runtime::assert_current_thread();
    let (child, member, release) = crate::test_child::member::releasable_root_with_a_member_contained(|cmd| {
        cmd.contain_with(crate::ContainMode::Strongest);
    });
    assert_eq!(child.containment(), crate::Containment::CgroupV2);
    let leaf = child.leaf_path_for_test().expect("a cgroup child holds its leaf");
    let _leaf_cleanup = LeafGuard::new(leaf.clone());
    let teardown = release_between_kill_and_wait(release);
    let _drain = member.release_when_the_drain_blocks();
    drop(child);
    teardown.assert_killed();
    member.assert_killed();
    assert_leaf_removed(&leaf);
}

/// The drop of an fd-marker tree kills its root and the tree's member. The armed drop waits for the
/// root, so the root is released once the drop's kill returned: a drop whose kills sent nothing
/// fails by assertion instead of waiting. The root then exits `0` and fails
/// `teardown.assert_killed()`, and a member nothing killed says `survived` and fails
/// `member.assert_killed()`.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn a_drop_kills_an_fd_marker_root_and_the_members_of_its_tree() {
    crate::tokio::test_runtime::assert_current_thread();
    let (child, member, release) = crate::test_child::member::releasable_root_with_a_member_contained(|cmd| {
        cmd.contain();
    });
    assert_eq!(child.containment(), crate::Containment::FdMarker);
    let teardown = release_between_kill_and_wait(release);
    drop(child);
    teardown.assert_killed();
    member.assert_killed();
}

/// The leaf an armed `Drop` killed is gone once the drop returns: the drop waits for the leaf to
/// drain, then removes it.
#[cfg(target_os = "linux")]
fn assert_leaf_removed(leaf: &std::path::Path) {
    assert!(
        !leaf.exists(),
        "the drop must drain and remove the leaf: {}",
        leaf.display()
    );
}

/// The drop's signals run inside a bounded section, which is what turns a wait added to them into a
/// debug panic: a hook run before the drop's `cgroup.kill` write sees it. So does a disarmed drop's
/// release, through its `rmdir`. An armed drop's own reap and drain run after the section: its
/// leaf's `rmdir` is outside it.
///
/// Mutants: the section ends before the signals; a disarmed drop leaves it before its release; the
/// armed drop waits inside it (the bounded contract panics there).
#[cfg(target_os = "linux")]
#[skuld::test]
async fn an_async_drops_signals_and_bounded_release_run_inside_a_bounded_section() {
    use std::cell::Cell;
    use std::rc::Rc;

    use crate::containment::cgroup::fault as leaf_fault;

    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let in_section_at = |name: &str, detach: bool| {
        let fake = crate::containment::cgroup::test_support::FakeLeaf::new(name, false);
        let at_kill = Rc::new(Cell::new(None));
        let at_rmdir = Rc::new(Cell::new(None));
        let (kill, rmdir) = (Rc::clone(&at_kill), Rc::clone(&at_rmdir));
        let _kill = leaf_fault::set_before_kill_write(move || kill.set(Some(crate::bounded::in_section())));
        let (path, events) = (fake.leaf.clone(), fake.events.clone());
        leaf_fault::set_rmdir_hook(move |_| {
            rmdir.set(Some(crate::bounded::in_section()));
            crate::containment::cgroup::test_support::FakeLeaf::rmdir(&path, &events)
        });
        let leaf = crate::containment::cgroup::test_support::entered_leaf_at(fake.leaf.clone());
        let (child, stdin) = blocker();
        // A detached drop kills nothing: its root ends when `_stdin` drops with the closure.
        let (teardown, _stdin) = if detach {
            (None, Some(stdin))
        } else {
            (Some(release_between_kill_and_wait(stdin)), None)
        };
        let mut child = linux::contained_by(child, leaf);
        if detach {
            child.detach().expect("detach");
        }
        drop(child);
        leaf_fault::take_rmdir_hook();
        if let Some(teardown) = teardown {
            teardown.assert_killed();
        }
        (at_kill.get(), at_rmdir.get())
    };

    assert_eq!(
        in_section_at("cosca-async-drop-armed-section", false),
        (Some(true), Some(false)),
        "(the armed drop's tree kill, its leaf's rmdir)"
    );
    assert_eq!(
        in_section_at("cosca-async-drop-detached-section", true),
        (None, Some(true)),
        "(no kill, the disarmed release's rmdir)"
    );
    assert!(!crate::bounded::in_section(), "the drop leaves the section");
}

#[cfg(target_os = "linux")]
#[skuld::test]
async fn an_async_drop_removes_an_already_drained_leaf() {
    let name = "cosca-async-drop-drained";
    let (fake, leaf) = linux::fake_leaf(name, false);
    let (child, teardown) = released_blocker();
    let (steps, levels) = linux::dropped(linux::contained_by(child, leaf), name);

    assert_eq!(steps, ["kill", "rmdir populated 0"]);
    assert_eq!(
        fake.kill_written(),
        crate::containment::cgroup::KILL_PAYLOAD,
        "the tree kill must write cgroup.kill"
    );
    assert!(!fake.leaf.exists(), "a drained leaf is removed");
    assert_eq!(levels, Vec::<log::Level>::new());
    teardown.assert_killed();
}

/// `kill_on_drop(false)` on a spawn that has not committed, with a `kill_tree()` behind it, as
/// tokio's `finish_elevated` leaves a handle: the handle no longer signals on drop, but the leaf
/// is still armed and this handle killed it. The tree has not drained, and the drop returns with
/// a warning, no second kill, and the root untouched.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_kill_on_drop_false_drop_of_an_armed_leaf_never_blocks() {
    if !alone(fixture_path!(a_kill_on_drop_false_drop_of_an_armed_leaf_never_blocks)) {
        return;
    }
    let name = "cosca-async-drop-opted-out-armed";
    let (fake, leaf) = linux::fake_leaf(name, true);
    let mut cmd = crate::test_child::command(Console::Hidden);
    cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
    cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
    cmd.kill_on_drop(false);
    let mut child = crate::tokio::spawn::spawn_uncommitted(&mut cmd).expect("spawn");
    let stdin = child.stdin().expect("piped stdin");
    let child = linux::contained_by(child, leaf);
    assert!(!child.kill_on_drop, "the command opted out");
    let root = linux::Pidfd::of(&child);
    // The root stays alive: only the tree is killed.
    let view = child.read_root_view(crate::containment::dispatch::TeardownCaller::Drop);
    let killed = child.kill_tree_members_unless_reaped(&view);
    assert!(killed.completed(), "kill the tree: {killed:?}");
    assert_eq!(
        fake.kill_written(),
        crate::containment::cgroup::KILL_PAYLOAD,
        "this handle's tree kill must write cgroup.kill"
    );
    // The handle's own kill, as `kill_tree` settles it: the premise of the drop below.
    child.settle_tree(killed, false).expect("the tree kill left nothing");
    assert!(child.tree_killed(), "this handle killed the tree");
    fake.clear_kill();
    let (steps, levels) = linux::dropped(child, name);

    assert_eq!(steps, ["rmdir populated 1"]);
    assert_eq!(fake.kill_written(), b"", "the landed kill is not written again");
    assert!(fake.leaf.exists());
    assert_eq!(levels, [log::Level::Warn]);
    assert_eq!(
        root.ended_after_closing(stdin),
        linux::Ended::Exited(0),
        "an opted-out drop must not signal the root"
    );
}

/// A teardown that ran no tree kill (`kill_on_drop(false)`) has nothing to settle, so it does not
/// mark the tree killed: a handle that killed nothing must not read as one that killed its tree.
///
/// Mutant: the teardown settles a placeholder outcome, `Ran(Ok(()))`, which marks.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_teardown_that_ran_no_tree_kill_does_not_mark_the_tree_killed() {
    let name = "cosca-async-teardown-no-kill";
    let (_fake, leaf) = linux::fake_leaf(name, true);
    let (child, stdin) = blocker();
    let mut child = linux::contained_by(child, leaf);
    child.detach().expect("detach");
    child.tear_down_now(crate::containment::dispatch::TeardownCaller::Drop);
    crate::containment::cgroup::fault::take_rmdir_hook();
    assert!(!child.tree_killed(), "no tree kill ran, so none is marked");
    drop(stdin);
}

#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_disarmed_never_killed_drop_never_kills_and_logs_at_debug() {
    if !alone(fixture_path!(
        a_disarmed_never_killed_drop_never_kills_and_logs_at_debug
    )) {
        return;
    }
    let name = "cosca-async-drop-disarmed-never-killed";
    let (fake, leaf) = linux::fake_leaf(name, true);
    let (child, stdin) = blocker();
    let mut child = linux::contained_by(child, leaf);
    child.detach().expect("detach");
    let root = linux::Pidfd::of(&child);
    let (steps, levels) = linux::dropped(child, name);

    assert_eq!(steps, ["rmdir populated 1"]);
    assert_eq!(linux::kill_file(&fake), b"", "nothing may write cgroup.kill");
    assert!(fake.leaf.exists());
    assert_eq!(levels, [log::Level::Debug]);
    assert_eq!(root.ended_after_closing(stdin), linux::Ended::Exited(0));
}

/// A root whose kill fails is left running, and nothing waits for it: the drop returns, the root
/// is alive, and it ends only when its stdin closes. A drop that waited for it would panic on the
/// debug contract. The root's number now belongs to tokio's orphan queue, so this test only ever
/// reads it through its own pidfd.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_root_whose_kill_fails_is_handed_off_not_waited_on() {
    crate::tokio::test_runtime::assert_current_thread();
    if !alone(fixture_path!(a_root_whose_kill_fails_is_handed_off_not_waited_on)) {
        return;
    }
    let name = "cosca-async-drop-handoff";
    let (fake, leaf) = linux::fake_leaf(name, false);
    let (child, stdin) = blocker();
    let mut child = linux::contained_by(child, leaf);
    let root = linux::Pidfd::of(&child);
    let pid = child.pid();

    // `kill_tree`'s backstop kill fails, so the root survives it: the tree kill alone does not
    // signal the root.
    let armed = fault::force_kill_failure();
    child.kill_tree().expect_err("the forced backstop failure surfaces");
    drop(armed);
    fake.clear_kill();

    // Take-once: armed again for the drop's own root kill.
    let _armed = fault::force_kill_failure();
    let backend_drops = fault::count_backend_drops();
    let mark = crate::log_capture::mark();
    let (steps, levels) = linux::dropped(child, name);

    assert_eq!(
        steps,
        ["kill", "rmdir populated 0"],
        "tree kill, then a release that removes"
    );
    assert_eq!(
        fake.kill_written(),
        crate::containment::cgroup::KILL_PAYLOAD,
        "the drop's tree kill must write cgroup.kill, not only record the step"
    );
    assert_eq!(levels, Vec::<log::Level>::new(), "the drained leaf is not warned about");
    let needle = format!("async child {pid} could not be terminated on drop");
    assert_eq!(
        crate::log_capture::levels_since(mark, &needle),
        [log::Level::Warn],
        "the root the drop could not signal is logged once"
    );
    assert_eq!(
        backend_drops.get(),
        1,
        "the drop must hand tokio its own Child to drop, never forget it"
    );
    assert!(!fake.leaf.exists(), "the drained leaf is released");
    // The drop returned with the root alive, so it can only end now, by the close.
    assert_eq!(
        root.ended_after_closing(stdin),
        linux::Ended::Exited(0),
        "neither the tree kill nor the drop may have signalled the root"
    );
}

/// A root the drop could not signal is logged, and the drop returns: it does not wait for a root
/// it could not stop. Closing its stdin afterwards lets it end on its own.
#[skuld::test]
async fn unreaped_root_could_not_be_terminated_on_drop_is_logged() {
    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let (child, stdin) = blocker();
    let pid = child.pid();
    let _armed = fault::force_kill_failure();
    let mark = crate::log_capture::mark();
    drop(child);

    let needle = format!("async child {pid} could not be terminated on drop");
    assert_eq!(crate::log_capture::levels_since(mark, &needle), [log::Level::Warn]);
    drop(stdin);
}

/// Async twin of the sync `drop_warns_instead_of_asserting_on_a_real_teardown_mechanism_failure`
/// (`child_tests.rs`): a failed `cgroup.kill` write reached during `Child::drop`'s OWN teardown
/// is a real OS outcome: Drop must warn and return normally.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn drop_warns_instead_of_asserting_on_a_real_teardown_mechanism_failure() {
    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let dir = tempfile::tempdir().expect("tempdir");
    let leaf_path = dir.path().join("cosca-async-drop-kill-fail-leaf");
    std::fs::create_dir(&leaf_path).expect("create the leaf");
    std::fs::write(leaf_path.join("occupant"), "").expect("keep the leaf unremovable");
    std::fs::create_dir(leaf_path.join("cgroup.kill")).expect("make cgroup.kill a directory");
    crate::child::spawn::fault::set_attachment_override(crate::containment::Attachment {
        containment: crate::containment::Containment::CgroupV2,
        attached: crate::containment::Attached::Cgroup(crate::containment::cgroup::test_support::entered_leaf_at(
            leaf_path.clone(),
        )),
        graceful: crate::graceful::GracefulMechanism::Process,
    });

    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");
    cmd.stdout(crate::Stdio::null()).expect("set stdout");
    // The override is consumed by this spawn; `kill_on_drop` defaults to true. The tree-kill call
    // in Drop runs on the dropping thread, so no probe is needed to observe it.
    let mut child = cmd.spawn().expect("spawn");
    let teardown = release_between_kill_and_wait(child.stdin().expect("piped stdin"));
    let pid = child.pid();
    let sends = crate::send_log::Capture::start();

    // Pin that the forced failure is the mechanism class this test claims (a raw `EISDIR` from
    // the `cgroup.kill` write, surfaced as `Error::Io`), so it cannot go vacuous if the forcing
    // stops reaching the kill.
    let forced = child
        .kill_tree()
        .expect_err("the forced cgroup.kill failure must surface from kill_tree");
    assert!(
        matches!(&forced, crate::error::Error::Io(io) if io.raw_os_error() == Some(libc::EISDIR)),
        "the forced failure must be a mechanism-class Error::Io(EISDIR), got {forced:?}"
    );

    let mark = crate::log_capture::mark();
    // A same-text record from ANOTHER thread, fixed before the drop by the join: the thread-filtered
    // scan below must not count it (a concurrent test's identical record would look the same).
    let marker = "Child::drop: contained-tree teardown did not fully succeed";
    std::thread::spawn(move || log::warn!("{marker}: from another thread"))
        .join()
        .expect("emit from another thread");
    let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(child)));
    assert!(
        unwound.is_ok(),
        "Child::drop must not panic on a real teardown-mechanism failure: {unwound:?}"
    );

    // `Drop` logs on the dropping thread, so the current-thread scan sees exactly its record.
    let records = crate::log_capture::records_since_on_current_thread(mark, marker);
    assert_eq!(
        records.iter().map(|(level, _)| *level).collect::<Vec<_>>(),
        [log::Level::Warn],
        "a real teardown-mechanism failure during Drop must be logged at warn, got {records:?}"
    );
    // The failed tree kill must not cost the root its own kill: its stdin, released once that kill
    // returned, would otherwise let it exit `0`.
    teardown.assert_killed();
    assert!(
        sends.sent(pid, crate::signal::Sig::Kill),
        "the drop must send the root SIGKILL: {:?}",
        sends.entries()
    );
}

// The fate of a teardown of a child already reaped =====

/// A child whose exit this handle collected is `Reaped` when its teardown runs, whether or not the
/// drop is armed, on every platform: nothing is left to signal.
///
/// Mutant: a disarmed teardown of a reaped child reports `Running`, as the child is left alone.
#[skuld::test]
async fn a_teardown_of_a_reaped_child_is_reaped_armed_or_not() {
    crate::tokio::test_runtime::assert_current_thread();
    for kill_on_drop in [true, false] {
        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");
        cmd.stdout(crate::Stdio::null()).expect("set stdout");
        cmd.kill_on_drop(kill_on_drop);
        let mut child = cmd.spawn().expect("spawn");
        drop(child.stdin().expect("piped stdin"));
        child.wait().await.expect("the child exits once its stdin closes");
        assert_eq!(
            child.tear_down_now(crate::containment::dispatch::TeardownCaller::Drop),
            crate::error::ChildFate::Reaped,
            "kill_on_drop={kill_on_drop}"
        );
    }
}