stackless-core 0.4.0

Definition model, state store, and lifecycle engine for stackless
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
//! PID + process start time: the PID-reuse-safe liveness identity used
//! for operation locks (§2) and daemon supervision (§3). Bounded
//! subprocess waits (Stripe / launchctl / reaper children) live here so
//! a hung helper cannot pin the control plane forever.

use std::collections::HashSet;
use std::io::Read;
use std::os::fd::AsFd;
use std::process::{Command, Output, Stdio};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::{Duration, Instant};

use sysinfo::{Pid, ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind};

use crate::types::{Pid as StacklessPid, ProcessStartTime};

/// Result of [`run_with_timeout`].
#[derive(Debug)]
pub enum TimedCommand {
    Finished(Output),
    LockFailed {
        path: std::path::PathBuf,
        detail: String,
    },
    TimedOut {
        pid: u32,
    },
    Spawn(std::io::Error),
    CaptureFailed(CaptureFailure),
    CleanupFailed {
        pid: u32,
    },
}

/// Stripe JSON can be larger than a console log. Neither stream may grow without a limit.
pub const COMMAND_STDOUT_LIMIT: usize = 4 * 1024 * 1024;
pub const COMMAND_STDERR_LIMIT: usize = 256 * 1024;
const DRAIN_JOIN: Duration = Duration::from_secs(2);

#[derive(Debug, thiserror::Error)]
pub enum CaptureFailure {
    #[error("{stream} exceeded {limit} bytes")]
    Limit { stream: &'static str, limit: usize },
    #[error("{stream} remained open after command cleanup")]
    Incomplete { stream: &'static str },
    #[error("could not capture {stream}: {source}")]
    Read {
        stream: &'static str,
        source: std::io::Error,
    },
}

pub fn real_user_id() -> u32 {
    rustix::process::getuid().as_raw()
}

pub fn effective_user_id() -> u32 {
    rustix::process::geteuid().as_raw()
}

/// Capture a child while this process remains alive. The internal helper uses this
/// primitive; controller callers must use `helper_command::HelperCommand` so the
/// deadline survives controller death. Incomplete or excess output cannot finish.
pub fn run_with_timeout(cmd: &mut Command, budget: Duration) -> TimedCommand {
    cmd.stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .stdin(Stdio::null());
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt;
        cmd.process_group(0);
    }
    let cookie = uuid::Uuid::new_v4().to_string();
    cmd.env("STACKLESS_SPAWN", &cookie);
    let child = match cmd.spawn() {
        Ok(child) => child,
        Err(err) => return TimedCommand::Spawn(err),
    };
    collect_child(
        child,
        &cookie,
        budget,
        COMMAND_STDOUT_LIMIT,
        COMMAND_STDERR_LIMIT,
    )
}

pub(crate) fn collect_child(
    mut child: std::process::Child,
    cookie: &str,
    budget: Duration,
    stdout_limit: usize,
    stderr_limit: usize,
) -> TimedCommand {
    let pid = child.id();
    let root = ProcessStamp::of(pid);
    let stdout = drain_read(child.stdout.take(), "stdout", stdout_limit);
    let stderr = drain_read(child.stderr.take(), "stderr", stderr_limit);
    let deadline = Instant::now() + budget;
    let stop = Arc::new(AtomicBool::new(false));
    let seen = Arc::new(std::sync::Mutex::new(HashSet::new()));
    let watcher = {
        let stop = stop.clone();
        let seen = seen.clone();
        thread::spawn(move || {
            while !stop.load(Ordering::Relaxed) {
                if let Some(root) = root {
                    seen.lock()
                        .unwrap_or_else(|e| e.into_inner())
                        .extend(descendants(root));
                }
                thread::sleep(Duration::from_millis(1));
            }
        })
    };
    let outcome = loop {
        if stdout.failed.load(Ordering::Acquire) || stderr.failed.load(Ordering::Acquire) {
            break TimedCommand::CaptureFailed(CaptureFailure::Incomplete { stream: "output" });
        }
        match child.try_wait() {
            Ok(Some(status)) => {
                break TimedCommand::Finished(Output {
                    status,
                    stdout: Vec::new(),
                    stderr: Vec::new(),
                });
            }
            Ok(None) if Instant::now() < deadline => thread::sleep(Duration::from_millis(20)),
            Ok(None) => break TimedCommand::TimedOut { pid },
            Err(err) => break TimedCommand::Spawn(err),
        }
    };
    stop.store(true, Ordering::Relaxed);
    let _ = watcher.join();
    let mut processes = seen.lock().unwrap_or_else(|e| e.into_inner()).clone();
    if let Some(root) = root {
        processes.extend(descendants(root));
    }
    processes.extend(cookie_processes(cookie));
    let cleanup_deadline = Instant::now() + Duration::from_secs(3);
    let cleaned = loop {
        kill_observed(&processes);
        processes.retain(ProcessStamp::is_alive);
        processes.extend(cookie_processes(cookie));
        if processes.is_empty() {
            break true;
        }
        if Instant::now() >= cleanup_deadline {
            break false;
        }
        thread::sleep(Duration::from_millis(10));
    };
    if !matches!(outcome, TimedCommand::Finished(_)) {
        let _ = child.kill();
        let _ = child.wait();
    }
    let drain_deadline = Instant::now() + DRAIN_JOIN;
    let out = take_drain(stdout, drain_deadline);
    let err = take_drain(stderr, drain_deadline);
    if !cleaned {
        return TimedCommand::CleanupFailed { pid };
    }
    match outcome {
        TimedCommand::Finished(mut output) => match (out, err) {
            (Ok(stdout), Ok(stderr)) => {
                output.stdout = stdout;
                output.stderr = stderr;
                TimedCommand::Finished(output)
            }
            (Err(error), _) | (_, Err(error)) => TimedCommand::CaptureFailed(error),
        },
        TimedCommand::CaptureFailed(fallback) => {
            TimedCommand::CaptureFailed(out.err().or_else(|| err.err()).unwrap_or(fallback))
        }
        other => other,
    }
}

struct Drain {
    failed: Arc<AtomicBool>,
    stop: Arc<AtomicBool>,
    handle: thread::JoinHandle<Result<Vec<u8>, CaptureFailure>>,
    stream: &'static str,
}

fn drain_read<R: Read + AsFd + Send + 'static>(
    pipe: Option<R>,
    stream: &'static str,
    limit: usize,
) -> Drain {
    let failed = Arc::new(AtomicBool::new(false));
    let stop = Arc::new(AtomicBool::new(false));
    let reader_failed = failed.clone();
    let reader_stop = stop.clone();
    let handle = thread::spawn(move || {
        let result = capture(pipe, stream, limit, &reader_stop, &reader_failed);
        if result.is_err() {
            reader_failed.store(true, Ordering::Release);
        }
        result
    });
    Drain {
        failed,
        stop,
        handle,
        stream,
    }
}

fn capture<R: Read + AsFd>(
    pipe: Option<R>,
    stream: &'static str,
    limit: usize,
    stop: &AtomicBool,
    failed: &AtomicBool,
) -> Result<Vec<u8>, CaptureFailure> {
    let mut pipe = pipe.ok_or_else(|| CaptureFailure::Read {
        stream,
        source: std::io::Error::other("command pipe missing"),
    })?;
    let flags = rustix::fs::fcntl_getfl(&pipe).map_err(|error| CaptureFailure::Read {
        stream,
        source: error.into(),
    })?;
    rustix::fs::fcntl_setfl(&pipe, flags | rustix::fs::OFlags::NONBLOCK).map_err(|error| {
        CaptureFailure::Read {
            stream,
            source: error.into(),
        }
    })?;
    let mut bytes = Vec::with_capacity(limit);
    let mut exceeded = false;
    let mut complete = false;
    let mut chunk = [0u8; 8192];
    while !stop.load(Ordering::Acquire) {
        match pipe.read(&mut chunk) {
            Ok(0) => {
                complete = true;
                break;
            }
            Ok(count) => {
                let retained = count.min(limit.saturating_sub(bytes.len()));
                bytes.extend_from_slice(&chunk[..retained]);
                if retained != count {
                    exceeded = true;
                    failed.store(true, Ordering::Release);
                }
            }
            Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                thread::sleep(Duration::from_millis(5))
            }
            Err(error) if error.kind() == std::io::ErrorKind::Interrupted => (),
            Err(source) => return Err(CaptureFailure::Read { stream, source }),
        }
    }
    if exceeded {
        return Err(CaptureFailure::Limit { stream, limit });
    }
    if !complete {
        return Err(CaptureFailure::Incomplete { stream });
    }
    Ok(bytes)
}

fn take_drain(drain: Drain, deadline: Instant) -> Result<Vec<u8>, CaptureFailure> {
    while !drain.handle.is_finished() && Instant::now() < deadline {
        thread::sleep(Duration::from_millis(5));
    }
    drain.stop.store(true, Ordering::Release);
    drain.handle.join().unwrap_or_else(|_| {
        Err(CaptureFailure::Read {
            stream: drain.stream,
            source: std::io::Error::other("command reader panicked"),
        })
    })
}

/// SIGKILL `root` and every descendant, including children that created
/// their own process group (Stripe CLI helpers).
pub fn kill_process_tree(root: u32) {
    if let Some(root) = ProcessStamp::of(root) {
        kill_process_tree_stamped(root);
    }
}

/// Stop only the supplied process incarnation and the descendants observed under it.
pub fn kill_process_tree_stamped(root: ProcessStamp) {
    kill_owned_processes(root, []);
}

pub(crate) fn kill_owned_processes(
    root: ProcessStamp,
    members: impl IntoIterator<Item = ProcessStamp>,
) {
    let mut processes = descendants(root);
    processes.extend(members);
    kill_observed(&processes);
}

fn process_stamp(pid: Pid, process: &sysinfo::Process) -> ProcessStamp {
    ProcessStamp {
        pid: StacklessPid::from_os(pid.as_u32()),
        start_time: ProcessStartTime::from_os(process.start_time()),
    }
}

fn descendants(root: ProcessStamp) -> HashSet<ProcessStamp> {
    let mut system = System::new();
    system.refresh_processes(ProcessesToUpdate::All, true);
    if system
        .process(Pid::from_u32(root.pid.get()))
        .is_none_or(|process| process.start_time() != root.start_time.get())
    {
        return HashSet::new();
    }
    let mut stack = vec![root.pid.get()];
    let mut visited = HashSet::new();
    let mut observed = HashSet::new();
    while let Some(pid) = stack.pop() {
        if !visited.insert(pid) {
            continue;
        }
        if let Some(process) = system.process(Pid::from_u32(pid))
            && process.status() != sysinfo::ProcessStatus::Zombie
        {
            observed.insert(process_stamp(Pid::from_u32(pid), process));
        }
        for (child, process) in system.processes() {
            if process.parent() == Some(Pid::from_u32(pid)) {
                stack.push(child.as_u32());
            }
        }
    }
    observed
}

fn cookie_processes(cookie: &str) -> HashSet<ProcessStamp> {
    let mut system = System::new();
    system.refresh_processes_specifics(
        ProcessesToUpdate::All,
        true,
        ProcessRefreshKind::nothing().with_environ(UpdateKind::Always),
    );
    let expected = format!("STACKLESS_SPAWN={cookie}");
    system
        .processes()
        .iter()
        .filter_map(|(pid, process)| {
            (process.status() != sysinfo::ProcessStatus::Zombie
                && process
                    .environ()
                    .iter()
                    .any(|value| value == std::ffi::OsStr::new(&expected)))
            .then_some(process_stamp(*pid, process))
        })
        .collect()
}

fn kill_observed(processes: &HashSet<ProcessStamp>) {
    let mut system = System::new();
    system.refresh_processes(ProcessesToUpdate::All, true);
    let owned: std::collections::HashMap<_, _> = processes
        .iter()
        .filter(|stamp| {
            system
                .process(Pid::from_u32(stamp.pid.get()))
                .is_some_and(|process| process.start_time() == stamp.start_time.get())
        })
        .map(|stamp| (stamp.pid.get(), *stamp))
        .collect();
    let mut ordered: Vec<_> = owned.values().copied().collect();
    ordered.sort_by_key(|stamp| {
        let mut depth = 0;
        let mut cursor = stamp.pid.get();
        let mut visited = HashSet::new();
        while visited.insert(cursor) {
            let Some(parent) = system
                .process(Pid::from_u32(cursor))
                .and_then(|process| process.parent())
            else {
                break;
            };
            if !owned.contains_key(&parent.as_u32()) {
                break;
            }
            depth += 1;
            cursor = parent.as_u32();
        }
        (depth, stamp.pid.get())
    });
    // Killing a sleeping child first can wake its shell into the next command.
    // Freeze every owned parent before any child is killed or changes wait status.
    for process in &ordered {
        if process.is_alive()
            && let Ok(raw) = i32::try_from(process.pid.get())
            && let Some(pid) = rustix::process::Pid::from_raw(raw)
        {
            let _ = rustix::process::kill_process(pid, rustix::process::Signal::STOP);
        }
    }
    for process in ordered {
        if !process.is_alive() {
            continue;
        }
        if is_process_group_leader(process.pid.get()) {
            kill_process_group(process.pid.get());
        }
        if process.is_alive() {
            kill_one(process.pid.get());
        }
    }
}

fn is_process_group_leader(pid: u32) -> bool {
    #[cfg(unix)]
    {
        let Ok(raw) = i32::try_from(pid) else {
            return false;
        };
        let Some(pid) = rustix::process::Pid::from_raw(raw) else {
            return false;
        };
        rustix::process::getpgid(Some(pid)).ok() == Some(pid)
    }
    #[cfg(not(unix))]
    {
        let _ = pid;
        false
    }
}

/// SIGKILL the process group whose leader is `pid` (set by `process_group(0)`).
pub fn kill_process_group(pid: u32) {
    #[cfg(unix)]
    if let Ok(raw) = i32::try_from(pid)
        && let Some(pgid) = rustix::process::Pid::from_raw(raw)
    {
        let _ = rustix::process::kill_process_group(pgid, rustix::process::Signal::KILL);
    }
    #[cfg(not(unix))]
    let _ = pid;
}

fn kill_one(pid: u32) {
    #[cfg(unix)]
    if let Ok(raw) = i32::try_from(pid)
        && let Some(pid) = rustix::process::Pid::from_raw(raw)
    {
        let _ = rustix::process::kill_process(pid, rustix::process::Signal::KILL);
    }
    #[cfg(not(unix))]
    let _ = pid;
}

/// Identifies one incarnation of one process.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ProcessStamp {
    pub pid: StacklessPid,
    /// Unix seconds the process started, per the OS.
    pub start_time: ProcessStartTime,
}

impl ProcessStamp {
    /// The stamp of the calling process.
    pub fn current() -> Self {
        let pid = StacklessPid::from_os(std::process::id());
        Self {
            pid,
            start_time: start_time_of(pid).unwrap_or(ProcessStartTime::from_os(0)),
        }
    }

    /// The stamp of an arbitrary live process, if it exists.
    pub fn of(pid: u32) -> Option<Self> {
        let pid = StacklessPid::from_os(pid);
        start_time_of(pid).map(|start_time| Self { pid, start_time })
    }

    /// True only if a process with this PID exists *and* started at the
    /// recorded time — a recycled PID does not count.
    pub fn is_alive(&self) -> bool {
        start_time_of(self.pid).is_some_and(|start| start == self.start_time)
    }
}

fn start_time_of(pid: StacklessPid) -> Option<ProcessStartTime> {
    let raw = pid.get();
    let mut system = System::new();
    system.refresh_processes_specifics(
        ProcessesToUpdate::Some(&[Pid::from_u32(raw)]),
        false,
        ProcessRefreshKind::nothing(),
    );
    system
        .process(Pid::from_u32(raw))
        .filter(|process| process.status() != sysinfo::ProcessStatus::Zombie)
        .map(sysinfo::Process::start_time)
        .map(ProcessStartTime::from_os)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn current_process_is_alive() {
        let stamp = ProcessStamp::current();
        assert!(stamp.start_time.get() > 0);
        assert!(stamp.is_alive());
    }

    #[test]
    fn wrong_start_time_is_not_alive() {
        let stamp = ProcessStamp {
            pid: StacklessPid::from_os(std::process::id()),
            start_time: ProcessStartTime::from_os(1),
        };
        assert!(!stamp.is_alive());
    }

    #[test]
    fn bogus_pid_is_not_alive() {
        let stamp = ProcessStamp {
            pid: StacklessPid::from_os(u32::MAX - 1),
            start_time: ProcessStartTime::from_os(1),
        };
        assert!(!stamp.is_alive());
    }

    #[test]
    fn an_unreaped_exit_is_not_a_live_process() {
        let mut child = Command::new("/bin/sh")
            .args(["-c", "read stackless_gate; exit 0"])
            .stdin(Stdio::piped())
            .spawn()
            .unwrap();
        let stamp = ProcessStamp::of(child.id()).unwrap();
        drop(child.stdin.take());
        let deadline = Instant::now() + Duration::from_secs(3);
        while stamp.is_alive() && Instant::now() < deadline {
            thread::sleep(Duration::from_millis(10));
        }
        let stopped = !stamp.is_alive();
        child.wait().unwrap();
        assert!(
            stopped,
            "an exited child was treated as alive until its parent reaped it"
        );
    }

    #[test]
    fn run_with_timeout_finishes_a_quick_command() {
        let mut cmd = Command::new("echo");
        cmd.arg("stackless-timeout-ok");
        match run_with_timeout(&mut cmd, Duration::from_secs(5)) {
            TimedCommand::Finished(out) => {
                assert!(out.status.success());
                assert!(String::from_utf8_lossy(&out.stdout).contains("stackless-timeout-ok"));
            }
            other => panic!("expected finish, got {other:?}"),
        }
    }

    #[test]
    fn run_with_timeout_kills_a_sleeper() {
        let mut cmd = Command::new("sleep");
        cmd.arg("30");
        let started = Instant::now();
        match run_with_timeout(&mut cmd, Duration::from_millis(250)) {
            TimedCommand::TimedOut { pid } => {
                assert!(pid > 0);
                assert!(started.elapsed() < Duration::from_secs(3));
                assert!(
                    ProcessStamp::of(pid).is_none_or(|stamp| !stamp.is_alive()),
                    "sleeper pid {pid} still alive after timeout kill"
                );
            }
            other => panic!("expected timeout, got {other:?}"),
        }
    }

    #[test]
    fn run_with_timeout_drains_stdout_larger_than_a_pipe() {
        let mut cmd = Command::new("python3");
        cmd.args(["-c", "print('x' * 200_000, end='')"]);
        match run_with_timeout(&mut cmd, Duration::from_secs(5)) {
            TimedCommand::Finished(out) => {
                assert!(out.status.success());
                assert_eq!(out.stdout.len(), 200_000);
            }
            other => panic!("expected finish, got {other:?}"),
        }
    }

    #[test]
    fn capture_rejects_overflow_even_when_the_child_exits_zero() {
        for (stream, limit) in [
            ("stdout", COMMAND_STDOUT_LIMIT),
            ("stderr", COMMAND_STDERR_LIMIT),
        ] {
            for excess in [0, 1] {
                let mut command = Command::new("python3");
                command.args([
                    "-c",
                    &format!(
                        "import sys; sys.{stream}.buffer.write(b'x' * {}); sys.{stream}.flush()",
                        limit + excess
                    ),
                ]);
                match run_with_timeout(&mut command, Duration::from_secs(5)) {
                    TimedCommand::Finished(output) if excess == 0 => {
                        assert!(output.status.success());
                        assert_eq!(
                            if stream == "stdout" {
                                output.stdout.len()
                            } else {
                                output.stderr.len()
                            },
                            limit
                        );
                    }
                    TimedCommand::CaptureFailed(CaptureFailure::Limit {
                        stream: actual,
                        limit: actual_limit,
                    }) if excess == 1 => {
                        assert_eq!(actual, stream);
                        assert_eq!(actual_limit, limit);
                    }
                    other => panic!("unexpected capture result: {other:?}"),
                }
            }
        }
    }

    #[test]
    fn capture_overflow_stops_the_process_before_its_time_budget() {
        let root = tempfile::tempdir().unwrap();
        let mut command = Command::new("python3");
        command.current_dir(root.path()).args([
            "-c",
            &format!(
                r#"
import os, pathlib, sys, time
pathlib.Path('pid').write_text(str(os.getpid()))
sys.stdout.buffer.write(b'x' * {})
sys.stdout.flush()
time.sleep(30)
pathlib.Path('late').touch()
"#,
                COMMAND_STDOUT_LIMIT * 2
            ),
        ]);
        let started = Instant::now();
        assert!(matches!(
            run_with_timeout(&mut command, Duration::from_secs(30)),
            TimedCommand::CaptureFailed(CaptureFailure::Limit {
                stream: "stdout",
                ..
            })
        ));
        assert!(started.elapsed() < Duration::from_secs(5));
        let pid = std::fs::read_to_string(root.path().join("pid"))
            .unwrap()
            .parse::<u32>()
            .unwrap();
        assert!(ProcessStamp::of(pid).is_none());
        assert!(!root.path().join("late").exists());
    }

    #[test]
    fn capture_stops_and_joins_a_reader_whose_writer_never_closes() {
        use std::io::Write;
        let (reader, mut writer) = std::os::unix::net::UnixStream::pair().unwrap();
        writer.write_all(br#"{"ok":true}"#).unwrap();
        let drain = drain_read(Some(reader), "stdout", 1024);
        let started = Instant::now();
        assert!(matches!(
            take_drain(drain, Instant::now() + Duration::from_millis(50)),
            Err(CaptureFailure::Incomplete { stream: "stdout" })
        ));
        assert!(started.elapsed() < Duration::from_secs(1));
        // Joining the reader drops its descriptor, although this writer is still open.
        assert!(writer.write_all(b"still open").is_err());
    }

    #[test]
    fn capture_cleanup_requires_an_exact_cookie_environment_entry() {
        let root = tempfile::tempdir().unwrap();
        let directory = root.path().to_path_buf();
        let worker = thread::spawn(move || {
            let mut command = Command::new("python3");
            command.current_dir(directory).args([
                "-c",
                r#"
import os, pathlib, time
pathlib.Path('cookie').write_text(os.environ['STACKLESS_SPAWN'])
while not pathlib.Path('finish').exists(): time.sleep(0.01)
"#,
            ]);
            run_with_timeout(&mut command, Duration::from_secs(10))
        });
        let deadline = Instant::now() + Duration::from_secs(5);
        let cookie = loop {
            if let Ok(cookie) = std::fs::read_to_string(root.path().join("cookie"))
                && uuid::Uuid::parse_str(&cookie).is_ok()
            {
                break cookie;
            }
            assert!(Instant::now() < deadline);
            thread::sleep(Duration::from_millis(10));
        };
        let mut unrelated = Command::new("/bin/sleep")
            .arg("30")
            .env("UNRELATED", &cookie)
            .spawn()
            .unwrap();
        let mut substring = Command::new("/bin/sleep")
            .arg("30")
            .env("STACKLESS_SPAWN", format!("prefix-{cookie}"))
            .spawn()
            .unwrap();
        std::fs::write(root.path().join("finish"), "done").unwrap();
        let result = worker.join();
        let unrelated_alive = unrelated.try_wait().unwrap().is_none();
        let substring_alive = substring.try_wait().unwrap().is_none();
        let _ = unrelated.kill();
        let _ = unrelated.wait();
        let _ = substring.kill();
        let _ = substring.wait();
        assert!(matches!(result.unwrap(), TimedCommand::Finished(_)));
        assert!(unrelated_alive && substring_alive);
    }

    #[test]
    fn capture_cleanup_rejects_a_stale_process_incarnation() {
        use std::os::unix::process::CommandExt;
        let mut child = Command::new("/bin/sleep")
            .arg("30")
            .process_group(0)
            .spawn()
            .unwrap();
        let actual = ProcessStamp::of(child.id()).unwrap();
        let stale = ProcessStamp {
            start_time: ProcessStartTime::from_os(1),
            ..actual
        };
        kill_observed(&HashSet::from([stale]));
        kill_process_tree_stamped(stale);
        let alive = actual.is_alive();
        kill_process_tree_stamped(actual);
        child.wait().unwrap();
        assert!(alive);
        assert!(!actual.is_alive());
    }

    #[test]
    fn run_with_timeout_keeps_stdout_when_helper_holds_the_pipe() {
        let marker = "stackless-bound-helper-marker";
        let mut cmd = Command::new("python3");
        cmd.args([
            "-c",
            &format!(
                r#"
import os, sys
sys.stdout.write('{{"ok":true}}')
sys.stdout.flush()
if os.fork() == 0:
    os.setsid()
    import time
    time.sleep(30)  # {marker}
os._exit(0)
"#
            ),
        ]);
        match run_with_timeout(&mut cmd, Duration::from_secs(8)) {
            TimedCommand::Finished(out) => {
                assert!(out.status.success());
                assert!(
                    String::from_utf8_lossy(&out.stdout).contains(r#"{"ok":true}"#),
                    "stdout was {:?}",
                    String::from_utf8_lossy(&out.stdout)
                );
            }
            other => panic!("expected finish, got {other:?}"),
        }
        let leftover = Command::new("pgrep")
            .args(["-f", &format!("python3.*{marker}")])
            .output()
            .expect("pgrep");
        assert!(
            leftover.stdout.is_empty(),
            "setsid helper still alive: {}",
            String::from_utf8_lossy(&leftover.stdout)
        );
    }

    #[test]
    fn run_with_timeout_reaps_setsid_helper_that_closes_stdio() {
        let marker = "stackless-closed-stdio-helper-marker";
        let mut cmd = Command::new("python3");
        cmd.args([
            "-c",
            &format!(
                r#"
import os, sys
sys.stdout.write('{{"ok":true}}')
sys.stdout.flush()
if os.fork() == 0:
    os.setsid()
    os.close(1)
    os.close(2)
    import time
    time.sleep(30)  # {marker}
os._exit(0)
"#
            ),
        ]);
        match run_with_timeout(&mut cmd, Duration::from_secs(8)) {
            TimedCommand::Finished(out) => {
                assert!(out.status.success());
                assert!(
                    String::from_utf8_lossy(&out.stdout).contains(r#"{"ok":true}"#),
                    "stdout was {:?}",
                    String::from_utf8_lossy(&out.stdout)
                );
            }
            other => panic!("expected finish, got {other:?}"),
        }
        let leftover = Command::new("pgrep")
            .args(["-f", &format!("python3.*{marker}")])
            .output()
            .expect("pgrep");
        assert!(
            leftover.stdout.is_empty(),
            "closed-stdio helper still alive: {}",
            String::from_utf8_lossy(&leftover.stdout)
        );
    }

    #[test]
    fn run_with_timeout_reaps_setsid_helper_grandchildren() {
        let marker = "stackless-setsid-grandchild-marker";
        let mut cmd = Command::new("python3");
        cmd.args([
            "-c",
            &format!(
                r#"
import os, sys, time
sys.stdout.write('{{"ok":true}}')
sys.stdout.flush()
if os.fork() == 0:
    os.setsid()
    if os.fork() == 0:
        time.sleep(30)  # {marker}
        os._exit(0)
    time.sleep(30)
os._exit(0)
"#
            ),
        ]);
        match run_with_timeout(&mut cmd, Duration::from_secs(8)) {
            TimedCommand::Finished(out) => {
                assert!(out.status.success());
                assert!(
                    String::from_utf8_lossy(&out.stdout).contains(r#"{"ok":true}"#),
                    "stdout was {:?}",
                    String::from_utf8_lossy(&out.stdout)
                );
            }
            other => panic!("expected finish, got {other:?}"),
        }
        let leftover = Command::new("pgrep")
            .args(["-f", &format!("python3.*{marker}")])
            .output()
            .expect("pgrep");
        assert!(
            leftover.stdout.is_empty(),
            "setsid grandchild still alive: {}",
            String::from_utf8_lossy(&leftover.stdout)
        );
    }

    #[test]
    fn run_with_timeout_does_not_kill_unrelated_stripe_named_process() {
        let decoy = std::env::temp_dir().join(format!(
            "stripe-cli-projects-unrelated-{}",
            std::process::id()
        ));
        std::fs::write(&decoy, b"#!/bin/sh\nexec sleep 60\n").expect("write decoy");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = std::fs::metadata(&decoy).expect("meta").permissions();
            perms.set_mode(0o755);
            std::fs::set_permissions(&decoy, perms).expect("chmod");
        }
        let mut decoy_child = Command::new(&decoy)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .expect("spawn decoy");
        let decoy_pid = decoy_child.id();
        let mut cmd = Command::new("sleep");
        cmd.arg("30");
        let timed = run_with_timeout(&mut cmd, Duration::from_millis(250));
        let decoy_alive = ProcessStamp::of(decoy_pid).is_some_and(|s| s.is_alive());
        let _ = decoy_child.kill();
        let _ = decoy_child.wait();
        let _ = std::fs::remove_file(&decoy);
        match timed {
            TimedCommand::TimedOut { .. } => {}
            other => panic!("expected timeout, got {other:?}"),
        }
        assert!(
            decoy_alive,
            "unrelated stripe-cli-projects decoy {decoy_pid} was killed"
        );
    }
    #[test]
    fn cleanup_cannot_wake_a_waiting_parent_into_its_next_action() {
        use std::os::unix::process::CommandExt;
        let root = tempfile::tempdir().unwrap();
        for iteration in 0..12 {
            let directory = root.path().join(iteration.to_string());
            std::fs::create_dir(&directory).unwrap();
            let mut child = Command::new("python3")
                .current_dir(&directory)
                .args([
                    "-c",
                    r#"
import os, pathlib, time
child = os.fork()
if child == 0:
    time.sleep(30)
    os._exit(0)
pathlib.Path('child.tmp').write_text(str(child))
os.replace('child.tmp', 'child')
os.waitpid(child, 0)
pathlib.Path('escaped').touch()
"#,
                ])
                .stdin(Stdio::null())
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .process_group(0)
                .spawn()
                .unwrap();
            let parent = ProcessStamp::of(child.id()).unwrap();
            let deadline = Instant::now() + Duration::from_secs(2);
            let sleeper = loop {
                if let Ok(pid) = std::fs::read_to_string(directory.join("child")) {
                    break ProcessStamp::of(pid.parse().unwrap()).unwrap();
                }
                assert!(Instant::now() < deadline, "waiting parent did not start");
                thread::sleep(Duration::from_millis(5));
            };
            // Exercise the order that used to kill the sleeper before its waiting parent.
            let processes = (0..64)
                .map(|_| HashSet::from([sleeper, parent]))
                .find(|processes| processes.iter().next() == Some(&sleeper))
                .unwrap();
            kill_observed(&processes);
            child.wait().unwrap();
            // Reaping the parent does not wait for SIGKILL to finish in its child.
            let deadline = Instant::now() + Duration::from_secs(3);
            while (parent.is_alive() || sleeper.is_alive()) && Instant::now() < deadline {
                thread::sleep(Duration::from_millis(5));
            }
            assert!(
                !parent.is_alive() && !sleeper.is_alive(),
                "cleanup left a live process: parent={parent:?}, child={sleeper:?}"
            );
            assert!(!directory.join("escaped").exists());
        }
    }
}