cosca 0.8.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
//! The parent's policy over its child, written once: concurrent `wait` and `signal` on one spawned
//! child from any number of threads, reap once, "already reaped is gone", and holding off reaps across
//! a check-then-send. The OS part is an [`OsChild`], which states what the OS says now.
//!
//! **The handle never reaps at adoption, and never blocks in a reap.** Every reap is a look through
//! [`OsChild::try_reap`], taken by the one waiter that holds the wait, and it never blocks: a
//! non-blocking `waitid`, only of an exit record, on the pidfd (Linux) or the pid (macOS); on Windows
//! there is nothing to consume, and the reap reads the signalled process handle's exit code (see
//! [`crate::wait::exit_only`]). A child that this process traces is therefore never taken for exited
//! when it merely stops.
//!
//! # States
//!
//! `N` (nobody is waiting), `W { token }` (one thread, the *holder*, runs the wait loop) and
//! `E(record)` (the exit record is cached, the child is reaped). Only the holder looks with
//! `try_reap` and waits on what the look returned, or pauses for a recheck, with the lock released
//! for both; every other waiter blocks on the [`Condvar`] and re-dispatches on each wake. While `W`
//! is held, only the holder reaps.
//!
//! - Every lock is taken poison-tolerantly (`lock().unwrap_or_else(PoisonError::into_inner)`),
//!   `Condvar` results included. `Debug` alone uses `try_lock`.
//! - Every state write calls `notify_all` ([`Inner::set`], then [`OwnedChild::notify`]).
//! - The holder is armed by a [`HolderGuard`] whose `Drop` restores `N` (and wakes the waiters):
//!   an `Err`, a `?` or a panic can never strand the waiters, and a normal return can never
//!   restore `N` under a new holder, because [`HolderGuard::finish`] consumes the guard.
//! - A holder re-reads the state after it re-locks, before it acts on a look.
//! - Every timed block is clamped and looped ([`crate::wait::clamp_block`], and `drive::run` for the
//!   holder's), and after every wake `crate::wait::now() >= deadline` decides expiry, never the
//!   primitive's own "timed out".

use std::fmt;
use std::sync::{Condvar, Mutex, MutexGuard, PoisonError};
use std::time::{Duration, Instant};

use crate::error::Error;
use crate::os_process::child::{ExitRecord, Foreign, OsChild, PeekOutcome};
use crate::signal::{Sent, Sig};

#[path = "owned/holder.rs"]
mod holder;
#[path = "owned/slot.rs"]
pub(crate) mod slot;
#[path = "owned/teardown.rs"]
pub(crate) mod teardown;
#[path = "owned/unreaped.rs"]
pub(crate) mod unreaped;
#[path = "owned/waiting.rs"]
mod waiting;

#[cfg(test)]
#[path = "owned/over.rs"]
mod over;
#[cfg(test)]
#[path = "owned/seams.rs"]
pub(crate) mod seams;

/// What the handle knows about the child's exit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
    /// Nobody is waiting.
    N,
    /// The thread that drew `token` is the holder, running the wait loop.
    W { token: u64 },
    /// Reaped: the exit record is cached.
    E(ExitRecord),
}

struct Inner {
    state: State,
    /// The next holder's token: one per holder, so a stale guard cannot touch a newer holder.
    next_token: u64,
    /// Every state write and every `notify_all`, in order, pushed in the critical section that
    /// makes it: what the tests check a hand-off against.
    #[cfg(test)]
    log: Vec<Logged>,
}

/// One entry of [`Inner::log`].
#[cfg(test)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Logged {
    Write(State),
    Notify,
}

impl Inner {
    /// Write `state`. The caller wakes the waiters with [`OwnedChild::notify`] in the same
    /// critical section.
    fn set(&mut self, state: State) {
        self.state = state;
        #[cfg(test)]
        self.log.push(Logged::Write(state));
    }
}

/// The lock guard. In a test build it also records which thread holds the lock, so a thread that
/// locks it again panics instead of deadlocking.
#[cfg(not(test))]
type Guard<'a> = MutexGuard<'a, Inner>;

#[cfg(test)]
struct Guard<'a> {
    guard: Option<MutexGuard<'a, Inner>>,
    owner: &'a Mutex<Option<std::thread::ThreadId>>,
}

#[cfg(test)]
impl std::ops::Deref for Guard<'_> {
    type Target = Inner;
    fn deref(&self) -> &Inner {
        self.guard.as_ref().expect("a live guard")
    }
}

#[cfg(test)]
impl std::ops::DerefMut for Guard<'_> {
    fn deref_mut(&mut self) -> &mut Inner {
        self.guard.as_mut().expect("a live guard")
    }
}

#[cfg(test)]
impl Drop for Guard<'_> {
    fn drop(&mut self) {
        if self.guard.is_some() {
            *self.owner.lock().unwrap_or_else(PoisonError::into_inner) = None;
        }
    }
}

/// How a wait on a child ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ChildWaitOutcome {
    /// The child's exit record: this handle reaped it, now or earlier.
    Exited(ExitRecord),
    /// The child is not this process's to reap.
    Foreign(Foreign),
    /// The deadline passed with the child still running, after one final look.
    DeadlinePassed,
}

/// A spawned child that several threads may wait on and signal at once.
pub(crate) struct OwnedChild {
    /// Outside the mutex: the holder waits on the `WaitFor` its look returned with the lock released.
    os: Box<dyn OsChild>,
    inner: Mutex<Inner>,
    condvar: Condvar,
    /// The thread that holds `inner`, for [`OwnedChild::lock`]'s self-deadlock check.
    #[cfg(test)]
    owner: Mutex<Option<std::thread::ThreadId>>,
}

// Construction =====

impl OwnedChild {
    /// Take over `os`. **Makes no call to it**: an already-exited child is still a zombie afterwards.
    pub(crate) fn new(os: Box<dyn OsChild>) -> OwnedChild {
        OwnedChild {
            os,
            inner: Mutex::new(Inner {
                state: State::N,
                next_token: 0,
                #[cfg(test)]
                log: Vec::new(),
            }),
            condvar: Condvar::new(),
            #[cfg(test)]
            owner: Mutex::new(None),
        }
    }

    /// Take over the std `child`, whose identity `id` the caller has just read, through
    /// [`adopt_std`](crate::os_process::child::adopt_std). **Never reaps**: an already-exited child
    /// is still a zombie afterwards. Tests only: a spawn adopts through the handle it holds.
    #[cfg(test)]
    #[allow(
        clippy::result_large_err,
        reason = "the child is handed back untouched for the caller to deal with"
    )]
    pub(crate) fn adopt(
        child: std::process::Child,
        id: crate::identity::ProcessId,
    ) -> Result<OwnedChild, (Error, std::process::Child)> {
        crate::os_process::child::adopt_std(child, id).map(OwnedChild::new)
    }

    /// The child's pid, if its spawn learned it; see [`OsChild::pid`].
    pub(crate) fn pid(&self) -> Option<u32> {
        self.os.pid()
    }

    /// A number that names this child's OS handle object and no other's, for as long as the child is
    /// not moved out of its box: what a wait carried across an await is checked against.
    pub(crate) fn handle_id(&self) -> usize {
        std::ptr::from_ref::<dyn OsChild>(&*self.os).cast::<()>() as usize
    }

    /// The child as the process the OS names, to watch for its exit without reaping it.
    pub(crate) fn os_process(&self) -> &dyn crate::os_process::OsProcess {
        &*self.os
    }

    /// What a kill of the child may be refused for; see [`OsChild::kill_refusal`].
    #[cfg(any(target_os = "linux", windows))]
    pub(crate) fn kill_refusal(&self) -> crate::os_process::child::KillRefusal {
        self.os.kill_refusal()
    }

    /// The OS object that names the child, borrowed from `self`.
    #[cfg(any(target_os = "linux", windows))]
    pub(crate) fn os_handle(&self) -> crate::os_process::child::OsHandle<'_> {
        self.os.os_handle()
    }

    /// The root's pidfd where it has one (Linux; macOS names no process by a handle), for the checks
    /// of a cgroup or a group that take it.
    #[cfg(unix)]
    pub(crate) fn pidfd(&self) -> Option<std::os::fd::BorrowedFd<'_>> {
        #[cfg(target_os = "linux")]
        return Some(self.os_handle());
        #[cfg(not(target_os = "linux"))]
        None
    }

    /// Whether this handle's own wait has recorded the reap, read under the lock. A reap by
    /// someone else is not seen here.
    pub(crate) fn is_reaped(&self) -> bool {
        matches!(self.lock().state, State::E(_))
    }

    /// What a peek through the child's own handle shows (the exit record once this handle has reaped
    /// it), without reaping; see [`ReapHold::peek`], which does the same under a hold.
    pub(crate) fn peek(&self) -> Result<PeekOutcome, Error> {
        self.peek_locked(&self.lock())
    }

    /// Whether the child is running, answered from its exit edge (see
    /// [`OsProcess::exited`](crate::os_process::OsProcess::exited)), so a zombie only a tracer sees
    /// is `Dead`. The reap peek is read only where the edge is silent: an exit record or a reap
    /// elsewhere, and (macOS) the zombie the edge withholds.
    ///
    /// An edge that fired and could not be confirmed (`exited` failed) is answered by the peek where
    /// the peek can: an exit record or a reap elsewhere is `Dead`; otherwise the failure stands,
    /// also when the peek reads the child running, which proves nothing (a tracer can hold the
    /// zombie, which the real parent's `waitid` does not see). A `Recheck` (the exit watch fired, and
    /// the process is not yet a zombie) answers `Alive` until the zombie, as `ProcessId::is_alive`
    /// does, unless the peek shows the exit record.
    pub(crate) fn liveness(&self) -> Result<crate::identity::Liveness, Error> {
        use crate::identity::Liveness;
        use crate::os_process::Exit;
        let edge = match self.os.exited() {
            Ok(Exit::Exited) => return Ok(Liveness::Dead),
            Ok(edge) => edge,
            Err(refused) => {
                return match self.peek() {
                    Ok(PeekOutcome::Exit(_) | PeekOutcome::Foreign(_)) => Ok(Liveness::Dead),
                    Ok(PeekOutcome::Running) => Err(refused),
                    Err(peek) => {
                        log::debug!(
                            "liveness: child {:?}: the exit confirmation was refused ({}), and the peek behind it failed too ({})",
                            self.pid(),
                            crate::error::Report(&refused),
                            crate::error::Report(&peek)
                        );
                        Err(refused)
                    }
                };
            }
        };
        Ok(match self.peek()? {
            PeekOutcome::Exit(_) | PeekOutcome::Foreign(_) => Liveness::Dead,
            PeekOutcome::Running if matches!(edge, Exit::Running(_)) && self.os.exited_unannounced()? => Liveness::Dead,
            PeekOutcome::Running => Liveness::Alive,
        })
    }

    /// Whether the root is still this handle's child to act on, from the handle's own evidence; see
    /// [`ReapHold::state`].
    #[cfg(unix)]
    pub(crate) fn state(&self) -> crate::signal::RootState {
        self.state_locked(&self.lock())
    }

    /// [`state`](Self::state), under `lock`.
    #[cfg(unix)]
    fn state_locked(&self, lock: &Guard<'_>) -> crate::signal::RootState {
        use crate::signal::RootState;
        if matches!(lock.state, State::E(_)) {
            return RootState::Reaped { own: true };
        }
        RootState::of_peek(self.os.peek().map_err(Error::into_io))
    }

    /// Whether this thread holds the lock. Tests only.
    #[cfg(all(unix, test))]
    pub(crate) fn is_held_by_current_thread(&self) -> bool {
        *self.owner.lock().unwrap_or_else(PoisonError::into_inner) == Some(std::thread::current().id())
    }

    fn lock(&self) -> Guard<'_> {
        #[cfg(test)]
        {
            let me = std::thread::current().id();
            let owner = self.owner.lock().unwrap_or_else(PoisonError::into_inner);
            assert_ne!(
                *owner,
                Some(me),
                "self-deadlock: this thread locked an OwnedChild it already holds"
            );
            drop(owner);
            let guard = match self.inner.try_lock() {
                Ok(guard) => guard,
                Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(),
                Err(std::sync::TryLockError::WouldBlock) => {
                    seams::lock_contended();
                    self.inner.lock().unwrap_or_else(PoisonError::into_inner)
                }
            };
            *self.owner.lock().unwrap_or_else(PoisonError::into_inner) = Some(me);
            Guard {
                guard: Some(guard),
                owner: &self.owner,
            }
        }
        #[cfg(not(test))]
        self.inner.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// Wake every waiter, in the critical section that wrote the state.
    #[cfg_attr(not(test), allow(unused_variables, reason = "only a test build logs the wake"))]
    fn notify(&self, lock: &mut Guard<'_>) {
        #[cfg(test)]
        lock.log.push(Logged::Notify);
        self.condvar.notify_all();
    }

    /// `Condvar::wait` on `lock`.
    fn cv_wait<'a>(&'a self, lock: Guard<'a>) -> Guard<'a> {
        #[cfg(test)]
        {
            let mut lock = lock;
            let inner = lock.guard.take().expect("a live guard");
            *self.owner.lock().unwrap_or_else(PoisonError::into_inner) = None;
            let inner = self.condvar.wait(inner).unwrap_or_else(PoisonError::into_inner);
            *self.owner.lock().unwrap_or_else(PoisonError::into_inner) = Some(std::thread::current().id());
            lock.guard = Some(inner);
            lock
        }
        #[cfg(not(test))]
        self.condvar.wait(lock).unwrap_or_else(PoisonError::into_inner)
    }

    /// `Condvar::wait_timeout` on `lock`.
    fn cv_wait_timeout<'a>(&'a self, lock: Guard<'a>, dur: Duration) -> Guard<'a> {
        #[cfg(test)]
        {
            let mut lock = lock;
            let inner = lock.guard.take().expect("a live guard");
            *self.owner.lock().unwrap_or_else(PoisonError::into_inner) = None;
            let (inner, _) = self
                .condvar
                .wait_timeout(inner, dur)
                .unwrap_or_else(PoisonError::into_inner);
            *self.owner.lock().unwrap_or_else(PoisonError::into_inner) = Some(std::thread::current().id());
            lock.guard = Some(inner);
            lock
        }
        #[cfg(not(test))]
        {
            let (lock, _) = self
                .condvar
                .wait_timeout(lock, dur)
                .unwrap_or_else(PoisonError::into_inner);
            lock
        }
    }
}

// Signals and holds =====

impl OwnedChild {
    /// Send `sig` to the child: [`Sent::Gone`] once this handle has reaped it, and otherwise what
    /// the OS says, with a refused send's bare errno. Under the lock, so none of our reaps lands
    /// between the check and the send.
    pub(crate) fn signal(&self, sig: Sig) -> Result<Sent, Error> {
        self.signal_locked(&self.lock(), sig)
    }

    /// [`signal`](Self::signal), under `lock`.
    fn signal_locked(&self, lock: &Guard<'_>, sig: Sig) -> Result<Sent, Error> {
        if matches!(lock.state, State::E(_)) {
            return Ok(Sent::Gone);
        }
        self.os.signal(sig)
    }

    /// The exit record or the foreign verdict a peek sees, without reaping, under `lock`.
    fn peek_locked(&self, lock: &Guard<'_>) -> Result<PeekOutcome, Error> {
        match lock.state {
            State::E(record) => Ok(PeekOutcome::Exit(record)),
            State::N | State::W { .. } => self.os.peek(),
        }
    }

    /// Whether the child has exited by `deadline` (`None`: for ever), read through the OS's own look
    /// ([`OsProcess::exited`](crate::os_process::OsProcess::exited)): a zombie its tracer holds
    /// counts on Linux; on macOS only at the hand-back (XNU withholds `NOTE_EXIT` from a
    /// non-tracer parent while `p_oppid` is set, `kern_event.c:1203-1210`). Reaps nothing: during a
    /// graceful tree shutdown the root stays an unreaped zombie until the group kill is done, so its
    /// pid names no one else (principle 4). Takes no lock, so it may run while another thread waits.
    pub(crate) fn exited_by(&self, deadline: Option<Instant>) -> Result<bool, Error> {
        crate::os_process::drive::exited_until(&*self.os, deadline)
    }

    /// Hold off this handle's reaps (every reap is taken under its lock) until the result drops:
    /// what it reads and sends meanwhile is one step to a `wait` on another thread. Nothing that locks the
    /// handle may be called while it is held.
    #[cfg(unix)]
    pub(crate) fn hold(&self, pid: u32) -> ReapHold<'_> {
        ReapHold {
            child: self,
            pid,
            lock: self.lock(),
        }
    }

    /// A contract breach after the `E` write: a consuming reap handed back a record that is not
    /// an exit. The state is already `E`, so this must run with the lock released.
    fn log_unreadable(&self, record: ExitRecord) {
        if let ExitRecord::Unreadable { si_code } = record {
            log::warn!(
                "{:?}: a consuming waitid returned si_code {si_code}, not an exit record",
                self.os
            );
            debug_assert!(false, "a consuming waitid on a zombie returned si_code {si_code}");
        }
    }
}

/// [`OwnedChild::hold`]: the handle's lock, and what may be read and sent under it.
#[cfg(unix)]
pub(crate) struct ReapHold<'a> {
    child: &'a OwnedChild,
    pid: u32,
    lock: Guard<'a>,
}

#[cfg(unix)]
impl ReapHold<'_> {
    /// What a peek through the child's own handle shows (the exit record once this handle has reaped
    /// it), without reaping.
    pub(crate) fn peek(&self) -> Result<PeekOutcome, Error> {
        self.child.peek_locked(&self.lock)
    }

    /// Whether the root is still this handle's child to act on, from the handle's own evidence, read
    /// under the lock so none of our reaps lands between the state and a send. A `Reaped` is `own`
    /// when this handle's wait collected it.
    ///
    /// A peek through the handle decides (Linux: the pidfd; macOS: the pid, checked against its
    /// unique id, with an unreadable id on a running child an error).
    pub(crate) fn state(&self) -> crate::signal::RootState {
        self.child.state_locked(&self.lock)
    }

    /// [`OwnedChild::signal`], under this hold.
    pub(crate) fn signal(&self, sig: Sig) -> Result<Sent, Error> {
        self.child.signal_locked(&self.lock, sig)
    }
}

/// The root for a send by its pid: [`state`](ReapHold::state) reads this hold's own evidence, so
/// no reap of ours lands between a check and its send. On Linux the root's pidfd names the group
/// exactly; on macOS there is none.
#[cfg(unix)]
impl crate::containment::Root for ReapHold<'_> {
    fn pid(&self) -> u32 {
        self.pid
    }

    fn signal(&self, sig: Sig) -> Result<Sent, Error> {
        ReapHold::signal(self, sig)
    }

    fn state(&self) -> crate::signal::RootState {
        ReapHold::state(self)
    }

    #[cfg(target_os = "linux")]
    fn pidfd(&self) -> Option<std::os::fd::BorrowedFd<'_>> {
        self.child.pidfd()
    }
}

/// The root of a tree call where nothing is sent by the root's pid (Windows): the call sends the
/// root its own signal through this handle. It holds no lock and holds off no reap.
#[cfg(not(unix))]
pub(crate) struct RootHandle<'a> {
    child: &'a OwnedChild,
    pid: u32,
}

#[cfg(not(unix))]
impl OwnedChild {
    /// The root for a tree call, under the name Unix's `hold` has, where it also locks the handle.
    /// Windows has no reap to hold off, so nothing is locked.
    pub(crate) fn hold(&self, pid: u32) -> RootHandle<'_> {
        RootHandle { child: self, pid }
    }
}

#[cfg(not(unix))]
impl crate::containment::Root for RootHandle<'_> {
    fn pid(&self) -> u32 {
        self.pid
    }

    fn signal(&self, sig: Sig) -> Result<Sent, Error> {
        self.child.signal(sig)
    }
}

// Debug =====

impl fmt::Debug for OwnedChild {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut d = f.debug_struct("OwnedChild");
        d.field("os", &self.os);
        // `try_lock`, never `lock`: formatting must not block behind a waiter.
        match self.inner.try_lock() {
            Ok(inner) => d.field("state", &inner.state),
            Err(std::sync::TryLockError::Poisoned(e)) => d.field("state", &e.into_inner().state),
            Err(std::sync::TryLockError::WouldBlock) => d.field("state", &format_args!("locked")),
        };
        d.finish_non_exhaustive()
    }
}

#[cfg(test)]
#[path = "owned_tests.rs"]
pub(crate) mod owned_tests;