use std::cell::RefCell;
use crate::test_child::Console;
use std::rc::Rc;
use std::sync::mpsc::channel;
use std::sync::Arc;
use std::thread::JoinHandle;
use crate::containment::unix::fault::{record_kill_group, record_term_group};
use crate::graceful_hooks::{at, HookPoint};
#[derive(Debug, PartialEq, Eq)]
enum Waiter {
Blocked,
Reaped,
}
type Wait = JoinHandle<Result<std::process::ExitStatus, crate::error::Error>>;
fn zombie_group_root() -> Arc<crate::Child> {
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
cmd.contain_with(crate::ContainMode::Session);
#[cfg(target_os = "macos")]
cmd.suppress_fd_marker();
let child = cmd.spawn().expect("spawn");
drop(writer);
crate::test_child::wait_until_zombie(child.pid());
Arc::new(child)
}
fn run_with_a_wait_after_the_look(child: &Arc<crate::Child>, op: impl FnOnce(&crate::Child)) -> (Waiter, Wait) {
let (tx, rx) = channel();
let _blocked = {
let tx = tx.clone();
crate::child::owned::seams::on_lock_contended(move || {
_ = tx.send(Waiter::Blocked);
})
};
let seen: Rc<RefCell<Option<(Waiter, Wait)>>> = Rc::default();
let _hook = at(HookPoint::AfterRootLook, {
let child = Arc::clone(child);
let seen = Rc::clone(&seen);
move || {
let wait = std::thread::spawn(move || {
let status = child.wait();
_ = tx.send(Waiter::Reaped);
status
});
let first = rx.recv().expect("the waiter reaps or blocks");
*seen.borrow_mut() = Some((first, wait));
}
});
op(child);
let seen = seen.borrow_mut().take().expect("the call took its look");
seen
}
fn own_wait_lands_after_the_sends(op: impl FnOnce(&crate::Child), on_send: impl FnOnce(Box<dyn FnMut(i32)>)) {
let child = zombie_group_root();
let pgid = child.pid() as i32;
let sends: Rc<RefCell<Vec<(i32, bool)>>> = Rc::default();
on_send(Box::new({
let child = Arc::clone(&child);
let sends = Rc::clone(&sends);
move |group| sends.borrow_mut().push((group, child.proc.is_held_by_current_thread()))
}));
let (first, wait) = run_with_a_wait_after_the_look(&child, op);
assert_eq!(
first,
Waiter::Blocked,
"an own wait reaped the root between the look and the sends"
);
let sends = sends.take();
assert!(
sends.iter().any(|(group, _)| *group == pgid),
"the group was signalled by its number: {sends:?}"
);
assert!(
sends.iter().all(|(group, held)| *group != pgid || *held),
"the group was signalled with this handle's reaps no longer held off: {sends:?}"
);
let status = wait.join().expect("the waiter").expect("the waiter reaps the root");
assert!(status.success(), "{status:?}");
}
#[skuld::test]
fn kill_tree_holds_off_an_own_wait_until_its_sends_are_done() {
let killed = record_kill_group();
own_wait_lands_after_the_sends(
|child| child.kill_tree().expect("kill the tree"),
|hook| killed.on_send(hook),
);
}
#[skuld::test]
fn terminate_tree_holds_off_an_own_wait_until_its_sends_are_done() {
let termed = record_term_group();
own_wait_lands_after_the_sends(
|child| child.terminate_tree().expect("signal the tree"),
|hook| termed.on_send(hook),
);
}
#[skuld::test]
fn graceful_shutdown_tree_holds_off_an_own_wait_until_its_sigterm_is_sent() {
let termed = record_term_group();
let _killed = record_kill_group();
own_wait_lands_after_the_sends(
|child| {
child
.graceful_shutdown_tree(std::time::Duration::ZERO)
.map(drop)
.unwrap_or_else(|e| panic!("shut the tree down: {e}"))
},
|hook| termed.on_send(hook),
);
}