cosca 0.6.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
//! Helpers the cgroup module's tests share.

/// Fork a child that runs `body` and `_exit(0)`s; `body` must be async-signal-safe (this process
/// has other threads). The child never `exec`s, so an unreaped orphan keeps this binary's
/// inherited fds (stdout) open. The returned [`KillOnDrop`] SIGKILLs and reaps it on drop unless
/// [`defused`](KillOnDrop::defuse).
///
/// Forks under [`spawn_lock`], so the never-exec child cannot inherit an fd another spawn holds
/// transiently non-`CLOEXEC` inside its own `spawn_lock` section. Held across `fork()` until the
/// parent's `pidfd_open` resolves.
#[cfg(target_os = "linux")]
pub(crate) fn fork_running(body: impl FnOnce()) -> KillOnDrop {
    let guard =
        crate::child::spawn::spawn_lock_tracked(crate::containment::cgroup::fault::run_fork_running_lock_contended);
    fork_running_held(Held::Owned(guard), body)
}

/// [`fork_running`] under a `spawn_lock` the caller already holds: forks without re-acquiring
/// it (the mutex is not reentrant) and leaves it held on return. For a caller whose own fds must
/// stay unpinned by other forks for longer than this fork.
#[cfg(target_os = "linux")]
pub(crate) fn fork_running_locked(guard: &crate::child::spawn::SpawnLockGuard, body: impl FnOnce()) -> KillOnDrop {
    debug_assert!(
        crate::child::spawn::spawn_lock_held_by_this_thread(),
        "fork_running_locked: the guard must be this thread's spawn_lock"
    );
    fork_running_held(Held::Borrowed(guard), body)
}

/// The `spawn_lock` a fork runs under: taken by [`fork_running`], or the caller's.
#[cfg(target_os = "linux")]
#[allow(
    dead_code,
    reason = "held only for their Drop / as proof the lock is held; never read"
)]
enum Held<'a> {
    Owned(crate::child::spawn::SpawnLockGuard),
    Borrowed(&'a crate::child::spawn::SpawnLockGuard),
}

#[cfg(target_os = "linux")]
impl Held<'_> {
    /// Unlock if this fork took the lock itself; a caller's guard stays held.
    fn release(self) {
        drop(self);
    }
}

#[cfg(target_os = "linux")]
fn fork_running_held(held: Held<'_>, body: impl FnOnce()) -> KillOnDrop {
    // Taken before the fork: the child uses this captured value, not the thread-local.
    let report_fd = crate::containment::cgroup::fault::take_fork_running_lock_held_report_fd();
    // SAFETY: the child runs only `body`, async-signal-safe by the caller's contract, then
    // `_exit`s without unwinding or running destructors.
    let raw_pid = unsafe { libc::fork() };
    match raw_pid {
        -1 => panic!("fork: {}", std::io::Error::last_os_error()),
        0 => {
            // Report whether the forked copy saw the lock held: a plain `Cell` read and a raw
            // `write(2)`, both async-signal-safe. Reading it in the child (not the parent) can't
            // be fooled by drop-before/reacquire-after the fork.
            if let Some(fd) = report_fd {
                let held: u8 = crate::child::spawn::spawn_lock_held_by_this_thread().into();
                // SAFETY: `fd` is a pipe write end a test provided for exactly this; `held` is a
                // valid one-byte buffer.
                let written = unsafe { libc::write(fd, (&raw const held).cast(), 1) };
                if written != 1 {
                    // No formatted panic (allocates) between fork and `_exit`: a distinct exit
                    // code the test reads from the reaped status.
                    // SAFETY: async-signal-safe.
                    unsafe { libc::_exit(REPORT_WRITE_FAILED_EXIT) };
                }
            }
            // Never dropped here: `MutexGuard::drop`'s unlock (an atomic swap, a `FUTEX_WAKE`
            // when contended) is not async-signal-safe, and this process's own copy of the lock
            // state dies with it regardless — only the parent's release is real.
            std::mem::forget(held);
            body();
            // SAFETY: async-signal-safe.
            unsafe { libc::_exit(0) }
        }
        raw_pid => {
            let pid = raw_pid as u32;
            // Opened right after the fork: only our still-unreaped child can hold this pid now,
            // so the pidfd names it exactly.
            let child = rustix::process::Pid::from_raw(raw_pid).expect("fork returned a positive pid");
            let pidfd = if crate::containment::cgroup::fault::take_force_fork_running_pidfd_failure() {
                Err(rustix::io::Errno::MFILE)
            } else {
                rustix::process::pidfd_open(child, rustix::process::PidfdFlags::empty())
            };
            match pidfd {
                Ok(pidfd) => {
                    let kod = KillOnDrop {
                        pid,
                        pidfd: Some(pidfd),
                    };
                    // Run only once `kod` exists, and only then release the lock: a seam that
                    // panics still unwinds through `kod`'s `Drop`, which kills and reaps the child.
                    crate::containment::cgroup::fault::run_after_fork_still_locked();
                    held.release();
                    kod
                }
                Err(e) => {
                    held.release();
                    crate::containment::cgroup::fault::run_fork_running_cleanup();
                    // The probe pidfd lets the test verify the reap without racing pid reuse. Its
                    // failure is reported but must not skip the kill/reap.
                    let probe = if crate::containment::cgroup::fault::take_force_fork_running_probe_pidfd_failure() {
                        Err(rustix::io::Errno::MFILE)
                    } else {
                        rustix::process::pidfd_open(child, rustix::process::PidfdFlags::empty())
                    };
                    match probe {
                        Ok(probe) => crate::containment::cgroup::fault::record_fork_running_pidfd_failure_probe(probe),
                        Err(probe_err) => {
                            use std::io::Write;
                            _ = writeln!(std::io::stderr(), "fork_running: probe pidfd_open: {probe_err}");
                        }
                    }
                    // Bare pid is safe: the child is still unreaped, so the pid can't be recycled.
                    // SAFETY: `raw_pid` is our unreaped child.
                    let killed = unsafe { libc::kill(raw_pid, libc::SIGKILL) };
                    let cleanup_err = if killed != 0 {
                        // The child may still be alive: a blocking reap here could hang forever.
                        Some(format!("kill: {}", std::io::Error::last_os_error()))
                    } else {
                        let mut status = 0;
                        let reaped = loop {
                            // SAFETY: `raw_pid` is our unreaped child.
                            let reaped = unsafe { libc::waitpid(raw_pid, &mut status, 0) };
                            if reaped == -1 && std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted
                            {
                                continue;
                            }
                            break reaped;
                        };
                        (reaped != raw_pid).then(|| format!("waitpid: {}", std::io::Error::last_os_error()))
                    };
                    // No pre-empting assert: `e` — the reason a guard couldn't be made — is the
                    // point of this panic, and a cleanup failure is additional detail on it, not
                    // a replacement for it.
                    match cleanup_err {
                        None => panic!("pidfd_open its own just-forked child: {e}"),
                        Some(cleanup_err) => {
                            panic!("pidfd_open its own just-forked child: {e} (cleanup also failed: {cleanup_err})")
                        }
                    }
                }
            }
        }
    }
}

/// Exit code of a [`fork_running`] child whose lock-held report `write(2)` failed or was short.
#[cfg(target_os = "linux")]
pub(crate) const REPORT_WRITE_FAILED_EXIT: i32 = 113;

/// SIGKILLs and reaps a forked child on drop unless [`defuse`](Self::defuse)d. Uses a pidfd, not
/// the pid, which the kernel may recycle after a reap.
#[cfg(target_os = "linux")]
#[must_use = "dropping this immediately kills and reaps the child; bind it for as long as the child must live"]
pub(crate) struct KillOnDrop {
    pid: u32,
    pidfd: Option<std::os::fd::OwnedFd>,
}

#[cfg(target_os = "linux")]
impl KillOnDrop {
    /// The guarded pid, without disarming the guard. Don't reap through this pid: it would race
    /// the guard's `Drop`. [`defuse`](Self::defuse) first to take over.
    pub(crate) fn pid(&self) -> u32 {
        self.pid
    }

    /// Disarm the guard and return the pid; the caller must now reap it.
    #[must_use = "the pid still needs reaping by some other means; dropping it here leaks the child"]
    pub(crate) fn defuse(mut self) -> u32 {
        self.pidfd = None;
        self.pid
    }
}

#[cfg(target_os = "linux")]
impl Drop for KillOnDrop {
    fn drop(&mut self) {
        use std::io::Write;
        use std::os::fd::AsFd;

        let Some(pidfd) = self.pidfd.take() else {
            return;
        };
        let panicking = std::thread::panicking();
        // Mid-unwind a panic would abort, so failures are reported to stderr, not asserted
        // (`writeln!` because `eprintln!` panics on a closed stderr). After a failed kill, return
        // without waiting: the child may still be alive.
        let killed = if crate::containment::cgroup::fault::take_force_kill_on_drop_kill_failure() {
            Err(rustix::io::Errno::PERM)
        } else {
            rustix::process::pidfd_send_signal(pidfd.as_fd(), rustix::process::Signal::KILL)
        };
        if killed.is_err() {
            _ = writeln!(std::io::stderr(), "KillOnDrop: pidfd_send_signal: {killed:?}");
            if !panicking {
                debug_assert!(killed.is_ok(), "pidfd_send_signal: {killed:?}");
            }
            return;
        }

        let reaped = loop {
            if crate::containment::cgroup::fault::take_force_kill_on_drop_waitid_eintr() {
                continue;
            }
            match rustix::process::waitid(
                rustix::process::WaitId::PidFd(pidfd.as_fd()),
                rustix::process::WaitIdOptions::EXITED,
            ) {
                Err(rustix::io::Errno::INTR) => continue,
                other => break other,
            }
        };
        if reaped.is_err() {
            if panicking {
                _ = writeln!(std::io::stderr(), "KillOnDrop: waitid: {reaped:?}");
            } else {
                debug_assert!(reaped.is_ok(), "waitid: {reaped:?}");
            }
        }
    }
}

/// Reap `pid`, a child of this process.
#[cfg(target_os = "linux")]
pub(crate) fn reap(pid: u32) {
    reap_status(pid);
}

/// [`reap`], returning the raw wait status.
#[cfg(target_os = "linux")]
pub(crate) fn reap_status(pid: u32) -> i32 {
    let mut status = 0;
    // SAFETY: `pid` is this process's own child; `status` is a valid, writable int.
    let reaped = unsafe { libc::waitpid(pid as i32, &mut status, 0) };
    assert_eq!(reaped, pid as i32, "waitpid: {}", std::io::Error::last_os_error());
    status
}

/// A forked child that fails inside [`block_on`] cannot say why — no formatted panic, no
/// allocation — so this number is the only diagnostic a hang left blocked on that read gets.
#[cfg(target_os = "linux")]
const BLOCK_ON_READ_FAILED_EXIT: i32 = 111;

/// Block on `gate` until a byte arrives. Async-signal-safe: may run in a forked, pre-exec child.
#[cfg(target_os = "linux")]
pub(crate) fn block_on(gate: std::os::fd::RawFd) {
    let mut byte = 0u8;
    let n = loop {
        // SAFETY: `gate` is an open read end; `byte` is a valid one-byte buffer.
        let n = unsafe { libc::read(gate, (&raw mut byte).cast(), 1) };
        if n == -1 {
            if std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted {
                continue;
            }
            // A real read failure leaves nothing to retry, and this may run pre-exec in a forked
            // child: no formatted panic (allocates), a distinct exit instead.
            // SAFETY: async-signal-safe.
            unsafe { libc::_exit(BLOCK_ON_READ_FAILED_EXIT) };
        }
        break n;
    };
    debug_assert_eq!(n, 1, "read");
}

/// A copy of `channel`'s child end, standing in for the one a forked child inherits: the parent's
/// own copy closes when the exchange ends, as it does in a real spawn.
#[cfg(target_os = "linux")]
pub(crate) fn childs_copy(
    channel: &crate::containment::cgroup::ReportChannel,
) -> (std::os::fd::OwnedFd, crate::containment::cgroup::ReportSlot) {
    use std::os::fd::{AsRawFd, BorrowedFd};

    // SAFETY: the slot's descriptor is open for as long as `channel` lives, which spans this call.
    let end = unsafe { BorrowedFd::borrow_raw(channel.slot().fd) }
        .try_clone_to_owned()
        .expect("dup the child's end");
    let slot = crate::containment::cgroup::ReportSlot {
        fd: end.as_raw_fd(),
        parent_fd: -1,
    };
    (end, slot)
}

/// Run the test `name` (its full path) alone, in a copy of this test binary, and assert it passed.
/// `true` in the copy, which runs the test's body; `false` in the caller, which returns.
///
/// For a test that closes the parent's end of a channel and needs the child to see that close: any
/// process another test forks meanwhile holds a copy of that end until its own `exec`, and keeps
/// the socket open past the close.
#[cfg(target_os = "linux")]
pub(crate) fn alone(name: &str) -> bool {
    const ALONE: &str = "COSCA_TEST_ALONE";
    if std::env::var_os(ALONE).is_some_and(|alone| alone == name) {
        return true;
    }
    let out = crate::test_spawn::output_captured(
        crate::test_reexec::with_json_events(
            crate::test_reexec::command(std::env::current_exe().expect("this test binary")).args([
                name,
                "--exact",
                crate::test_reexec::NOCAPTURE,
                "--test-threads=1",
            ]),
        )
        .env(ALONE, name),
    )
    .expect("run the test alone");
    let stdout = String::from_utf8_lossy(&out.stdout);
    if let Err(why) = crate::test_reexec::suite_passed_exactly_one(&out) {
        panic!(
            "{why}\n{}\n--- stdout ---\n{stdout}\n--- stderr ---\n{}",
            out.status,
            String::from_utf8_lossy(&out.stderr)
        );
    }
    false
}

/// A pidfd for `pid`, an unreaped child of this process, or this process itself.
#[cfg(target_os = "linux")]
pub(crate) fn pidfd_of(pid: u32) -> std::os::fd::OwnedFd {
    rustix::process::pidfd_open(
        rustix::process::Pid::from_raw(pid as i32).expect("a positive pid"),
        rustix::process::PidfdFlags::empty(),
    )
    .expect("open a pidfd")
}

/// `pid` as a spawn holds its child, with `pidfd` its pidfd.
#[cfg(target_os = "linux")]
pub(crate) fn handle_of(pid: u32, pidfd: &std::os::fd::OwnedFd) -> crate::containment::ChildHandle<'_> {
    crate::containment::ChildHandle {
        pid,
        pidfd: std::os::fd::AsFd::as_fd(pidfd),
    }
}

/// A test leaf at `leaf_path` whose verdict is taken, with the child reported `Placed`: an
/// attached leaf whose `Drop` may kill.
#[cfg(target_os = "linux")]
pub(crate) fn entered_leaf_at(leaf_path: std::path::PathBuf) -> crate::containment::cgroup::CgroupLeaf {
    let mut leaf = crate::containment::cgroup::CgroupLeaf::for_test_at(leaf_path);
    // SAFETY: the slot's channel lives as long as `leaf`.
    unsafe { leaf.placement_slot().report_placed_for_test() };
    // The verdict needs a live pid: this process's own stands in for the child.
    leaf.take_placement(handle_of(std::process::id(), &pidfd_of(std::process::id())))
        .expect("the child reported Placed");
    leaf
}

/// A temp-directory stand-in for a cgroup leaf, `<tempdir>/<name>`.
///
/// Its `cgroup.events` is a symlink to a file outside the leaf, so removing the leaf leaves that
/// file, and every fd and watch on it, untouched: as removing a real leaf neither modifies its
/// `cgroup.events` nor delivers any event on it. [`rmdir`](FakeLeaf::rmdir) answers as cgroupfs
/// does, through [`fault::set_rmdir_hook`](super::fault::set_rmdir_hook).
#[cfg(target_os = "linux")]
pub(crate) struct FakeLeaf {
    _dir: tempfile::TempDir,
    pub(crate) leaf: std::path::PathBuf,
    /// The file the leaf's `cgroup.events` resolves to.
    pub(crate) events: std::path::PathBuf,
}

#[cfg(target_os = "linux")]
impl FakeLeaf {
    pub(crate) fn new(name: &str, populated: bool) -> FakeLeaf {
        let dir = tempfile::tempdir().expect("tempdir");
        let leaf = dir.path().join(name);
        std::fs::create_dir(&leaf).expect("create the leaf");
        let events = dir.path().join(format!("{name}.events"));
        std::fs::write(&events, format!("populated {}\nfrozen 0\n", u8::from(populated))).expect("write cgroup.events");
        std::os::unix::fs::symlink(&events, leaf.join("cgroup.events")).expect("link cgroup.events");
        std::fs::write(leaf.join("cgroup.kill"), b"").expect("create cgroup.kill");
        FakeLeaf {
            _dir: dir,
            leaf,
            events,
        }
    }

    /// Flip `populated` by rewriting its one digit in place: a truncating rewrite could be read
    /// half-done, as an empty file.
    pub(crate) fn set_populated(events: &std::path::Path, populated: bool) {
        use std::os::unix::fs::FileExt as _;
        let file = std::fs::OpenOptions::new()
            .write(true)
            .open(events)
            .expect("open cgroup.events");
        file.write_all_at(if populated { b"1" } else { b"0" }, "populated ".len() as u64)
            .expect("flip populated");
    }

    pub(crate) fn is_populated(events: &std::path::Path) -> bool {
        let contents = std::fs::read_to_string(events).expect("read cgroup.events");
        contents.lines().any(|l| l.trim() == "populated 1")
    }

    /// What a third party's `rmdir` of the leaf does: the leaf is gone, its `cgroup.events` file
    /// is untouched.
    pub(crate) fn remove(leaf: &std::path::Path) {
        for entry in std::fs::read_dir(leaf).expect("list the leaf") {
            std::fs::remove_file(entry.expect("leaf entry").path()).expect("remove a leaf file");
        }
        std::fs::remove_dir(leaf).expect("remove the leaf");
    }

    /// cgroupfs's `rmdir`: `ENOENT` once gone, `EBUSY` while populated, else the leaf goes.
    pub(crate) fn rmdir(leaf: &std::path::Path, events: &std::path::Path) -> std::io::Result<()> {
        if !leaf.exists() {
            return Err(std::io::Error::from_raw_os_error(libc::ENOENT));
        }
        if FakeLeaf::is_populated(events) {
            return Err(std::io::Error::from_raw_os_error(libc::EBUSY));
        }
        FakeLeaf::remove(leaf);
        Ok(())
    }
}

/// Remove a real leaf once it drains, for a test's cleanup after a failure. Blocks on the leaf's
/// own drain watch.
#[cfg(target_os = "linux")]
pub(crate) fn remove_drained_leaf(leaf_path: &std::path::Path) {
    let leaf = crate::containment::cgroup::CgroupLeaf::for_test_at(leaf_path.to_path_buf());
    leaf.wait_drained(None).expect("wait for the drain");
    match std::fs::remove_dir(leaf_path) {
        Ok(()) => {}
        Err(e) if e.raw_os_error() == Some(libc::ENOENT) => {}
        Err(e) => panic!("remove the leaf: {e}"),
    }
}

/// A real child placed in a leaf; `Drop` kills and reaps it so a panic cannot leak it or block
/// the leaf's removal. Declare it after its leaf so it drops first. Field 1 is the piped stdin,
/// held open so `cat` never exits on its own.
#[cfg(target_os = "linux")]
pub(crate) struct Member(
    pub(crate) std::process::Child,
    #[allow(
        dead_code,
        reason = "held only so dropping the Member closes the child's stdin; never read"
    )]
    pub(crate) std::process::ChildStdin,
);

#[cfg(target_os = "linux")]
impl Drop for Member {
    fn drop(&mut self) {
        // Already killed and reaped by the test in the common case. If std's own `Child` already
        // observed the exit (a prior `wait`/`try_wait`), `kill` checks its cached status first and
        // returns `Ok` without a syscall — `InvalidInput` never happens on that path. If the child
        // was reaped some other way instead (a foreign `waitpid`, principle 5), the syscall does
        // run, and fails with `ESRCH`. Both are "already gone"; only something outside that set is
        // the real signal/reap failure this guard exists to catch.
        if let Err(e) = self.0.kill() {
            debug_assert_eq!(
                e.raw_os_error(),
                Some(libc::ESRCH),
                "Member::drop's kill failed unexpectedly: {e}"
            );
        }
        if let Err(e) = self.0.wait() {
            debug_assert_eq!(
                e.raw_os_error(),
                Some(libc::ECHILD),
                "Member::drop's wait failed unexpectedly: {e}"
            );
        }
    }
}

/// Installs the drain-wait seams (`set_wait_site_park_notifier`, `set_drain_blocking_notifier`,
/// `set_drain_zero_remaining_notifier`), uninstalling all three on drop — panic-safe, so a panic
/// in the closure passed to [`WaitObserver::run`]/[`WaitObserver::run_with_block_sender`] still
/// leaves this thread's seams clean for whatever runs next.
#[cfg(target_os = "linux")]
struct WaitObserverGuard;

#[cfg(target_os = "linux")]
impl Drop for WaitObserverGuard {
    fn drop(&mut self) {
        crate::containment::cgroup::fault::take_wait_site_park_notifier();
        crate::containment::cgroup::fault::take_drain_blocking_notifier();
        crate::containment::cgroup::fault::take_drain_zero_remaining_notifier();
    }
}

#[cfg(target_os = "linux")]
pub(crate) struct WaitObserver;

#[cfg(target_os = "linux")]
impl WaitObserver {
    /// Run `f` with the seams installed, and return its own result alongside what they saw: each
    /// time the wait site's own wait call returned (see `WaitSitePark`'s own doc for what that
    /// does and doesn't prove), how many times `drain_step` announced it was about to block, and
    /// how many times the zero-remaining shortcut fired.
    pub(crate) fn run<T>(
        f: impl FnOnce() -> T,
    ) -> (T, Vec<crate::containment::cgroup::fault::WaitSitePark>, usize, usize) {
        let (block_tx, block_rx) = std::sync::mpsc::channel();
        let (result, parks, zero_remainings) = Self::run_with_block_sender(block_tx, f);
        (result, parks, block_rx.try_iter().count(), zero_remainings)
    }

    /// Like `run`, but the caller supplies the `drain_blocking` sender itself — so it (or a
    /// receiver on another thread, paired with this same sender) can react to the signal while
    /// the wait is still in progress, before this call returns. The caller owns the count of
    /// whatever it receives on the matching receiver; this returns only the parks and the
    /// zero-remaining count.
    pub(crate) fn run_with_block_sender<T>(
        block_tx: std::sync::mpsc::Sender<()>,
        f: impl FnOnce() -> T,
    ) -> (T, Vec<crate::containment::cgroup::fault::WaitSitePark>, usize) {
        use crate::containment::cgroup::fault;
        let (park_tx, park_rx) = std::sync::mpsc::channel();
        let (zero_tx, zero_rx) = std::sync::mpsc::channel();
        fault::set_wait_site_park_notifier(park_tx);
        fault::set_drain_blocking_notifier(block_tx);
        fault::set_drain_zero_remaining_notifier(zero_tx);
        let _guard = WaitObserverGuard;
        let result = f();
        (result, park_rx.try_iter().collect(), zero_rx.try_iter().count())
    }
}

/// Installs `set_wait_deadline_arg_notifier` on construction, uninstalling it on drop —
/// panic-safe, same pattern as `WaitObserverGuard`. Separate from `WaitObserver` itself: this
/// seam is for the one property `WaitSitePark`'s own `deadline` field can't prove (that a
/// bounded park was armed with the caller's own requested instant, not merely returns it back
/// unread), not something every sync-wait test needs.
#[cfg(target_os = "linux")]
#[must_use]
pub(crate) struct WaitDeadlineArgGuard;

#[cfg(target_os = "linux")]
impl WaitDeadlineArgGuard {
    pub(crate) fn install() -> (Self, std::sync::mpsc::Receiver<std::time::Instant>) {
        let (tx, rx) = std::sync::mpsc::channel();
        crate::containment::cgroup::fault::set_wait_deadline_arg_notifier(tx);
        (Self, rx)
    }
}

#[cfg(target_os = "linux")]
impl Drop for WaitDeadlineArgGuard {
    fn drop(&mut self) {
        crate::containment::cgroup::fault::take_wait_deadline_arg_notifier();
    }
}

/// A bounded call concludes exactly once via the zero-remaining shortcut, and every announced
/// block must show up as a wait-site return (see `WaitSitePark`'s own doc for what that does and
/// doesn't prove — on its own, it does not prove a real park happened; pair this with a real
/// pump-batch count for that). At most one wait-site return times out (event_listener reports
/// "not woken" only at/after its deadline), and it must be last. Earlier ones may be
/// `woken: true` (cgroup.events writes don't imply a membership change). Zero is legitimate: the
/// first check may already find the deadline spent.
#[cfg(target_os = "linux")]
pub(crate) fn assert_bounded_conclusion(
    parks: &[crate::containment::cgroup::fault::WaitSitePark],
    blocks: usize,
    zero_remainings: usize,
    expected_deadline: std::time::Instant,
) {
    assert_eq!(
        zero_remainings, 1,
        "a bounded call that finds a member still alive must conclude exactly once through \
         the zero-remaining shortcut, got {zero_remainings}"
    );
    assert_eq!(
        blocks,
        parks.len(),
        "drain_step announced blocking {blocks} times, but the wait site's own wait call \
         returned {} times — every announced block must show up as a wait-site return",
        parks.len()
    );
    // Structural: elapsed time proves only "not early", not "armed with the right instant".
    assert!(
        parks.iter().all(|p| p.deadline == Some(expected_deadline)),
        "every park in a bounded call must report the caller's own requested deadline instant \
         exactly, got {parks:?}"
    );
    let timed_out = parks.iter().filter(|p| !p.woken).count();
    assert!(
        timed_out <= 1,
        "at most one park can genuinely time out before the call concludes, got {parks:?}"
    );
    if let Some(pos) = parks.iter().rposition(|p| !p.woken) {
        assert_eq!(pos, parks.len() - 1, "a timed-out park must be the last, got {parks:?}");
    }
}

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