cosca 0.9.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! A tree's own kernel edge for "no member runs" ([`DrainEdge`]), and the per-OS drivers that await
//! it. The drivers use the async waits' primitives (`crate::tokio::wait`'s clock, its armed
//! deadline and its readiness loop), which know nothing of a tree.

use std::time::Instant;

use crate::containment::tree_kill::Settled;
use crate::containment::TreeDrain;
use crate::error::Error;

/// A tree's own kernel edge for "every member has exited", borrowed from the tree that owns the
/// resource it watches for the child's whole life (a marker's read end must, to keep its pipe's
/// identity from being reissued). Both waits report the [`TreeDrain`] verdict.
pub(crate) enum DrainEdge<'a> {
    /// The leaf's own watch of `cgroup.events`.
    #[cfg(target_os = "linux")]
    Cgroup(&'a crate::containment::cgroup::CgroupLeaf),
    /// The job's live process list.
    #[cfg(windows)]
    Job(&'a crate::containment::windows::JobHandle),
    /// The marker pipe's read end, which reads EOF once the last holder of the write end is gone.
    #[cfg(target_os = "macos")]
    Marker(&'a crate::containment::fdmarker::Marker),
}

impl DrainEdge<'_> {
    /// Block until every member of the tree has EXITED (not reaped), or until `deadline` (`None`: for
    /// ever): `MembersRemain` at expiry.
    pub(crate) fn wait(&self, deadline: Option<Instant>) -> Result<TreeDrain, Error> {
        self.wait_past(deadline, &Settled::default())
    }

    /// [`wait`](Self::wait) for a caller that has already waited for the members `settled`, each of which
    /// exited or failed in a way it reported. Only a Job's edge, which confirms an empty listing from a
    /// system snapshot of its members, has anything to skip: it neither waits for them nor reports them
    /// again.
    #[cfg_attr(
        not(windows),
        allow(unused_variables, reason = "only a Job's edge reads what a wait settled")
    )]
    pub(crate) fn wait_past(&self, deadline: Option<Instant>, settled: &Settled) -> Result<TreeDrain, Error> {
        match self {
            #[cfg(target_os = "linux")]
            DrainEdge::Cgroup(leaf) => leaf.wait_drained(deadline),
            #[cfg(windows)]
            DrainEdge::Job(job) => job.wait_drained_past(deadline, settled),
            #[cfg(target_os = "macos")]
            DrainEdge::Marker(m) => m.wait_drained(deadline),
        }
    }

    /// [`wait`](Self::wait), awaited, with no thread blocked: macOS is reactor-native (`AsyncFd`);
    /// Linux awaits a broadcast from the pump thread its leaf owns (an `event_listener` future, no
    /// reactor registration); Windows awaits registered waits on the job's members (job objects
    /// have no pollable handle). `deadline` is on tokio's clock
    /// ([`deadline_from`](crate::tokio::wait::deadline_from)).
    #[cfg(feature = "tokio")]
    pub(crate) async fn wait_async(&self, deadline: Option<Instant>) -> Result<TreeDrain, Error> {
        self.wait_async_past(deadline, &Settled::default()).await
    }

    /// [`wait_past`](Self::wait_past), awaited.
    #[cfg(feature = "tokio")]
    #[cfg_attr(
        not(windows),
        allow(unused_variables, reason = "only a Job's edge reads what a wait settled")
    )]
    pub(crate) async fn wait_async_past(
        &self,
        deadline: Option<Instant>,
        settled: &Settled,
    ) -> Result<TreeDrain, Error> {
        match self {
            #[cfg(target_os = "linux")]
            DrainEdge::Cgroup(leaf) => cgroup_wait_tree_drained(leaf, deadline).await,
            #[cfg(windows)]
            DrainEdge::Job(job) => job_wait_tree_drained(job, deadline, settled).await,
            #[cfg(target_os = "macos")]
            DrainEdge::Marker(m) => {
                // Same reissued-fd check as `Marker::wait_drained`: a reissued read-end fd would
                // deliver a false `EV_EOF`.
                m.check_read_end_still_valid()?;
                debug_assert_ne!(
                    crate::containment::marker_eof::write_end_check(m.read_end()),
                    crate::containment::marker_eof::WriteEndCheck::HeldByUs,
                    "the supervisor still holds a copy of the marker write end - the tree-drain edge can never fire"
                );
                wait_tree_deadline(m.read_end(), deadline).await
            }
        }
    }
}

// The per-OS drivers -----

/// Resolve when every holder of the marker's write end has exited: poll-free, no internal deadline.
/// It never drains a sustained writer on its behalf (see `marker_eof`'s module doc); bound the
/// caller's patience with a deadline instant.
///
/// Exited is not reaped: this says nothing about statuses. A caller wanting a status waits on
/// the root as well.
///
/// The marker's own kqueue is what gets registered with the reactor, not the marker
/// descriptor: a knote is keyed on `(kqueue, fd, filter)`, so a second waiter registering the
/// same descriptor directly would take over the first's registration and park it forever —
/// each call arms its OWN private kqueue (`marker_eof::arm`).
#[cfg(all(feature = "tokio", target_os = "macos"))]
pub(crate) async fn wait_tree_drained(read_end: std::os::fd::BorrowedFd<'_>) -> Result<(), Error> {
    wait_tree_drained_watched(read_end, None).await
}

/// Deadline-bounded, [`TreeDrain`]-returning wrapper over [`wait_tree_drained_inner`] — the macOS
/// arm of `tokio::Child::wait_tree`/`wait_tree_timeout`. `Duration::ZERO` delegates to the sync
/// backend's one-shot, non-blocking probe (safe to call directly from async code).
///
/// The bounded arm arms with `unbounded_wait: false`, as the sync `block_until_drained` does for a
/// real deadline; `wait_tree_drained` is always unbounded.
#[cfg(all(feature = "tokio", target_os = "macos"))]
pub(crate) async fn wait_tree_deadline(
    read_end: std::os::fd::BorrowedFd<'_>,
    deadline: Option<Instant>,
) -> Result<TreeDrain, Error> {
    use crate::tokio::wait::{arm_at, tokio_now};
    match deadline {
        None => {
            wait_tree_drained(read_end).await?;
            Ok(TreeDrain::AllMarkersClosed)
        }
        Some(at) => {
            if crate::wait::remaining_at(deadline, tokio_now()) == Some(std::time::Duration::ZERO) {
                return crate::containment::marker_eof::probe(read_end);
            }
            match arm_at(at, wait_tree_drained_inner(read_end, false, None)).await {
                Some(res) => res.map(|()| TreeDrain::AllMarkersClosed),
                // One final probe, so an EOF already pending is not missed.
                None => crate::containment::marker_eof::probe(read_end),
            }
        }
    }
}

/// Test seam: fires every time [`wait_tree_drained_inner`]'s watch loop gets
/// `DrainOutcome::Declined` (a genuine non-EOF event, retrieved and interpreted, that did not
/// resolve the wait; its bytes were discarded when `suppress_drain` is false and left buffered
/// when true). Never `Spurious`. A `#[cfg(test)]` thread-local installed by an RAII guard, so no
/// production signature carries it. It relies on a current-thread runtime: the future is polled on
/// the installing thread.
#[cfg(all(test, feature = "tokio", target_os = "macos"))]
pub(crate) mod declined_hook {
    use std::cell::RefCell;

    use ::tokio::sync::mpsc::UnboundedSender;

    thread_local! {
        static DECLINED: RefCell<Option<UnboundedSender<()>>> = const { RefCell::new(None) };
    }

    /// Fire this thread's installed hook, if any. Called only from
    /// [`super::wait_tree_drained_inner`]'s `DrainOutcome::Declined` arm.
    pub(crate) fn notify() {
        DECLINED.with(|d| {
            if let Some(tx) = d.borrow().as_ref() {
                let sent = tx.send(());
                debug_assert!(sent.is_ok(), "declined_hook receiver dropped before the hook fired");
            }
        });
    }

    /// RAII installer: clears the hook on `Drop`, including during unwinding.
    #[must_use]
    pub(crate) struct DeclinedGuard {
        _private: (),
    }

    impl DeclinedGuard {
        pub(crate) fn install(tx: UnboundedSender<()>) -> Self {
            DECLINED.with(|d| {
                let mut slot = d.borrow_mut();
                debug_assert!(slot.is_none(), "a declined_hook is already installed on this thread");
                *slot = Some(tx);
            });
            Self { _private: () }
        }
    }

    impl Drop for DeclinedGuard {
        fn drop(&mut self) {
            DECLINED.with(|d| {
                let prev = d.borrow_mut().take();
                debug_assert!(
                    prev.is_some(),
                    "declined_hook slot was cleared before its guard dropped"
                );
            });
        }
    }
}

/// Test-only entry point that reports the instant its kqueue is armed, on a channel the
/// CALLER owns — no shared/global observer state, so concurrently-running tests (this file
/// has four) cannot steal each other's notification.
#[cfg(all(test, feature = "tokio", target_os = "macos"))]
pub(crate) async fn wait_tree_drained_for_test(
    read_end: std::os::fd::BorrowedFd<'_>,
    armed: std::sync::mpsc::Sender<std::os::fd::RawFd>,
) -> Result<(), Error> {
    wait_tree_drained_watched(read_end, Some(armed)).await
}

/// The ONE call site for [`wait_tree_drained`]'s own "always unbounded" choice — shared with
/// [`wait_tree_drained_for_test`].
#[cfg(all(feature = "tokio", target_os = "macos"))]
async fn wait_tree_drained_watched(
    read_end: std::os::fd::BorrowedFd<'_>,
    armed: Option<std::sync::mpsc::Sender<std::os::fd::RawFd>>,
) -> Result<(), Error> {
    wait_tree_drained_inner(read_end, true, armed).await
}

/// `unbounded_wait` must be the SAME expression the sync backend's `block_until_drained` uses
/// (`crate::wait::remaining(deadline).is_none()`) for whichever deadline the caller is honoring,
/// threaded through to both `arm` and `drain_kqueue`.
///
/// `armed`, if given, receives the raw fd of the kqueue this call just armed (test-only; see
/// [`wait_tree_drained_for_test`]). That fd is valid only while this future is alive.
/// `DrainOutcome::Declined` fires [`declined_hook::notify`] in test builds only.
#[cfg(all(feature = "tokio", target_os = "macos"))]
async fn wait_tree_drained_inner(
    read_end: std::os::fd::BorrowedFd<'_>,
    unbounded_wait: bool,
    armed: Option<std::sync::mpsc::Sender<std::os::fd::RawFd>>,
) -> Result<(), Error> {
    use std::os::fd::AsRawFd;

    use crate::containment::marker_eof::DrainOutcome;
    use crate::tokio::wait::watch_readable;
    use ::tokio::io::unix::AsyncFd;
    use ::tokio::io::Interest;
    let kq = crate::containment::marker_eof::arm(read_end, unbounded_wait)?;
    if let Some(tx) = armed {
        let sent = tx.send(kq.as_raw_fd());
        debug_assert!(
            sent.is_ok(),
            "the `armed` receiver was dropped before the kqueue was armed"
        );
    }
    let afd = AsyncFd::with_interest(kq, Interest::READABLE).map_err(Error::Io)?;
    watch_readable(&afd, move |kq| {
        match crate::containment::marker_eof::drain_kqueue(kq, read_end, unbounded_wait)? {
            DrainOutcome::Drained(_) => Ok(Some(())),
            DrainOutcome::Declined => {
                #[cfg(test)]
                declined_hook::notify();
                Ok(None)
            }
            DrainOutcome::Spurious => Ok(None),
        }
    })
    .await
}

/// Resolve when every process in the cgroup v2 leaf has EXITED (not reaped), or until `deadline`.
/// The async twin of `CgroupLeaf::wait_drained`, taking the same `CgroupLeaf::drain_step`s and
/// awaiting each broadcast where the sync wait blocks on it. It never touches the watch itself, so
/// a future dropped, or never polled again, holds nothing another wait needs. Each round awaits
/// for exactly the caller's own remaining time; no interval anywhere.
#[cfg(all(feature = "tokio", target_os = "linux"))]
pub(crate) async fn cgroup_wait_tree_drained(
    leaf: &crate::containment::cgroup::CgroupLeaf,
    deadline: Option<Instant>,
) -> Result<TreeDrain, Error> {
    use crate::containment::cgroup::DrainStep;
    use crate::tokio::wait::{arm_at, tokio_now};

    loop {
        match leaf.drain_step_on(deadline, tokio_now)? {
            DrainStep::Done(drain) => return Ok(drain),
            DrainStep::Block {
                listener,
                deadline: None,
            } => {
                #[cfg(test)]
                crate::wait::block_probe::blocking(None, crate::wait::block_probe::WaitObject::DrainAwait);
                #[cfg(test)]
                crate::tokio::wait::armed_deadline_seam::notify(
                    crate::tokio::wait::armed_deadline_seam::Armed::Unbounded,
                );
                listener.await
            }
            // The timeout is looked at by the next step, which reads the leaf once more.
            DrainStep::Block {
                listener,
                deadline: Some(at),
            } => {
                #[cfg(test)]
                crate::wait::block_probe::blocking(
                    Some(at.saturating_duration_since(tokio_now())),
                    crate::wait::block_probe::WaitObject::DrainAwait,
                );
                let _ = arm_at(at, listener).await;
            }
        }
    }
}

/// The members of `job` a system snapshot names ([`unsignalled_members_of`]), found on tokio's
/// blocking pool: the scan opens every process on the system, which a runtime thread must not do.
/// The task owns a duplicate of the job's handle, because it can outlive the future that asked for
/// it. The scan is bounded work that waits for no exit, as the pool is for.
#[cfg(all(feature = "tokio", windows))]
async fn snapshot_members_on_pool(
    job: &crate::containment::windows::JobHandle,
) -> Result<Vec<crate::containment::windows::Found>, Error> {
    use crate::containment::windows::unsignalled_members_of;
    use std::os::windows::io::AsHandle as _;

    let held = job.handle().try_clone_to_owned().map_err(|e| Error::Unassessable {
        detail: "the job's handle could not be duplicated for the snapshot of its members".to_owned(),
        source: Some(Error::io_cause(e)),
    })?;
    crate::tokio::wait::on_blocking_pool("the snapshot of a job's members", move || {
        unsignalled_members_of(held.as_handle())
    })
    .await
    .map_err(|unjoined| {
        use crate::tokio::wait::Unjoined;

        let (detail, cause) = match unjoined {
            Unjoined::NotRun(cause) => (
                "the snapshot of the job's members did not run, so their exit cannot be observed",
                cause,
            ),
            Unjoined::Unknown(cause) => (
                "nothing can be said of what the snapshot of the job's members did, so their exit cannot be observed",
                cause,
            ),
        };
        Error::Unassessable {
            detail: detail.to_owned(),
            source: Some(std::sync::Arc::new(cause)),
        }
    })?
}

/// Resolve when every process in the Windows job has EXITED (not reaped), or until `deadline`.
/// Job objects expose no pollable handle, so — unlike the macOS arm, on the reactor, and the Linux
/// arm, awaiting its leaf's pump — this runs the sync wait's own rounds
/// ([`JobHandle::drain_round`](crate::containment::windows::JobHandle::drain_round)) and awaits each
/// round's member handles as registered waits where the sync wait blocks on them. The deadline is
/// the registrations' own timeout (see [`registered_wait`](crate::os_process::windows::registered_wait));
/// dropping the future unregisters the round's waits.
///
/// An empty listing is confirmed from a system snapshot, as the sync wait's `confirm_drained` does:
/// each job member it names, except those `settled`, is awaited in turn through the by-identity wait
/// a kill's wait uses.
///
/// The round's own work (up to two listings and the opens of the pids they name) runs on the polling
/// thread: it waits for no exit and is bounded. The snapshot of an empty listing, with one open per
/// process on the system, runs on the blocking pool ([`snapshot_members_on_pool`]).
#[cfg(all(feature = "tokio", windows))]
async fn job_wait_tree_drained(
    job: &crate::containment::windows::JobHandle,
    deadline: Option<Instant>,
    settled: &Settled,
) -> Result<TreeDrain, Error> {
    use crate::containment::tree_kill::wait_for_member_async;
    use crate::containment::windows::{ConfirmStep, ConfirmWalk, Ended, Round};
    use crate::identity::HostKernel;
    use crate::os_process::windows::registered_wait::{wait_any, Woke};

    // The registered waits run on the real clock, not tokio's.
    let deadline = crate::tokio::wait::to_real_clock(deadline);
    loop {
        // A round that starts past the deadline is the last, as in the sync wait.
        let last_round = deadline.is_some_and(|at| crate::wait::now() >= at);
        let round = match job.drain_round(last_round)? {
            Round::Watch(watch) => {
                let ended = if last_round {
                    watch.poll()?
                } else {
                    match wait_any(watch.handles(), deadline).await.map_err(Error::Io)? {
                        Woke::Signalled => Ended::Signalled,
                        // The deadline passed first: one look at the handles, as the sync wait takes.
                        Woke::TimedOut => watch.poll()?,
                    }
                };
                match ended {
                    Ended::Deadline => return Ok(TreeDrain::MembersRemain),
                    Ended::Signalled => job.after_signalled(&watch)?,
                }
            }
            round => round,
        };
        match round {
            Round::Answer(drain) => return Ok(drain),
            Round::Confirm => {
                let Some(count) = job.count_to_confirm()? else {
                    return Ok(TreeDrain::AllMembersExited);
                };
                let mut walk = ConfirmWalk::new(job, count, snapshot_members_on_pool(job).await, settled);
                loop {
                    match walk.next() {
                        ConfirmStep::Wait(id) => {
                            walk.waited(id, wait_for_member_async(&HostKernel, id, deadline).await)
                        }
                        ConfirmStep::Done(answer) => return answer,
                    }
                }
            }
            Round::List | Round::Watch(_) => {}
        }
        // A signal that was already waiting at registration resolves without a pending poll, and a
        // round that found every member gone reached no suspension at all: yield before listing
        // again.
        ::tokio::task::yield_now().await;
    }
}