microsandbox-agentd 0.6.15

Guest init process and agent daemon for microsandbox microVMs.
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
//! Process lifecycle management for the agent daemon.
//!
//! Initializing [`ProcessManager`] transfers process-wide child-status ownership
//! to its dedicated thread: it drains `waitpid(-1, WNOHANG)` and therefore also
//! consumes statuses for untracked children. Code that needs an exit status must
//! acquire [`ProcessManager::spawn_guard`] before creating the process and finish
//! with [`ProcessSpawnGuard::track`]. It must not independently wait on the same
//! child PID during normal operation. Terminal teardown is the deliberate
//! exception: it may reap any remaining child directly when the manager itself
//! can no longer be assumed healthy.

use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::future::Future;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::pin::Pin;
use std::sync::{Arc, Mutex, MutexGuard, OnceLock, mpsc};
use std::task::{Context, Poll};
use std::thread;

use tokio::signal::unix::{Signal, SignalKind};
use tokio::sync::{oneshot, watch};

use crate::error::{AgentdError, AgentdResult};

//--------------------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------------------

static PROCESS_MANAGER: OnceLock<Arc<ProcessManager>> = OnceLock::new();

/// Maximum number of children reaped while holding the process-state lock.
///
/// Releasing the lock between batches prevents a fork-heavy workload from
/// starving exec spawning and signalling indefinitely.
const REAP_BATCH_SIZE: usize = 64;

//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------

/// Coordinates process spawning, exit observation, and process-wide child reaping.
#[derive(Debug)]
pub struct ProcessManager {
    state: Mutex<ProcessManagerState>,
    startup_error: OnceLock<String>,
    failure_tx: watch::Sender<Option<String>>,
}

#[derive(Debug)]
struct ProcessManagerState {
    processes: HashMap<i32, TrackedProcess>,
    next_generation: u64,
    terminal_error: Option<String>,
}

#[derive(Debug)]
struct TrackedProcess {
    generation: u64,
    exit_tx: Option<oneshot::Sender<i32>>,
}

/// Keeps process reaping paused until a newly spawned PID is tracked.
pub struct ProcessSpawnGuard<'a> {
    state: MutexGuard<'a, ProcessManagerState>,
}

/// Stable identity for one registration of an operating-system PID.
///
/// PIDs can be reused after a child is reaped. The generation prevents an old
/// exec session from signalling a newer process that received the same PID.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ProcessIdentity {
    pid: i32,
    generation: u64,
}

/// Observes the eventual exit code of a tracked process.
///
/// Processes terminated by a signal resolve to `-1`. If the reaper thread
/// unexpectedly drops the notification, the failure is logged and also resolves
/// to `-1` to preserve the exec-session wire protocol.
pub struct ProcessExitWatcher {
    identity: ProcessIdentity,
    receiver: oneshot::Receiver<i32>,
}

//--------------------------------------------------------------------------------------------------
// Methods
//--------------------------------------------------------------------------------------------------

impl ProcessManager {
    /// Returns the process-wide manager, starting its `SIGCHLD` thread on first use.
    ///
    /// The first call blocks synchronously until the dedicated thread has built
    /// its runtime and installed the `SIGCHLD` listener.
    ///
    /// # Errors
    ///
    /// Returns an error if the thread, runtime, or signal listener cannot start,
    /// or if the process manager has terminated unexpectedly.
    pub fn get() -> AgentdResult<Arc<Self>> {
        if let Some(manager) = PROCESS_MANAGER.get() {
            return manager.result();
        }

        let candidate = Arc::new(Self::new());
        let manager = PROCESS_MANAGER.get_or_init(move || {
            candidate.launch_thread();
            candidate
        });
        manager.result()
    }

    fn new() -> Self {
        let (failure_tx, _) = watch::channel(None);
        Self {
            state: Mutex::new(ProcessManagerState::new()),
            startup_error: OnceLock::new(),
            failure_tx,
        }
    }

    /// Opens a spawn section that must end by tracking the child PID.
    ///
    /// The returned guard serializes the short spawn-to-registration window with
    /// reaping. Production exec requests are already spawned serially by the agent
    /// loop, so allowing parallel spawns here adds complexity without throughput.
    ///
    /// # Errors
    ///
    /// Returns an error if the process manager has terminated.
    pub fn spawn_guard(&self) -> AgentdResult<ProcessSpawnGuard<'_>> {
        let state = self
            .state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        if let Some(error) = state.terminal_error.as_ref() {
            return Err(AgentdError::ExecSession(error.clone()));
        }
        Ok(ProcessSpawnGuard { state })
    }

    /// Subscribes to terminal process-manager failures.
    ///
    /// The receiver is created before checking current state so a failure cannot
    /// occur between the check and subscription without being observed.
    pub fn subscribe_failure(&self) -> AgentdResult<watch::Receiver<Option<String>>> {
        let receiver = self.failure_tx.subscribe();
        if let Some(error) = self
            .state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .terminal_error
            .as_ref()
        {
            return Err(AgentdError::ExecSession(error.clone()));
        }
        Ok(receiver)
    }

    /// Signals a tracked process group only while its registration is current.
    ///
    /// Holding the state lock across the identity check and `kill` prevents a
    /// reaped PID from being registered to a new session in between them.
    pub fn signal_process_group(&self, identity: ProcessIdentity, signum: i32) -> AgentdResult<()> {
        let state = self
            .state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let Some(process) = state.processes.get(&identity.pid) else {
            return Ok(());
        };
        if process.generation != identity.generation {
            return Ok(());
        }

        if process.exit_tx.is_some() {
            signal_process_group_or_process(identity.pid, signum)
        } else {
            // Once the leader has been reaped, a direct-PID fallback could hit
            // an unrelated process that reused its PID. Only the still-existing
            // process group is a valid target for the completed registration.
            signal_process_group_only(identity.pid, signum)
        }
    }

    /// Releases a process registration when its exec session is no longer signalable.
    pub(crate) fn release(&self, identity: ProcessIdentity) {
        let mut state = self
            .state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        if state.matches(identity) {
            state.processes.remove(&identity.pid);
        }
    }

    fn launch_thread(self: &Arc<Self>) {
        let (startup_tx, startup_rx) = mpsc::sync_channel(1);
        let manager = Arc::clone(self);
        let spawn_result = thread::Builder::new()
            .name("agentd-process-manager".to_string())
            .spawn(move || {
                let failure_manager = Arc::clone(&manager);
                let result = catch_unwind(AssertUnwindSafe(|| {
                    run_process_manager_thread(manager, startup_tx)
                }));
                let error = match result {
                    Ok(Err(error)) => error,
                    Ok(Ok(())) => "process manager thread stopped unexpectedly".to_string(),
                    Err(_) => "process manager thread panicked".to_string(),
                };
                failure_manager.fail(error);
            });

        let startup_result = match spawn_result {
            Ok(_) => startup_rx
                .recv()
                .unwrap_or_else(|error| Err(format!("receive thread startup: {error}"))),
            Err(error) => Err(format!("spawn process manager thread: {error}")),
        };
        if let Err(error) = startup_result {
            let _ = self.startup_error.set(error);
        }
    }

    fn result(self: &Arc<Self>) -> AgentdResult<Arc<Self>> {
        if let Some(error) = self.startup_error.get() {
            return Err(AgentdError::ExecSession(format!(
                "start process manager: {error}"
            )));
        }
        match self
            .state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .terminal_error
            .as_ref()
        {
            Some(error) => Err(AgentdError::ExecSession(error.clone())),
            None => Ok(Arc::clone(self)),
        }
    }

    fn fail(&self, error: String) {
        let mut state = self
            .state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        if state.terminal_error.is_some() {
            return;
        }

        state.terminal_error = Some(error.clone());
        for (pid, process) in &state.processes {
            if process.exit_tx.is_some() {
                let _ = signal_process_group_or_process(*pid, libc::SIGKILL);
            } else {
                let _ = signal_process_group_only(*pid, libc::SIGKILL);
            }
        }
        state.processes.clear();
        drop(state);

        // `send_replace` retains the failure even if the agent has not subscribed yet.
        self.failure_tx.send_replace(Some(error));
    }

    async fn run(self: Arc<Self>, mut signal: Signal) -> Result<(), String> {
        self.reap_until_idle()?;
        while signal.recv().await.is_some() {
            self.reap_until_idle()?;
        }
        Err("process manager SIGCHLD listener closed".to_string())
    }

    fn reap_until_idle(&self) -> Result<(), String> {
        while self.reap_exited_batch()? {
            // The state lock is released between batches. Yielding here gives
            // a waiting spawn or signal request a chance to acquire it.
            thread::yield_now();
        }
        Ok(())
    }

    fn reap_exited_batch(&self) -> Result<bool, String> {
        let mut state = self
            .state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        state
            .reap_exited_batch()
            .map_err(|error| format!("waitpid failed while reaping processes: {error}"))
    }
}

impl ProcessManagerState {
    fn new() -> Self {
        Self {
            processes: HashMap::new(),
            next_generation: 1,
            terminal_error: None,
        }
    }

    fn track(&mut self, pid: i32) -> AgentdResult<ProcessExitWatcher> {
        if pid <= 0 {
            return Err(AgentdError::ExecSession(format!(
                "cannot track invalid process PID {pid}"
            )));
        }

        let generation = self.next_generation;
        self.next_generation = self.next_generation.checked_add(1).ok_or_else(|| {
            AgentdError::ExecSession("process registration generation exhausted".to_string())
        })?;
        let identity = ProcessIdentity { pid, generation };
        let (exit_tx, receiver) = oneshot::channel();
        match self.processes.entry(pid) {
            Entry::Vacant(entry) => {
                entry.insert(TrackedProcess {
                    generation,
                    exit_tx: Some(exit_tx),
                });
                Ok(ProcessExitWatcher { identity, receiver })
            }
            Entry::Occupied(mut entry) if entry.get().exit_tx.is_none() => {
                // A PID can only be reused after its old process group is gone.
                // Replacing the completed registration invalidates the old
                // session identity without rejecting the new exec request.
                entry.insert(TrackedProcess {
                    generation,
                    exit_tx: Some(exit_tx),
                });
                Ok(ProcessExitWatcher { identity, receiver })
            }
            Entry::Occupied(_) => Err(AgentdError::ExecSession(format!(
                "process PID {pid} is already tracked"
            ))),
        }
    }

    fn matches(&self, identity: ProcessIdentity) -> bool {
        self.processes
            .get(&identity.pid)
            .is_some_and(|process| process.generation == identity.generation)
    }

    /// Reaps at most one bounded batch.
    ///
    /// Returns `true` when the batch filled completely, which tells the caller
    /// to yield and immediately check for more exited children without relying
    /// on another (possibly coalesced) `SIGCHLD` notification.
    fn reap_exited_batch(&mut self) -> std::io::Result<bool> {
        let mut reaped = 0;
        while reaped < REAP_BATCH_SIZE {
            let mut status = 0;
            let pid = unsafe { libc::waitpid(-1, &mut status, libc::WNOHANG) };
            if pid > 0 {
                reaped += 1;
                let mut remove = false;
                if let Some(process) = self.processes.get_mut(&pid)
                    && let Some(exit_tx) = process.exit_tx.take()
                {
                    // Keep a completed registration as a tombstone while the
                    // exec session drains output and remains signalable. If the
                    // watcher was already dropped, no session owns the identity.
                    remove = exit_tx.send(exit_code(status)).is_err();
                }
                if remove {
                    self.processes.remove(&pid);
                }
                continue;
            }
            if pid == 0 {
                return Ok(false);
            }

            let error = std::io::Error::last_os_error();
            if error.raw_os_error() == Some(libc::EINTR) {
                continue;
            }
            if error.raw_os_error() == Some(libc::ECHILD) {
                return Ok(false);
            }
            return Err(error);
        }
        Ok(true)
    }
}

impl ProcessSpawnGuard<'_> {
    /// Tracks the spawned PID before allowing process reaping to proceed.
    ///
    /// Consuming the guard makes the manager the sole owner of that PID's exit
    /// status during normal operation. Await the returned [`ProcessExitWatcher`]
    /// instead of calling `waitpid` or a child handle's wait method. Terminal
    /// teardown may bypass the manager as a last-resort fallback.
    ///
    /// # Errors
    ///
    /// Returns an error when `pid` is not positive or is already tracked.
    pub fn track(mut self, pid: i32) -> AgentdResult<ProcessExitWatcher> {
        match self.state.track(pid) {
            Ok(exit_watcher) => Ok(exit_watcher),
            Err(error) => {
                // Registration failed while the state lock still prevents the
                // reaper from freeing and reusing this PID.
                if pid > 0 {
                    let _ = signal_process_group_or_process(pid, libc::SIGKILL);
                }
                Err(error)
            }
        }
    }
}

impl ProcessIdentity {
    /// Returns the operating-system PID associated with this registration.
    pub fn pid(self) -> i32 {
        self.pid
    }
}

impl ProcessExitWatcher {
    /// Returns the stable identity of the tracked process.
    pub fn identity(&self) -> ProcessIdentity {
        self.identity
    }
}

//--------------------------------------------------------------------------------------------------
// Trait Implementations
//--------------------------------------------------------------------------------------------------

impl Future for ProcessExitWatcher {
    type Output = i32;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match Pin::new(&mut self.receiver).poll(cx) {
            Poll::Ready(Ok(code)) => Poll::Ready(code),
            Poll::Ready(Err(error)) => {
                eprintln!(
                    "agentd: process manager dropped the exit notification for PID {}: {error}",
                    self.identity.pid
                );
                Poll::Ready(-1)
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

fn run_process_manager_thread(
    manager: Arc<ProcessManager>,
    startup: mpsc::SyncSender<Result<(), String>>,
) -> Result<(), String> {
    let runtime = match tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
    {
        Ok(runtime) => runtime,
        Err(error) => {
            let error = format!("build process manager Tokio runtime: {error}");
            let _ = startup.send(Err(error.clone()));
            return Err(error);
        }
    };

    runtime.block_on(async move {
        match tokio::signal::unix::signal(SignalKind::child()) {
            Ok(signal) => {
                let _ = startup.send(Ok(()));
                manager.run(signal).await
            }
            Err(error) => {
                let error = format!("install process manager SIGCHLD listener: {error}");
                let _ = startup.send(Err(error.clone()));
                Err(error)
            }
        }
    })
}

fn signal_process_group_or_process(pid: i32, signum: i32) -> AgentdResult<()> {
    let group_result = unsafe { libc::kill(-pid, signum) };
    if group_result == 0 {
        return Ok(());
    }

    let group_error = std::io::Error::last_os_error();
    if group_error.raw_os_error() != Some(libc::ESRCH) {
        return Err(group_error.into());
    }

    // A failed pre-exec may exit before establishing its process group. The
    // direct PID is still safe to target because the caller holds the current
    // registration lock, so it cannot refer to a reused process here.
    let process_result = unsafe { libc::kill(pid, signum) };
    if process_result == 0 {
        return Ok(());
    }

    let process_error = std::io::Error::last_os_error();
    if process_error.raw_os_error() == Some(libc::ESRCH) {
        Ok(())
    } else {
        Err(process_error.into())
    }
}

fn signal_process_group_only(pid: i32, signum: i32) -> AgentdResult<()> {
    let result = unsafe { libc::kill(-pid, signum) };
    if result == 0 {
        return Ok(());
    }

    let error = std::io::Error::last_os_error();
    if error.raw_os_error() == Some(libc::ESRCH) {
        Ok(())
    } else {
        Err(error.into())
    }
}

fn exit_code(status: i32) -> i32 {
    if libc::WIFEXITED(status) {
        libc::WEXITSTATUS(status)
    } else {
        -1
    }
}

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::io::Read;
    use std::process::{Command, Stdio};
    use std::sync::Arc;
    use std::thread;
    use std::time::{Duration, Instant};

    use super::*;

    const HELPER_ENV: &str = "MSB_AGENTD_PROCESS_MANAGER_HELPER";
    const HELPER_SENTINEL: &str = "process-manager-helper-passed";
    const TEST_NAME: &str = "process::tests::reaping_is_batched_and_tracks_exit_codes";

    #[test]
    fn reaping_is_batched_and_tracks_exit_codes() {
        if std::env::var_os(HELPER_ENV).is_some() {
            run_batched_reap_scenario();
            println!("{HELPER_SENTINEL}");
            return;
        }

        let mut helper = Command::new(std::env::current_exe().expect("current test binary"))
            .args(["--exact", TEST_NAME, "--nocapture"])
            .env(HELPER_ENV, "1")
            .stdout(Stdio::piped())
            .spawn()
            .expect("spawn isolated process manager test");
        let mut output = String::new();
        helper
            .stdout
            .take()
            .expect("helper stdout")
            .read_to_string(&mut output)
            .expect("read helper stdout");

        match helper.wait() {
            Ok(status) => assert!(status.success(), "helper failed: {status}\n{output}"),
            Err(error) if error.raw_os_error() == Some(libc::ECHILD) => {}
            Err(error) => panic!("wait for helper: {error}"),
        }
        assert!(
            output.contains(HELPER_SENTINEL),
            "helper did not complete the reap scenario:\n{output}"
        );
    }

    #[test]
    fn invalid_pids_are_rejected() {
        let manager = ProcessManager::new();
        for pid in [-1, 0] {
            let error = match manager
                .spawn_guard()
                .expect("acquire process spawn guard")
                .track(pid)
            {
                Ok(_) => panic!("invalid PID should be rejected"),
                Err(error) => error,
            };
            assert!(error.to_string().contains(&pid.to_string()));
        }
    }

    #[test]
    fn terminal_failure_rejects_spawns_and_wakes_exits() {
        const UNUSED_PID: i32 = i32::MAX;

        let manager = Arc::new(ProcessManager::new());
        let mut failure_rx = manager
            .subscribe_failure()
            .expect("subscribe to process manager failure");
        let exit_watcher = manager
            .spawn_guard()
            .expect("acquire process spawn guard")
            .track(UNUSED_PID)
            .expect("track test PID");

        manager.fail("process manager test failure".to_string());

        assert!(manager.result().is_err());
        assert!(manager.spawn_guard().is_err());
        let runtime = tokio::runtime::Builder::new_current_thread()
            .build()
            .expect("test runtime");
        runtime
            .block_on(failure_rx.changed())
            .expect("receive process manager failure");
        assert_eq!(
            failure_rx.borrow().as_deref(),
            Some("process manager test failure")
        );
        assert_eq!(runtime.block_on(exit_watcher), -1);
    }

    #[test]
    fn stale_identity_does_not_match_reused_pid() {
        const PID: i32 = i32::MAX;

        let manager = ProcessManager::new();
        let first = manager
            .spawn_guard()
            .expect("acquire first process spawn guard")
            .track(PID)
            .expect("track first PID generation");
        let first_identity = first.identity();
        let first_exit_tx = manager
            .state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .processes
            .get_mut(&PID)
            .expect("first process registration")
            .exit_tx
            .take()
            .expect("first exit sender");
        first_exit_tx.send(0).expect("send first exit code");

        assert!(
            manager
                .state
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner())
                .matches(first_identity)
        );

        let second = manager
            .spawn_guard()
            .expect("acquire second process spawn guard")
            .track(PID)
            .expect("track reused PID generation");
        let second_identity = second.identity();
        assert_ne!(first_identity, second_identity);

        let state = manager
            .state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        assert!(!state.matches(first_identity));
        assert!(state.matches(second_identity));
        drop(state);

        // Releasing an old session must not unregister the new owner of the
        // reused PID.
        manager.release(first_identity);
        assert!(
            manager
                .state
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner())
                .matches(second_identity)
        );
        manager.release(second_identity);
        assert!(
            !manager
                .state
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner())
                .matches(second_identity)
        );
    }

    fn run_batched_reap_scenario() {
        let manager = Arc::new(ProcessManager::new());
        let mut tracked = Vec::with_capacity(REAP_BATCH_SIZE + 1);
        for offset in 0..=REAP_BATCH_SIZE {
            let code = 10 + (offset % 50) as i32;
            let guard = manager.spawn_guard().expect("acquire process spawn guard");
            let child = Command::new("/bin/sh")
                .args(["-c", &format!("exit {code}")])
                .spawn()
                .expect("spawn tracked child");
            let pid = child.id() as i32;
            drop(child);
            let exit_watcher = guard.track(pid).expect("track child");
            tracked.push((pid, code, exit_watcher));
        }

        for (pid, _, _) in &tracked {
            wait_until_exited_without_reaping(*pid);
        }

        assert!(
            manager
                .reap_exited_batch()
                .expect("reap first bounded batch")
        );
        drop(
            manager
                .spawn_guard()
                .expect("spawn lock should be released between reap batches"),
        );
        assert!(
            !manager
                .reap_exited_batch()
                .expect("reap remaining children")
        );

        let runtime = tokio::runtime::Builder::new_current_thread()
            .build()
            .expect("test runtime");
        for (_, expected_code, exit_watcher) in tracked {
            assert_eq!(runtime.block_on(exit_watcher), expected_code);
        }

        let orphan = Command::new("/bin/sh")
            .args(["-c", "exit 43"])
            .spawn()
            .expect("spawn untracked child");
        let orphan_pid = orphan.id() as i32;
        drop(orphan);
        wait_until_exited_without_reaping(orphan_pid);

        assert!(!manager.reap_exited_batch().expect("reap untracked child"));
        assert_already_reaped(orphan_pid);
    }

    fn wait_until_exited_without_reaping(pid: i32) {
        let deadline = Instant::now() + Duration::from_secs(5);
        while Instant::now() < deadline {
            let mut info = unsafe { std::mem::zeroed::<libc::siginfo_t>() };
            let ret = unsafe {
                libc::waitid(
                    libc::P_PID,
                    pid as libc::id_t,
                    &mut info,
                    libc::WEXITED | libc::WNOHANG | libc::WNOWAIT,
                )
            };
            assert_eq!(ret, 0, "waitid failed: {}", std::io::Error::last_os_error());
            if unsafe { info.si_pid() } == pid {
                return;
            }
            thread::sleep(Duration::from_millis(10));
        }

        panic!("child {pid} did not exit");
    }

    fn assert_already_reaped(pid: i32) {
        let ret = unsafe { libc::waitpid(pid, std::ptr::null_mut(), libc::WNOHANG) };
        assert_eq!(ret, -1);
        assert_eq!(
            std::io::Error::last_os_error().raw_os_error(),
            Some(libc::ECHILD)
        );
    }
}