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>,
reaped_before_the_sweep: bool,
op: impl FnOnce(&crate::Child),
) -> (Waiter, Wait) {
let (tx, rx) = channel();
let rx = Rc::new(rx);
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);
let rx = Rc::clone(&rx);
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));
}
});
let _reaped = reaped_before_the_sweep.then(|| {
let seen = Rc::clone(&seen);
at(HookPoint::AfterTerminate, move || {
let blocked = matches!(seen.borrow().as_ref(), Some((Waiter::Blocked, _)));
if blocked {
assert_eq!(rx.recv().expect("the waiter reaps"), Waiter::Reaped);
}
})
});
op(child);
let seen = seen.borrow_mut().take().expect("the call took its look");
seen
}
fn own_wait_lands_after_the_sends(
reaped_before_the_sweep: bool,
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, reaped_before_the_sweep, 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(
false,
|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(
false,
|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();
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
own_wait_lands_after_the_sends(
true,
|child| {
let err = child
.graceful_shutdown_tree(std::time::Duration::ZERO)
.expect_err("a reaped root's group is not signalled by its number");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "got {err:?}");
assert!(
err.to_string()
.contains(crate::child::drop_report::END_THEM_BEFORE_WAIT),
"the error says how to end the descendants: {err}"
);
},
|hook| termed.on_send(hook),
);
assert_eq!(
killed.killed(),
Vec::<i32>::new(),
"the sweep sent no killpg by the reaped root's number"
);
}