cosca 0.3.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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! Async `Child` handle, wrapping `::tokio::process::Child` plus the stable `ProcessId` and the
//! contained-tree `Attached`.

#[path = "child/graceful.rs"]
mod graceful;

#[path = "child/proc_source.rs"]
mod proc_source;
pub(crate) use proc_source::ProcSource;

#[path = "child/reaper.rs"]
mod reaper;

use std::collections::BTreeMap;
use std::process::ExitStatus;

#[cfg(unix)]
use crate::child::ParentEnd;
use crate::containment::{Attached, Containment};
use crate::error::Error;
use crate::identity::ProcessId;
use crate::stdio::Fd;

/// Parent ends of fd >= 3 pipes, keyed by descriptor. Unix stashes the raw sync `ParentEnd`
/// (converted to a reactor pipe at take time); Windows stashes the already-registered overlapped
/// async end (the raw backend's fd >= 3 pipes), taken directly (no `from_raw_handle`, which would
/// double-register the IOCP handle).
#[cfg(unix)]
pub(super) type FdPipes = BTreeMap<Fd, ParentEnd>;
#[cfg(windows)]
pub(super) type FdPipes = BTreeMap<Fd, super::stdio::OwnedStd>;

/// The `expect` behind the two backend accessors: only `Drop` takes `proc`, and nothing runs on
/// the handle after that, so both are infallible.
const PROC_TAKEN: &str = "the async child's process backend is taken only by Drop";

/// Every field of a [`Child`] that owns an OS resource, **declared in the order they must be
/// released**: the backend first, so the pid stays pinned for the whole wait and each other
/// release is ordered after the reap.
///
/// This grouping is the parity contract with [`reaper::ReapJob`], which carries one of these
/// verbatim: `Drop` hands the whole group over, so a resource-owning field added here travels to
/// the reaper — and is released in the right order — with no other edit. Adding one directly to
/// `Child` instead would silently keep its release on the dropping thread.
#[derive(Debug, Default)]
pub(crate) struct OsResources {
    /// `Option` so `Drop` can move the backend into the reaper job; nothing else takes it.
    /// Read it through [`proc_mut`](OsResources::proc_mut), never directly.
    pub(crate) proc: Option<ProcSource>,
    pub(crate) attached: Attached,
    /// Parent ends of fd >= 3 pipes, read by [`fd_read_end`](Child::fd_read_end) /
    /// [`fd_write_end`](Child::fd_write_end). Unix: `command-fds`-wired reactor pipes; Windows: the
    /// raw backend's overlapped async ends (empty on the std path, which routes fd >= 3 to the raw
    /// backend).
    pub(crate) pipes: FdPipes,
    /// Our-owned parent ends of piped std-slot MERGE TARGETS (the spawn pre-pass owns those
    /// pipes; tokio's internal ones cannot be shared), keyed by the target slot.
    pub(crate) owned_std: BTreeMap<Fd, super::stdio::OwnedStd>,
}

impl OsResources {
    /// The process backend. The single `expect` site: `Drop` is the only thing that empties this,
    /// and it is the last reader.
    pub(crate) fn proc_mut(&mut self) -> &mut ProcSource {
        self.proc.as_mut().expect(PROC_TAKEN)
    }
}

#[derive(Debug)]
pub struct Child {
    os: OsResources,
    id: ProcessId,
    kill_on_drop: bool,
    containment: Containment,
    graceful: crate::graceful::GracefulMechanism,
    /// The achieved elevation state, or `None` if elevation was not requested (mirrors the sync
    /// `Child`). Drives the universal-teardown kill mapping.
    elevation: Option<crate::elevation::ElevationReport>,
}

impl Child {
    // The only caller is the sibling `spawn` (and `OwnedStd` is module-scoped).
    pub(super) fn from_parts(
        proc: ProcSource,
        id: ProcessId,
        kill_on_drop: bool,
        attachment: crate::containment::Attachment,
        pipes: FdPipes,
        owned_std: BTreeMap<Fd, super::stdio::OwnedStd>,
    ) -> Child {
        Child {
            os: OsResources {
                proc: Some(proc),
                attached: attachment.attached,
                pipes,
                owned_std,
            },
            id,
            kill_on_drop,
            containment: attachment.containment,
            graceful: attachment.graceful,
            elevation: None,
        }
    }

    /// The process backend. `pub(super)`: the sibling `pump` module borrows it for
    /// `communicate`'s `wait` future, and `child::graceful` for its pid-pinning guard.
    pub(super) fn proc_mut(&mut self) -> &mut ProcSource {
        self.os.proc_mut()
    }
    /// Shared read-only accessor for the two `pins_pid` guards, which hold `&self`.
    #[cfg(windows)]
    pub(super) fn proc(&self) -> &ProcSource {
        self.os.proc.as_ref().expect(PROC_TAKEN)
    }

    /// Attach the elevation report — set by the spawn arms before the deferred password write, so
    /// a cleanup `kill` in the write-failure path already sees the elevated state.
    pub(crate) fn set_elevation(&mut self, report: Option<crate::elevation::ElevationReport>) {
        self.elevation = report;
    }
    /// The achieved elevation state, or `None` if elevation was not requested (mirrors the sync
    /// [`Child::elevation`](crate::Child::elevation)).
    pub fn elevation(&self) -> Option<crate::elevation::ElevationReport> {
        self.elevation.clone()
    }
    /// Is this a wrapper-elevated child a plain parent may be unable to signal?
    /// (`AlreadyElevated` is an ordinary child of an already-root parent — killable.)
    fn is_elevated_wrapper(&self) -> bool {
        matches!(
            self.elevation.as_ref().map(|r| &r.via),
            Some(crate::elevation::ElevatedVia::Wrapped(_) | crate::elevation::ElevatedVia::WindowsUac)
        )
    }
    /// Blocking reap used by the POSIX spawn-error cleanup path (a sync context — no reactor
    /// `await` available).
    ///
    /// **Wait-only, and the caller must already have killed successfully** — that kill is the
    /// precondition that bounds this wait. Killing again here would gate the wait on the second
    /// kill's result, and a second kill of an elevated child can be refused (it drops to root
    /// mid-teardown, and a zombie keeps the credentials it died with), so the reap would be
    /// skipped on the ordinary success path.
    ///
    /// Unix-only: the Windows elevation arm builds its child in-module with no deferred password.
    #[cfg(unix)]
    pub(super) fn wait_and_reap_blocking(&mut self) {
        let pid = self.id.pid();
        self.proc_mut().wait_and_reap(pid);
    }

    /// The child's stable identity — valid after `wait`.
    pub fn id(&self) -> ProcessId {
        self.id
    }
    pub fn is_alive(&self) -> crate::identity::Liveness {
        self.id.is_alive()
    }
    pub fn containment(&self) -> Containment {
        self.containment
    }
    /// Async mirror of [`Child::graceful_mechanism`](crate::Child::graceful_mechanism) — see
    /// there for what the value claims, and what it deliberately does not.
    pub fn graceful_mechanism(&self) -> crate::graceful::GracefulMechanism {
        self.graceful
    }

    pub fn stdin(&mut self) -> Option<super::stdio::ChildStdin> {
        if let Some(owned) = self.take_owned_in(crate::stdio::Fd::STDIN) {
            return Some(super::stdio::ChildStdin { inner: owned });
        }
        self.proc_mut().take_stdin().map(|s| super::stdio::ChildStdin {
            inner: super::stdio::InInner::Tokio(s),
        })
    }
    pub fn stdout(&mut self) -> Option<super::stdio::ChildStdout> {
        if let Some(owned) = self.take_owned_out(crate::stdio::Fd::STDOUT) {
            return Some(super::stdio::ChildStdout { inner: owned });
        }
        self.proc_mut().take_stdout().map(|s| super::stdio::ChildStdout {
            inner: super::stdio::OutInner::Stdout(s),
        })
    }
    pub fn stderr(&mut self) -> Option<super::stdio::ChildStderr> {
        if let Some(owned) = self.take_owned_out(crate::stdio::Fd::STDERR) {
            return Some(super::stdio::ChildStderr { inner: owned });
        }
        self.proc_mut().take_stderr().map(|s| super::stdio::ChildStderr {
            inner: super::stdio::OutInner::Stderr(s),
        })
    }

    /// Take the stashed our-owned read end of an Out-direction merge target (plain
    /// `BTreeMap::remove` — TAKE semantics: the first call moves the end out, later calls
    /// return `None`, matching the tokio-owned branch's `Option::take`). Unix converts the
    /// raw end to a reactor pipe here; on a conversion failure (a contract violation:
    /// debug_assert + `log::warn!`) the end drops, so the child observes EPIPE on writes —
    /// visible, never a hang.
    #[cfg(unix)]
    fn take_owned_out(&mut self, fd: Fd) -> Option<super::stdio::OutInner> {
        use std::os::fd::OwnedFd;
        match self.os.owned_std.remove(&fd)? {
            ParentEnd::Reader(r) => match ::tokio::net::unix::pipe::Receiver::from_owned_fd(OwnedFd::from(r)) {
                Ok(recv) => Some(super::stdio::OutInner::Owned(recv)),
                Err(e) => {
                    debug_assert!(false, "own pipe end failed tokio conversion: {e}");
                    log::warn!(
                        "{fd} merge-target read end dropped: tokio conversion failed ({e}); the child will see EPIPE on writes"
                    );
                    None
                }
            },
            end => {
                self.os.owned_std.insert(fd, end); // wrong direction — put it back (fd_read_end mirror)
                None
            }
        }
    }

    /// The Windows twin yields the stashed `WinOwnedRead` DIRECTLY — the `NamedPipeServer`
    /// and its connect task were created at spawn, inside the runtime, so no conversion
    /// (and no `from_raw_handle`, which would double-register the IOCP handle) exists here.
    #[cfg(windows)]
    fn take_owned_out(&mut self, fd: Fd) -> Option<super::stdio::OutInner> {
        match self.os.owned_std.remove(&fd)? {
            super::stdio::OwnedStd::Read(r) => Some(super::stdio::OutInner::Owned(r)),
            end => {
                self.os.owned_std.insert(fd, end); // wrong direction — put it back (fd_read_end mirror)
                None
            }
        }
    }

    /// Take the stashed our-owned write end of an In-direction merge target (TAKE
    /// semantics, as [`take_owned_out`](Child::take_owned_out)). On a Unix conversion
    /// failure the dropped end closes the pipe, so the child observes EOF on reads.
    #[cfg(unix)]
    fn take_owned_in(&mut self, fd: Fd) -> Option<super::stdio::InInner> {
        use std::os::fd::OwnedFd;
        match self.os.owned_std.remove(&fd)? {
            ParentEnd::Writer(w) => match ::tokio::net::unix::pipe::Sender::from_owned_fd(OwnedFd::from(w)) {
                Ok(send) => Some(super::stdio::InInner::Owned(send)),
                Err(e) => {
                    debug_assert!(false, "own pipe end failed tokio conversion: {e}");
                    log::warn!(
                        "{fd} merge-target write end dropped: tokio conversion failed ({e}); the child will see EOF on reads"
                    );
                    None
                }
            },
            end => {
                self.os.owned_std.insert(fd, end); // wrong direction — put it back (fd_write_end mirror)
                None
            }
        }
    }

    /// The Windows twin of [`take_owned_in`](Child::take_owned_in) — direct, no conversion
    /// (see [`take_owned_out`](Child::take_owned_out)).
    #[cfg(windows)]
    fn take_owned_in(&mut self, fd: Fd) -> Option<super::stdio::InInner> {
        match self.os.owned_std.remove(&fd)? {
            super::stdio::OwnedStd::Write(w) => Some(super::stdio::InInner::Owned(w)),
            end => {
                self.os.owned_std.insert(fd, end); // wrong direction — put it back (fd_write_end mirror)
                None
            }
        }
    }

    /// Take the parent's read end of the pipe on child descriptor `fd` (configured via
    /// `Command::fd(n, Stdio::pipe_out())`), as a reactor-registered pipe. Unix only.
    ///
    /// # Panics
    ///
    /// Panics outside a runtime with the IO driver enabled (the pipe registers with the
    /// reactor).
    ///
    /// # Returns
    ///
    /// `Some(receiver)` on success. `None` if the fd was not configured as a piped read end,
    /// if it was already taken, or if reactor registration failed (a contract violation:
    /// debug_assert + `log::warn!`; the dropped end closes the fd, so the child observes
    /// EPIPE on its write end — a visible failure, never a hang).
    #[cfg(unix)]
    pub fn fd_read_end(&mut self, fd: impl Into<crate::stdio::Fd>) -> Option<::tokio::net::unix::pipe::Receiver> {
        use std::os::fd::OwnedFd;
        let fd = fd.into();
        match self.os.pipes.remove(&fd)? {
            crate::child::ParentEnd::Reader(r) => {
                match ::tokio::net::unix::pipe::Receiver::from_owned_fd(OwnedFd::from(r)) {
                    Ok(recv) => Some(recv),
                    // Reactor registration failure — a contract violation for an
                    // our-own-pipe end (see docstring).
                    Err(e) => {
                        debug_assert!(false, "own pipe end failed tokio conversion: {e}");
                        log::warn!("fd {fd} read end dropped: tokio conversion failed ({e}); the child will see EPIPE on writes");
                        None
                    }
                }
            }
            end => {
                self.os.pipes.insert(fd, end); // wrong direction — put it back (sync mirror)
                None
            }
        }
    }

    /// Take the parent's write end of the pipe on child descriptor `fd` (configured via
    /// `Command::fd(n, Stdio::pipe_in())`). Unix only.
    ///
    /// # Panics
    ///
    /// Panics outside a runtime with the IO driver enabled (the pipe registers with the
    /// reactor).
    ///
    /// # Returns
    ///
    /// `Some(sender)` on success. `None` if the fd was not configured as a piped write end,
    /// if it was already taken, or if reactor registration failed (a contract violation:
    /// debug_assert + `log::warn!`; the dropped end closes the fd, so the child observes
    /// EOF on its read end — a visible failure, never a hang).
    #[cfg(unix)]
    pub fn fd_write_end(&mut self, fd: impl Into<crate::stdio::Fd>) -> Option<::tokio::net::unix::pipe::Sender> {
        use std::os::fd::OwnedFd;
        let fd = fd.into();
        match self.os.pipes.remove(&fd)? {
            crate::child::ParentEnd::Writer(w) => {
                match ::tokio::net::unix::pipe::Sender::from_owned_fd(OwnedFd::from(w)) {
                    Ok(send) => Some(send),
                    Err(e) => {
                        debug_assert!(false, "own pipe end failed tokio conversion: {e}");
                        log::warn!(
                            "fd {fd} write end dropped: tokio conversion failed ({e}); the child will see EOF on reads"
                        );
                        None
                    }
                }
            }
            end => {
                self.os.pipes.insert(fd, end);
                None
            }
        }
    }

    /// Take the parent's read end of the pipe on child descriptor `fd` (configured via
    /// `Command::fd(n, Stdio::pipe_out())`), as an async [`ChildStdout`](super::stdio::ChildStdout).
    /// The raw `CreateProcessW` backend serves fd >= 3 on Windows; the end is the overlapped
    /// named-pipe async end created at spawn (inside the runtime), yielded directly.
    ///
    /// # Returns
    ///
    /// `Some(reader)` on success. `None` if the fd was not configured as a piped read end, or was
    /// already taken. A wrong-direction take (a write end) leaves the end in place for
    /// [`fd_write_end`](Child::fd_write_end).
    #[cfg(windows)]
    pub fn fd_read_end(&mut self, fd: impl Into<Fd>) -> Option<super::stdio::ChildStdout> {
        let fd = fd.into();
        match self.os.pipes.remove(&fd)? {
            super::stdio::OwnedStd::Read(r) => Some(super::stdio::ChildStdout {
                inner: super::stdio::OutInner::Owned(r),
            }),
            end => {
                self.os.pipes.insert(fd, end); // wrong direction — put it back (Unix mirror)
                None
            }
        }
    }

    /// Take the parent's write end of the pipe on child descriptor `fd` (configured via
    /// `Command::fd(n, Stdio::pipe_in())`), as an async [`ChildStdin`](super::stdio::ChildStdin).
    /// See [`fd_read_end`](Child::fd_read_end) for the Windows raw-backend surface.
    ///
    /// # Returns
    ///
    /// `Some(writer)` on success. `None` if the fd was not configured as a piped write end, or was
    /// already taken. A wrong-direction take leaves the end in place for
    /// [`fd_read_end`](Child::fd_read_end).
    #[cfg(windows)]
    pub fn fd_write_end(&mut self, fd: impl Into<Fd>) -> Option<super::stdio::ChildStdin> {
        let fd = fd.into();
        match self.os.pipes.remove(&fd)? {
            super::stdio::OwnedStd::Write(w) => Some(super::stdio::ChildStdin {
                inner: super::stdio::InInner::Owned(w),
            }),
            end => {
                self.os.pipes.insert(fd, end); // wrong direction — put it back (Unix mirror)
                None
            }
        }
    }

    /// Test-only: whether this child is inside the crate's Job Object (`IsProcessInJob`
    /// against the held handle, not "any job"). `pub` so integration tests can call it.
    #[cfg(windows)]
    pub fn test_job_handle_contains_self(&self) -> bool {
        crate::containment::windows::job_contains_pid(&self.os.attached, self.id.pid())
    }

    /// Block until the child exits, returning its status. For a bounded wait use
    /// `tokio::time::timeout(d, child.wait())`.
    pub async fn wait(&mut self) -> Result<ExitStatus, Error> {
        self.proc_mut().wait().await
    }
    /// Exit status if the child has already exited (non-blocking).
    pub fn try_wait(&mut self) -> Result<Option<ExitStatus>, Error> {
        self.proc_mut().try_wait()
    }

    /// Hard-kill the (lone) child. Handle-bound, so it cannot race a recycled pid.
    /// `Ok(())` if the child already exited or was reaped by a prior `wait` (tokio's
    /// `start_kill` maps the reaped state to `Ok`). Signal-only: does not reap —
    /// `wait().await` (or `Drop`) collects the exit status.
    pub fn kill(&mut self) -> Result<(), Error> {
        // A plain child is unaffected (the mapping only fires on an elevated wrapper child whose
        // kill returns EPERM/ACCESS_DENIED); everything else stays `Io`/`Ok` exactly as before.
        match self.proc_mut().start_kill() {
            Err(Error::Io(e)) => Err(crate::elevation::map_elevated_kill_error(e, self.is_elevated_wrapper())),
            other => other,
        }
    }

    /// Hard-kill the contained tree. Requires an actionable containment mechanism
    /// (errors `Unsupported` otherwise — use [`kill`](Child::kill) for a lone process).
    /// If both the group teardown and the handle backstop fail, the group error is returned.
    ///
    /// On the Unix process-group and session mechanisms this returns
    /// [`Error::Containment`](crate::error::Error::Containment) when a live member of the
    /// group refused the signal — a setuid binary in the tree is the ordinary cause. The
    /// tree is still running and this process cannot bring it down.
    ///
    /// **This guarantee, and its converse — that `Ok` is positive proof the group cleared —
    /// hold only for the `ProcessGroup`/`Session` mechanisms**, not `TreeWalk`: a separate,
    /// unfixed gap means `TreeWalk` does not yet propagate a live refuser's outcome into this
    /// call's result.
    ///
    /// **A `hidepid`-restricted Linux host can still return `Ok` with a live refuser left
    /// running.** `/proc` is this mechanism's only way to confirm the group cleared, and
    /// `hidepid=invisible`/`hidepid=2` hides a foreign-uid process from it entirely — the
    /// ordinary setuid-in-a-container case. That member is then never listed, never
    /// classified, never signaled, and the group can report cleared regardless. No fix
    /// exists within this mechanism: the pid is never learned, and `killpg`'s own return
    /// value is not trustworthy evidence either.
    pub fn kill_tree(&mut self) -> Result<(), Error> {
        self.require_contained()?;
        // Precondition (a separate, unfixed gap — asserted, not fixed, here): see the sync
        // twin, `Child::kill_tree` in `src/child.rs`, for the full rationale (including which
        // mechanisms `carries_recyclable_pgid` covers, and why this is `#[cfg(unix)]`).
        #[cfg(unix)]
        debug_assert!(
            !self.os.attached.carries_recyclable_pgid() || {
                let now = ProcessId::of(self.id.pid());
                let now_liveness = match now {
                    crate::identity::Resolved::Found(id) => id.is_alive(),
                    crate::identity::Resolved::Gone | crate::identity::Resolved::Unknown => {
                        crate::identity::Liveness::Unknown
                    }
                };
                !crate::child::root_pid_was_recycled(self.id, now, now_liveness)
            },
            "kill_tree/terminate_tree called after the contained root's pid ({}) was reaped and \
             recycled onto a different, live process; a pgid-based mechanism would now signal an \
             unrelated process group",
            self.id.pid()
        );
        let group_result = self.os.attached.hard_kill();
        // Backstop for the TreeWalk mechanism: its hard_kill kills the root by identity, which
        // no-ops if `ProcessId::of` transiently fails to resolve — this handle-based kill
        // covers that, so its failure is contract-relevant.
        let backstop = self.kill();
        // Both-fail: the group error is surfaced; subsuming the backstop's is deliberate.
        if let (Err(group), Err(bs)) = (&group_result, &backstop) {
            log::debug!("kill_tree handle backstop also failed ({bs}); surfacing the group error: {group}");
        }
        group_result.and(backstop)
    }

    /// Send the graceful termination signal to the contained group — `SIGTERM` via
    /// `killpg`/cgroup, or `CTRL_BREAK` to the job/console group. **Signal-only:** does
    /// not wait or reap. Requires an actionable containment mechanism (errors
    /// `Unsupported` otherwise). Cooperative best-effort: on the `TreeWalk` mechanism a
    /// descendant whose identity transiently fails to resolve is intentionally left
    /// unsignaled; [`kill_tree`](Child::kill_tree) is the guaranteed hard teardown.
    ///
    /// **Windows: what this actually signals.** `CTRL_BREAK` is delivered to the root's
    /// **process group**, not to the tree. A nested contained descendant leads its own
    /// group and never receives it, so from THIS handle only
    /// [`kill_tree`](Child::kill_tree) reaches every member. The layers this skips are not
    /// beyond a polite shutdown, though: the holder of a nested descendant's own `Child` can
    /// drain it with [`terminate`](Child::terminate) or
    /// [`graceful_shutdown`](Child::graceful_shutdown) before this root is torn down, and a
    /// chain in which each level shuts down its own children drains completely, because a
    /// child that owns a console can politely signal its own group-leading children.
    ///
    /// **And success here does not prove the event was delivered.** A root that shares no
    /// console with the caller is reported as success and reaches nobody — including a root
    /// spawned with [`no_window`](crate::Command::no_window) or
    /// `detached()`, which gets a console of its own. Such a root is
    /// reported as [`GracefulMechanism::OtherConsoleGroup`](crate::GracefulMechanism::OtherConsoleGroup)
    /// by [`graceful_mechanism`](Child::graceful_mechanism): that is what cosca recorded about
    /// the *route* from this process, never an authority on whether a signal will arrive. It is
    /// also not "this child cannot be shut down politely" — a process attached to the child's own
    /// console can deliver the event. The cooperative op returns `Ok` and delivers nothing; the
    /// forced ops ([`kill`](Child::kill) / [`kill_tree`](Child::kill_tree), and the escalation
    /// half of [`graceful_shutdown_tree`](Child::graceful_shutdown_tree)) are unaffected.
    ///
    /// **And it needs the caller to have a console.** The event is deliverable only within
    /// the *calling* process's console, so a GUI-subsystem binary, a service, or anything
    /// spawned detached cannot deliver it. The failure is classified best-effort: usually
    /// [`Error::NoConsole`](crate::error::Error::NoConsole), but a raw `Error::Io` when the
    /// crate cannot confirm the cause. Treat **any** error here as "no signal was sent, the
    /// tree is still running" rather than keying a fallback on the variant alone. Attach a
    /// console before spawning the tree, or use `kill_tree`, which needs none.
    ///
    /// On the Unix process-group and session mechanisms this returns
    /// [`Error::Containment`](crate::error::Error::Containment) when a live member of the
    /// group refused the signal — a setuid binary in the tree is the ordinary cause. The
    /// tree is still running and this process cannot bring it down.
    ///
    /// See [`kill_tree`](Child::kill_tree)'s doc for two things that also apply here: the
    /// `ProcessGroup`/`Session`-only scope of this guarantee (a separate, unfixed gap for
    /// `TreeWalk`), and the residual `hidepid` gap on Linux.
    ///
    /// **Windows, after the root's exit has been observed.** The event is addressed by the
    /// ROOT's pid — on the job-object mechanism and the tree walk alike — and this handle stops
    /// pinning that pid the moment `wait`/`try_wait` reports the exit, so the OS may reissue it
    /// to an unrelated group leader. A root exiting while its descendants are still alive is an
    /// ordinary flow, so this is refused from that point
    /// ([`Error::Unassessable`](crate::error::Error::Unassessable)) rather than fired at a bare
    /// pid; [`kill_tree`](Child::kill_tree) addresses no pid and still reaches the survivors.
    /// The sync [`Child`](crate::Child) pins for its whole life and is unaffected.
    pub fn terminate_tree(&self) -> Result<(), Error> {
        self.require_contained()?;
        // After the mechanism guard, which is permanent and pid-independent: an uncontained
        // child must keep hearing why it has no tree to signal, not why a pid is unpinned.
        #[cfg(windows)]
        if !self.proc().pins_pid() {
            return Err(self.unpinned_pid_refusal("terminate_tree"));
        }
        // See kill_tree's identical precondition assert for the full rationale, including the
        // `#[cfg(unix)]` gate (`carries_recyclable_pgid` does not exist on Windows).
        #[cfg(unix)]
        debug_assert!(
            !self.os.attached.carries_recyclable_pgid() || {
                let now = ProcessId::of(self.id.pid());
                let now_liveness = match now {
                    crate::identity::Resolved::Found(id) => id.is_alive(),
                    crate::identity::Resolved::Gone | crate::identity::Resolved::Unknown => {
                        crate::identity::Liveness::Unknown
                    }
                };
                !crate::child::root_pid_was_recycled(self.id, now, now_liveness)
            },
            "kill_tree/terminate_tree called after the contained root's pid ({}) was reaped and \
             recycled onto a different, live process; a pgid-based mechanism would now signal an \
             unrelated process group",
            self.id.pid()
        );
        self.os.attached.terminate(self.id.pid())
    }

    /// The refusal both cooperative ops answer with once this handle has stopped pinning the
    /// child's pid — the async backend releases the Windows process handle when `wait`/`try_wait`
    /// observes the exit. Shared so the lone and the tree op cannot drift on what is, for both,
    /// the same hazard: a console control event carries a bare pid and nothing else.
    #[cfg(windows)]
    fn unpinned_pid_refusal(&self, op: &str) -> Error {
        Error::Unassessable {
            detail: format!(
                "this handle no longer pins pid {pid}: the async backend released the child's \
                 process handle when its exit was observed, so the pid may since name an \
                 unrelated process. A console control event is addressed by pid alone, so {op}() \
                 sent nothing. The child itself is already gone; kill_tree() addresses no pid and \
                 still tears down any survivors.",
                pid = self.id.pid()
            ),
            source: None,
        }
    }

    /// Guard for the `_tree` operations (single-sourced with the sync `Child`).
    fn require_contained(&self) -> Result<(), Error> {
        crate::containment::require_contained(self.containment, &self.os.attached)
    }

    /// Guard for `wait_tree`/`wait_tree_timeout` (single-sourced with the sync `Child`).
    fn require_drainable(&self) -> Result<(), Error> {
        crate::containment::require_drainable(self.containment, &self.os.attached)
    }

    /// Block until every member of the contained tree has EXITED — not reaped; a status is
    /// never collected by this call, only the root's own `wait`/`try_wait` does that. Requires
    /// a mechanism with a real kernel drain edge (`Unsupported` otherwise — cgroup v2, a
    /// Windows job object, and the macOS fd marker have one; `ProcessGroup`/`Session`/
    /// `TreeWalk` and an uncontained or nested-`Delegated` child do not). Reactor-native on
    /// Linux and macOS (no polling interval); Windows hands the wait to `spawn_blocking` (job
    /// objects have no pollable handle) with a cancel event so a dropped future releases the
    /// blocking watcher promptly instead of parking out the wait.
    pub async fn wait_tree(&self) -> Result<crate::containment::TreeDrain, Error> {
        self.require_drainable()?;
        super::wait::wait_tree_drained_dispatch(&self.os.attached, None).await
    }

    /// Like [`wait_tree`](Child::wait_tree) but bounded by `timeout`.
    /// `TreeDrain::MembersRemain` at expiry is not an error. A `timeout` so large it would
    /// overflow `Instant` is treated as unbounded, matching [`wait_tree`](Child::wait_tree).
    pub async fn wait_tree_timeout(
        &self,
        timeout: std::time::Duration,
    ) -> Result<crate::containment::TreeDrain, Error> {
        self.require_drainable()?;
        let deadline = crate::wait::deadline_from(timeout);
        super::wait::wait_tree_drained_dispatch(&self.os.attached, deadline).await
    }
}

impl Child {
    /// Leave the child (and its contained tree) running after this handle drops.
    pub fn detach(&mut self) {
        self.kill_on_drop = false;
        self.os.attached.disarm();
    }
}

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

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

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

#[cfg(all(test, windows))]
impl Child {
    /// Test-only: install the per-instance raw-wait observer on this child (see the raw backend's
    /// `WaitObserver`), forwarded to the backend.
    pub(crate) fn install_wait_observer(
        &mut self,
        started: ::tokio::sync::oneshot::Sender<()>,
        outcome: ::tokio::sync::oneshot::Sender<crate::child::spawn::windows_raw::WaitOutcome>,
    ) {
        self.proc_mut().install_wait_observer(started, outcome);
    }
}

/// Signals the tree and the root and does NOT wait: the child is not necessarily gone when
/// `drop` returns, and neither is it necessarily reaped. A caller that must observe the teardown
/// calls [`kill`](Child::kill)/[`kill_tree`](Child::kill_tree) and then awaits
/// [`wait`](Child::wait)/[`wait_tree`](Child::wait_tree).
///
/// The reap is handed to a small fixed pool of reaper threads (currently two) that starts on
/// the first kill-on-drop drop and persists for the process's life. It is guaranteed except on
/// the two paths below, both of which are loud. Under thread exhaustion the pool cannot start,
/// and the child goes to the runtime's orphan handling instead — an `error` per child, and each
/// drop then costs at most that many failing spawn syscalls.
///
/// # Known limitation: `fork()` without `exec`
///
/// **A process that forks after this pool has started, and then keeps running Rust code in the
/// child, loses the reap silently in that child.** `fork` duplicates only the calling thread, so
/// the child inherits the pool's job queue with none of its workers; sends into it still succeed
/// (the receiver handles exist in the copied address space), so nothing errors and nothing is
/// logged, and every child handle dropped there queues forever, pinning its process handle,
/// containment resource and stdio for the life of that process. It affects daemonizing and
/// pre-fork worker models. It does **not** affect ordinary subprocess spawning — `fork`+`exec`
/// and `posix_spawn` replace the child image immediately — nor Windows, which has no `fork`.
///
/// Until it is fixed, a forking consumer should either fork before its first kill-on-drop drop,
/// or `kill` and `await` [`wait`](Child::wait) explicitly in the forked child rather than relying
/// on `Drop`. The sync [`Child`](crate::Child) is unaffected: it reaps on the dropping thread.
///
/// That divergence from the sync `Child`, which still blocks, is otherwise deliberate.
impl Drop for Child {
    fn drop(&mut self) {
        // The opt-out/`detach()` contract: nothing is signalled and nothing is logged.
        if !self.kill_on_drop {
            return;
        }
        #[cfg(test)]
        let probe = reaper::test_probe::take();
        #[cfg(test)]
        if let Some(p) = probe.as_ref() {
            let _ = p.entered.send(std::thread::current().id());
        }
        // Tree teardown — the SOLE coverage for descendants (the root's own kill below reaches
        // only the root), so surface a real mechanism failure in debug. A no-op for an
        // uncontained child.
        //
        // MUST stay on the dropping thread, before the handle is dismembered: on Windows a job
        // object's kill is the only signal reaching a nested descendant that leads its own console
        // group (console control events stop at that boundary). On Unix this and `terminate_tree`
        // have the same radius. The contract either way: the tree is signalled before `drop`
        // returns.
        let tree = self.os.attached.hard_kill();
        if let Err(e) = &tree {
            debug_assert!(
                !crate::child::is_teardown_mechanism_failure(e),
                "contained-tree teardown failed on async Drop: {e:?}"
            );
            log::warn!("Child::drop: contained-tree teardown did not fully succeed: {e}");
        }
        let _ = tree;

        let pid = self.id.pid();
        // The WHOLE resource group moves, so a field added to `OsResources` is carried here
        // without touching this function. On every early return below it drops in group order,
        // on this thread — exactly where it dropped before the reap moved off it.
        let mut os = std::mem::take(&mut self.os);
        // Already reaped: no signal to issue and no exit to wait for.
        if os.proc_mut().is_reaped() {
            return;
        }
        // No `debug_assert` here: a failed kill is a designed outcome the branch below serves (a
        // higher-integrity elevated child), and asserting would panic inside a destructor.
        //
        // The test seam REPLACES the kill rather than masking its result — a masked kill would
        // still have signalled the child, and the branch below is about one that was not.
        #[cfg(test)]
        let killed = if reaper::fault::take_force_kill_failure() {
            Err(Error::Io(std::io::Error::other("forced kill failure (test seam)")))
        } else {
            os.proc_mut().start_kill()
        };
        #[cfg(not(test))]
        let killed = os.proc_mut().start_kill();
        if killed.is_err() {
            if !matches!(os.proc_mut().try_wait(), Ok(Some(_))) {
                log::warn!("async child {pid} could not be terminated on drop; leaving it running");
            }
            // An unsignalled child is never submitted: its wait is unbounded, and a worker parked
            // on it would never come back. Its resources release with this handle instead.
            return;
        }
        reaper::submit(reaper::ReapJob {
            os,
            pid,
            #[cfg(test)]
            origin: std::thread::current().id(),
            #[cfg(test)]
            probe,
            #[cfg(test)]
            force_panic: false,
            #[cfg(test)]
            force_release_panic: false,
            #[cfg(test)]
            force_glue_panic: false,
        });
    }
}

/// Guaranteed synchronous teardown for `Drop`: kill the child, then block until it has exited.
/// The kill here is what bounds the wait, so a caller that has ALREADY killed must use
/// [`wait_and_reap`] instead — re-killing would make its reap conditional on a second kill that
/// can be refused.
/// **Invariant:** no `wait()` future for this child is in flight when this runs.
pub(crate) fn reap_now(child: &mut ::tokio::process::Child, pid: u32, done_ok: bool) {
    // `start_kill` bounds the wait below — it MUST run in release (NOT inside `debug_assert!`,
    // whose argument is stripped in release). A no-op on an already-exited child.
    let killed = child.start_kill();
    debug_assert!(killed.is_ok(), "start_kill of an owned child should not fail");
    // A failed start_kill means this is not a live process to wait on (ESRCH = already exited;
    // EPERM is impossible for our own child) — skip, so a kill failure can never turn the bounded
    // exit-wait into an unbounded block. tokio's field-drop reaps any leftover zombie.
    if killed.is_err() {
        return;
    }
    wait_and_reap(child, pid, done_ok);
}

/// The wait-then-reap half of [`reap_now`], with **no kill of its own**: the caller's own
/// successful kill is the precondition that bounds this wait.
///
/// On Unix we wait with `WNOWAIT` (NOT reaping), so tokio's own `Child` field-drop reaps the
/// zombie synchronously in its drop (its `try_wait` returns `Ok(Some)`, not a park-dependent
/// orphan enqueue) — a guaranteed reap before the handle is gone. We only wait while tokio still
/// owns the child (`id().is_some()`), which pins the pid; once tokio is `Done` (a prior `wait()`
/// reaped it), the pid may be recycled and we must not wait on it. `done_ok` says whether an
/// already-`Done` child is legal here: `true` for `Drop` (the user may have `wait()`ed), `false`
/// for a caller whose child was never awaited.
/// **Invariant:** no `wait()` future for this child is in flight when this runs.
pub(crate) fn wait_and_reap(child: &mut ::tokio::process::Child, pid: u32, done_ok: bool) {
    // tokio `Done` ⇒ already reaped, pid possibly recycled ⇒ nothing to do (the recycled-pid wait
    // hazard the sync side avoids by holding a handle).
    if child.id().is_none() {
        debug_assert!(
            done_ok,
            "wait_and_reap found an already-reaped child where one was impossible"
        );
        return;
    }
    #[cfg(unix)]
    {
        // `nix` doesn't expose `waitid` on macOS (0.31 configures it out), so call `libc::waitid`
        // directly (WEXITED | WNOWAIT: block until exit without reaping) — portable across every Unix
        // target and the identical syscall.
        debug_assert!(pid <= i32::MAX as u32, "pid {pid} exceeds i32::MAX");
        let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
        loop {
            // SAFETY: a well-formed `waitid` call; `info` is a valid, owned, zeroed `siginfo_t` the
            // kernel fills in. WNOWAIT leaves the child reapable for tokio's in-drop reap.
            let rc = unsafe { libc::waitid(libc::P_PID, pid as libc::id_t, &mut info, libc::WEXITED | libc::WNOWAIT) };
            if rc == 0 {
                break;
            }
            let err = std::io::Error::last_os_error();
            if err.raw_os_error() == Some(libc::EINTR) {
                continue;
            }
            // id() was Some above (tokio un-reaped ⇒ pid pinned), so no ECHILD / other errno should
            // occur — a debug tripwire, with a safe release `break`.
            debug_assert!(false, "waitid in wait_and_reap failed unexpectedly: {err}");
            break;
        }
    }
    #[cfg(windows)]
    {
        use windows::Win32::Foundation::{HANDLE, WAIT_OBJECT_0};
        use windows::Win32::System::Threading::{WaitForSingleObject, INFINITE};
        let _ = pid;
        let h = child.raw_handle().expect("tokio owns the handle while id() is Some");
        // SAFETY: tokio owns and (on its field-drop) closes the handle; we only wait on it.
        // INFINITE is bounded by the kill the caller already issued.
        let waited = unsafe { WaitForSingleObject(HANDLE(h), INFINITE) };
        debug_assert!(
            waited == WAIT_OBJECT_0,
            "wait_and_reap did not observe the child's exit: {waited:?}"
        );
    }
}