use crate::child::spawn::fault::{release_between_kill_and_wait, TeardownBlocker};
use crate::containment::treewalk::fault::record_walks;
use crate::containment::unix::fault::{record_kill_group, record_term_group};
use crate::send_log::Capture;
use crate::signal::Sig;
use crate::test_child::Console;
#[cfg(target_os = "linux")]
use crate::test_groups::{cgroup, Group};
use crate::tokio::child::drop_fault;
use crate::tokio::Command;
use crate::{ContainMode, Containment};
const WARN: &str = "Child::drop: the root is already reaped";
#[cfg(target_os = "macos")]
const SESSION_CONTAINMENT: Containment = Containment::FdMarker;
#[cfg(not(target_os = "macos"))]
const SESSION_CONTAINMENT: Containment = Containment::Session;
fn session(argv: &[&str]) -> Command {
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(argv.iter().copied());
cmd.contain_with(ContainMode::Session);
cmd
}
fn session_blocker_cmd() -> Command {
let mut cmd = session(crate::test_child::BLOCKER_ARGV);
cmd.stdin(crate::Stdio::pipe()).expect("stdin pipe");
cmd
}
#[track_caller]
fn assert_the_drop_killed(teardown: &TeardownBlocker, sends: &Capture, pid: u32) {
teardown.assert_killed();
assert!(
sends.sent(pid, Sig::Kill),
"the drop must send the root SIGKILL: {:?}",
sends.entries()
);
}
fn drop_warns_since(mark: usize) -> Vec<(log::Level, String)> {
crate::log_capture::records_since_on_current_thread(mark, WARN)
}
#[skuld::test]
async fn dropping_a_waited_on_process_group_child_sends_no_killpg_and_warns_with_the_pgid() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let root = drop_fault::record();
let mut child = session(&["true"]).spawn().expect("spawn");
assert_eq!(child.containment(), SESSION_CONTAINMENT);
let pid = child.pid();
child.wait().await.expect("reap the root");
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(
recorder.killed(),
Vec::<i32>::new(),
"a reaped root's number may name another group: no killpg"
);
assert_eq!(
(root.kills(), root.forgets()),
(0, 0),
"tokio reaped it; nothing is left to do"
);
let warns = drop_warns_since(mark);
assert_eq!(warns.len(), 1, "exactly one warn, got {warns:?}");
let (level, text) = &warns[0];
assert_eq!(*level, log::Level::Warn);
assert!(text.contains(&format!("pgid {pid}")), "the warn names the pgid: {text}");
assert!(
text.contains("kill_tree()") && text.contains("wait()"),
"the warn says how to end descendants: {text}"
);
}
#[skuld::test]
async fn dropping_a_running_process_group_child_still_kills_the_group_and_does_not_warn() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let root = drop_fault::record();
let mut child = session_blocker_cmd().spawn().expect("spawn");
let teardown = release_between_kill_and_wait(child.stdin().expect("stdin pipe"));
let pid = child.pid();
let sends = Capture::start();
let mark = crate::log_capture::mark();
drop(child);
recorder.assert_killed_only(pid as i32);
assert_eq!((root.kills(), root.forgets()), (1, 0));
assert_eq!(drop_warns_since(mark), []);
assert_the_drop_killed(&teardown, &sends, pid);
}
#[skuld::test]
async fn dropping_an_exited_but_unreaped_process_group_child_still_kills_the_group() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let mut child = session_blocker_cmd().spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
let pid = child.pid();
child.kill().expect("kill the root");
drop(stdin);
crate::test_child::assert_zombie_sigkilled("the root", pid);
let mark = crate::log_capture::mark();
drop(child);
recorder.assert_killed_only(pid as i32);
assert_eq!(drop_warns_since(mark), []);
}
#[skuld::test]
async fn dropping_a_waited_on_tree_walk_child_walks_nothing_and_warns_with_the_root_pid() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let walks = record_walks();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(["true"]);
cmd.contain_with(ContainMode::TreeWalk);
let mut child = cmd.spawn().expect("spawn");
let pid = child.pid();
child.wait().await.expect("reap the root");
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(
walks.walked(),
Vec::<u32>::new(),
"a reaped root's number names no tree"
);
let warns = drop_warns_since(mark);
assert_eq!(warns.len(), 1, "exactly one warn, got {warns:?}");
assert_eq!(warns[0].0, log::Level::Warn);
assert!(
warns[0].1.contains(&format!("root pid {pid}")),
"the warn names the root: {warns:?}"
);
}
#[skuld::test]
async fn dropping_a_running_tree_walk_child_walks_from_its_pid_and_does_not_warn() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let walks = record_walks();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(crate::Stdio::pipe()).expect("stdin pipe");
cmd.contain_with(ContainMode::TreeWalk);
let mut child = cmd.spawn().expect("spawn");
let teardown = release_between_kill_and_wait(child.stdin().expect("stdin pipe"));
let pid = child.pid();
let sends = Capture::start();
let mark = crate::log_capture::mark();
drop(child);
let walked = walks.walked();
assert!(!walked.is_empty() && walked.iter().all(|w| *w == pid), "{walked:?}");
assert_eq!(drop_warns_since(mark), []);
assert_the_drop_killed(&teardown, &sends, pid);
}
#[skuld::test]
async fn dropping_a_foreign_reaped_process_group_child_sends_no_killpg() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let root = drop_fault::record();
let child = foreign_reaped(session_blocker_cmd());
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(recorder.killed(), Vec::<i32>::new());
assert_eq!(
(root.kills(), root.forgets()),
(0, 1),
"the number may belong to another child by now: no kill, and tokio's `Child` is forgotten so \
its own drop cannot reap by pid"
);
let warns =
crate::log_capture::records_since_on_current_thread(mark, "the root is already reaped, so this drop does not");
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(warns[0].1.contains("leaks"), "{warns:?}");
}
fn foreign_reaped(mut cmd: Command) -> crate::tokio::Child {
let mut child = cmd.spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
let pid = child.pid();
child.kill().expect("kill the root");
drop(stdin);
crate::test_child::assert_zombie_sigkilled("the root", pid);
let mut status = 0;
let reaped = unsafe { libc::waitpid(pid as libc::pid_t, &mut status, 0) };
assert_eq!(reaped, pid as libc::pid_t, "{}", std::io::Error::last_os_error());
child
}
#[skuld::test]
async fn a_detached_drop_after_a_foreign_reap_forgets_tokios_child() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let root = drop_fault::record();
let backend_drops = crate::tokio::child::fault::count_backend_drops();
let mut child = foreign_reaped(session_blocker_cmd());
child.detach().expect("detach");
drop(child);
assert_eq!(
(root.kills(), root.forgets(), backend_drops.get()),
(0, 1, 0),
"a detached drop must not run tokio's `Child` drop, which reaps by the reused number"
);
}
#[skuld::test]
async fn a_kill_on_drop_false_drop_after_a_foreign_reap_forgets_tokios_child() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let root = drop_fault::record();
let backend_drops = crate::tokio::child::fault::count_backend_drops();
let mut cmd = session_blocker_cmd();
cmd.kill_on_drop(false);
let child = foreign_reaped(cmd);
drop(child);
assert_eq!((root.kills(), root.forgets(), backend_drops.get()), (0, 1, 0));
}
#[skuld::test]
async fn drop_after_kill_tree_and_wait_skips_at_debug_and_does_not_warn() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let mut child = session(&["true"]).spawn().expect("spawn");
let pid = child.pid();
child.kill_tree().expect("kill_tree");
child.wait().await.expect("reap the root");
recorder.assert_killed_only(pid as i32);
let sent = recorder.killed();
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(recorder.killed(), sent, "the drop sends no further killpg");
assert_skipped_at_debug(mark);
}
#[skuld::test]
async fn drop_after_graceful_shutdown_tree_skips_at_debug_and_does_not_warn() {
use std::os::unix::process::ExitStatusExt as _;
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let mut child = session_blocker_cmd().spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
let pid = child.pid();
let _release = crate::graceful_hooks::release_at(crate::graceful_hooks::HookPoint::AfterTerminate, stdin);
let status = child
.graceful_shutdown_tree(std::time::Duration::from_secs(60))
.await
.expect("graceful_shutdown_tree");
assert_eq!(
status.signal(),
Some(libc::SIGTERM),
"the root dies to the group's SIGTERM: {status:?}"
);
recorder.assert_killed_only(pid as i32);
let mark = crate::log_capture::mark();
drop(child);
assert_skipped_at_debug(mark);
}
#[skuld::test]
async fn a_failed_elevated_spawns_cleanup_drops_its_handle_without_a_warn() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let mut child = session_blocker_cmd().spawn().expect("spawn");
let stdin = child.stdin().expect("piped stdin");
let teardown = crate::child::spawn::fault::release_between_kill_and_wait(stdin);
let pid = child.pid();
let mark = crate::log_capture::mark();
let (err, fate) = crate::tokio::spawn::finish_elevated(
child,
Err(crate::error::Error::Io(std::io::Error::other("no password"))),
)
.expect_err("the password write failed")
.expect_may_have_started_with();
assert!(matches!(err, crate::error::Error::Elevation { .. }), "{err:?}");
assert_eq!(
fate,
crate::error::ChildFate::Reaped,
"the group's kill and the root's reap"
);
recorder.assert_killed_only(pid as i32);
teardown.assert_killed();
assert_eq!(drop_warns_since(mark), []);
}
async fn waited_on_blocker_root() -> crate::tokio::Child {
let mut child = session_blocker_cmd().spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
child.kill().expect("kill the root");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().await.expect("reap the root"));
child
}
#[skuld::test]
async fn dropping_a_waited_on_child_whose_number_reads_unknown_still_sends_no_killpg() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let root = drop_fault::record();
let child = waited_on_blocker_root().await;
let _unknown = crate::child::fault::force_next_root_read(crate::identity::Resolved::Unknown);
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(recorder.killed(), Vec::<i32>::new());
assert_eq!((root.kills(), root.forgets()), (0, 0));
assert_eq!(drop_warns_since(mark).len(), 1);
}
#[skuld::test]
async fn dropping_an_unreaped_child_whose_number_reads_unknown_kills_and_logs_the_failed_read() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let root = drop_fault::record();
let mut child = session_blocker_cmd().spawn().expect("spawn");
let teardown = release_between_kill_and_wait(child.stdin().expect("stdin pipe"));
let pid = child.pid();
let _unknown = crate::child::fault::force_next_root_read(crate::identity::Resolved::Unknown);
let sends = Capture::start();
let mark = crate::log_capture::mark();
drop(child);
recorder.assert_killed_only(pid as i32);
assert_eq!((root.kills(), root.forgets()), (1, 0));
assert_eq!(drop_warns_since(mark), []);
let reads = crate::log_capture::records_since_on_current_thread(mark, "could not be read");
assert_eq!(reads.len(), 1, "{reads:?}");
assert_eq!(reads[0].0, log::Level::Debug);
assert_the_drop_killed(&teardown, &sends, pid);
}
#[skuld::test]
async fn drop_after_a_failed_kill_tree_and_wait_still_warns() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
recorder.fail_with_unassessable();
let mut child = session(&["true"]).spawn().expect("spawn");
child.kill_tree().expect_err("the forced refusal");
child.wait().await.expect("reap the root");
let mark = crate::log_capture::mark();
drop(child);
let records = drop_warns_since(mark);
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
}
#[skuld::test]
async fn drop_after_terminate_tree_and_wait_still_warns() {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let termed = record_term_group();
let mut child = session(&["true"]).spawn().expect("spawn");
let pid = child.pid();
child.terminate_tree().expect("terminate_tree");
assert_eq!(
termed.termed(),
vec![pid as i32],
"terminate_tree signals the group once"
);
child.wait().await.expect("reap the root");
let mark = crate::log_capture::mark();
drop(child);
let records = drop_warns_since(mark);
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
async fn a_drop_whose_walk_leaves_a_descendant_warns_once_and_kills_the_root() {
use crate::containment::treewalk::fault::LeftBehind;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving_async(LeftBehind::Descendant);
let teardown = release_between_kill_and_wait(stdin);
let pid = child.pid();
let sends = Capture::start();
let mark = crate::log_capture::mark();
drop(child);
assert_the_drop_killed(&teardown, &sends, pid);
let records = crate::log_capture::records_since_on_current_thread(mark, "was not killed");
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
let drop_records = crate::log_capture::warns_since(mark, "Child::drop");
assert_eq!(drop_records.len(), 1, "one report of the event: {drop_records:?}");
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
async fn kill_tree_and_drop_after_a_walk_that_left_a_descendant_and_the_root_name_the_descendant_alone() {
use crate::containment::treewalk::fault::LeftBehind;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, stdin) = crate::test_child::held_treewalk_blocker_leaving_async(LeftBehind::RootAndDescendant);
let root = child.pid();
let descendant = root.wrapping_add(1);
let mark = crate::log_capture::mark();
let err = child.kill_tree().expect_err("a descendant was left");
assert!(
matches!(err, crate::error::Error::MemberNotKilled { pid, .. } if pid == descendant),
"{err:?}"
);
let stale = crate::log_capture::records_since_on_current_thread(mark, &format!("pid {root} was not killed"));
assert_eq!(stale, [], "the root was killed by the handle");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().await.expect("reap the root"));
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving_async(LeftBehind::RootAndDescendant);
let teardown = release_between_kill_and_wait(stdin);
let root = child.pid();
let sends = Capture::start();
let mark = crate::log_capture::mark();
drop(child);
assert_the_drop_killed(&teardown, &sends, root);
let warns = crate::log_capture::warns_since(mark, "");
assert_eq!(warns.len(), 1, "one report of the event: {warns:?}");
let text = &warns[0];
assert!(
text.contains(&format!("pid {} was not killed", root.wrapping_add(1))),
"{text}"
);
assert!(!text.contains(&format!("pid {root} was not killed")), "{text}");
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
async fn a_drop_whose_walk_leaves_only_the_root_warns_it_could_not_vouch_for_the_tree() {
use crate::containment::treewalk::fault::LeftBehind;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (child, stdin) = crate::test_child::held_treewalk_blocker_leaving_async(LeftBehind::Root);
let teardown = release_between_kill_and_wait(stdin);
let pid = child.pid();
let sends = Capture::start();
let mark = crate::log_capture::mark();
drop(child);
assert_the_drop_killed(&teardown, &sends, pid);
let warns = crate::log_capture::warns_since(mark, "Child::drop");
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(warns[0].contains("could not be vouched for"), "{warns:?}");
assert!(!warns[0].contains("members"), "{warns:?}");
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
async fn a_second_kill_tree_after_a_walk_left_a_member_reports_it_again_and_does_not_mark_the_tree() {
use crate::containment::treewalk::fault::LeftBehind;
crate::tokio::test_runtime::assert_current_thread();
let (mut child, stdin) = crate::test_child::held_treewalk_blocker_leaving_async(LeftBehind::Descendant);
let first = child.kill_tree().expect_err("the walk left a member");
let second = child
.kill_tree()
.expect_err("the member the first walk left is not shown killed");
let pid_of = |error: &crate::error::Error| match error {
crate::error::Error::MemberNotKilled { pid, .. } => *pid,
other => panic!("expected MemberNotKilled, got {other:?}"),
};
assert_eq!(pid_of(&second), pid_of(&first), "{first:?}, {second:?}");
assert!(!child.tree_killed(), "a member was left");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().await.expect("reap the root"));
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
async fn a_drop_after_a_kill_tree_that_left_a_member_warns_of_it() {
use crate::containment::treewalk::fault::LeftBehind;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, stdin) = crate::test_child::held_treewalk_blocker_leaving_async(LeftBehind::Descendant);
child.kill_tree().expect_err("the walk left a member");
let mark = crate::log_capture::mark();
drop(child);
let records = crate::log_capture::records_since_on_current_thread(mark, "was not killed");
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
drop(stdin);
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
async fn a_kill_tree_after_wait_reports_the_member_an_earlier_kill_left() {
use crate::containment::treewalk::fault::LeftBehind;
crate::tokio::test_runtime::assert_current_thread();
let (mut child, stdin) = crate::test_child::held_treewalk_blocker_leaving_async(LeftBehind::Descendant);
let first = child.kill_tree().expect_err("the walk left a member");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().await.expect("reap the root"));
let second = child
.kill_tree()
.expect_err("the root is gone, and the member is not shown killed");
let pid_of = |error: &crate::error::Error| match error {
crate::error::Error::MemberNotKilled { pid, .. } => *pid,
other => panic!("expected MemberNotKilled, got {other:?}"),
};
assert_eq!(pid_of(&second), pid_of(&first), "{first:?}, {second:?}");
assert!(!child.tree_killed(), "a member was left");
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
async fn a_drop_after_a_kill_tree_that_left_a_member_and_a_wait_warns_of_it() {
use crate::containment::treewalk::fault::LeftBehind;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, stdin) = crate::test_child::held_treewalk_blocker_leaving_async(LeftBehind::Descendant);
child.kill_tree().expect_err("the walk left a member");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().await.expect("reap the root"));
let mark = crate::log_capture::mark();
drop(child);
let records = crate::log_capture::records_since_on_current_thread(mark, "was not killed");
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
async fn drop_after_a_walk_that_left_only_the_root_and_a_backstop_kill_warns_of_an_unvouched_tree() {
use crate::containment::treewalk::fault::LeftBehind;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, stdin) = crate::test_child::held_treewalk_blocker_leaving_async(LeftBehind::Root);
child.kill_tree().expect("the backstop killed the only member left");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().await.expect("reap the root"));
let mark = crate::log_capture::mark();
drop(child);
let warns = drop_warns_since(mark);
assert_eq!(warns.len(), 1, "{warns:?}");
assert_eq!(warns[0].0, log::Level::Warn, "{warns:?}");
assert!(warns[0].1.contains("could not be vouched for"), "{warns:?}");
assert!(!warns[0].1.contains("members"), "{warns:?}");
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn kill_tree_on_a_tree_walk_whose_root_is_reaped_is_unassessable_and_does_not_mark_the_tree_killed() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, stdin) = crate::test_child::held_treewalk_blocker_async();
drop(stdin);
child.wait().await.expect("reap the root");
let err = child
.kill_tree()
.expect_err("a root that is gone leaves no tree to walk: nothing was killed");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert!(!child.tree_killed(), "a walk that found no root killed nothing");
let mark = crate::log_capture::mark();
drop(child);
let records = drop_warns_since(mark);
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn, "{records:?}");
}
#[cfg(not(target_os = "macos"))]
#[skuld::test]
async fn drop_after_an_incomplete_tree_walk_kill_and_wait_still_warns() {
use crate::containment::treewalk::fault::LeftBehind;
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, stdin) = crate::test_child::held_treewalk_blocker_leaving_async(LeftBehind::Descendant);
let left = child
.kill_tree()
.expect_err("a tree walk that left a member is an error");
assert!(matches!(left, crate::error::Error::MemberNotKilled { .. }), "{left:?}");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().await.expect("reap the root"));
let mark = crate::log_capture::mark();
drop(child);
let records = drop_warns_since(mark);
assert_eq!(records.len(), 1, "{records:?}");
assert_eq!(records[0].0, log::Level::Warn);
}
#[cfg(target_os = "linux")]
pub(super) use crate::test_groups::cgroup_leaf as cgroup_common;
#[cfg(target_os = "linux")]
#[skuld::test]
async fn cgroup_drop_after_wait_still_kills_the_tree_and_does_not_warn(#[fixture(cgroup)] _group: &Group) {
crate::tokio::test_runtime::assert_current_thread();
use ::tokio::io::AsyncReadExt as _;
crate::log_capture::install();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args([
"sh",
"-c",
"exec 3<&0; { cat <&3 3<&- && echo eof; } & echo started; exit 0",
]);
cmd.contain_with(ContainMode::Strongest);
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.stdout(crate::Stdio::pipe_out()).expect("stdout pipe");
cmd.stderr(crate::Stdio::pipe_out()).expect("stderr pipe");
let mut child = cmd.spawn().expect("spawn");
assert_eq!(child.containment(), Containment::CgroupV2);
let leaf = match &child.os.attached {
crate::containment::Attached::Cgroup(leaf) => leaf.path().to_path_buf(),
other => panic!("a cgroup child holds its leaf, got {other:?}"),
};
let _leaf_cleanup = cgroup_common::LeafGuard::new(leaf.clone());
let mut started = [0u8; 8];
let mut stdout = child.stdout().expect("stdout pipe");
stdout.read_exact(&mut started).await.expect("read `started`");
let stdin = child.stdin().expect("stdin pipe");
let mut stderr = child.stderr().expect("stderr pipe");
child.wait().await.expect("reap the root");
let release = crate::child::front_cgroup_tests::release_after_cgroup_kill(stdin);
let mark = crate::log_capture::mark();
drop(child);
release.assert_killed();
assert_eq!(drop_warns_since(mark), []);
let mut rest = Vec::new();
stderr
.read_to_end(&mut rest)
.await
.expect("the cgroup kill ended the cat, closing stderr");
let mut said = Vec::new();
stdout
.read_to_end(&mut said)
.await
.expect("every holder of stdout is gone");
crate::child::front_cgroup_tests::assert_nothing_ended_by_itself(&said);
assert!(
!leaf.exists(),
"the drop must remove the drained leaf: {}",
leaf.display()
);
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn cgroup_drop_of_an_exited_unreaped_root_kills_its_orphaned_descendant(#[fixture(cgroup)] _group: &Group) {
crate::tokio::test_runtime::assert_current_thread();
use ::tokio::io::AsyncReadExt as _;
crate::log_capture::install();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args([
"sh",
"-c",
"exec 3<&0; { cat <&3 3<&- && echo eof; } & echo started; exit 0",
]);
cmd.contain_with(ContainMode::Strongest);
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.stdout(crate::Stdio::pipe_out()).expect("stdout pipe");
cmd.stderr(crate::Stdio::pipe_out()).expect("stderr pipe");
let mut child = cmd.spawn().expect("spawn");
assert_eq!(child.containment(), Containment::CgroupV2);
let leaf = match &child.os.attached {
crate::containment::Attached::Cgroup(leaf) => leaf.path().to_path_buf(),
other => panic!("a cgroup child holds its leaf, got {other:?}"),
};
let _leaf_cleanup = cgroup_common::LeafGuard::new(leaf.clone());
let mut started = [0u8; 8];
let mut stdout = child.stdout().expect("stdout pipe");
stdout.read_exact(&mut started).await.expect("read `started`");
let stdin = child.stdin().expect("stdin pipe");
let mut stderr = child.stderr().expect("stderr pipe");
crate::test_child::wait_until_zombie(child.pid());
let release = crate::child::front_cgroup_tests::release_after_cgroup_kill(stdin);
let mark = crate::log_capture::mark();
drop(child);
release.assert_killed();
assert_eq!(drop_warns_since(mark), []);
let mut rest = Vec::new();
stderr
.read_to_end(&mut rest)
.await
.expect("the cgroup kill ended the cat, closing stderr");
let mut said = Vec::new();
stdout
.read_to_end(&mut said)
.await
.expect("every holder of stdout is gone");
crate::child::front_cgroup_tests::assert_nothing_ended_by_itself(&said);
assert!(
!leaf.exists(),
"the drop must remove the drained leaf: {}",
leaf.display()
);
}
#[cfg(target_os = "linux")]
#[skuld::test]
async fn cgroup_async_drop_alone_kills_the_members_and_removes_the_leaf(#[fixture(cgroup)] _group: &Group) {
crate::tokio::test_runtime::assert_current_thread();
use ::tokio::io::AsyncReadExt as _;
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args([
"sh",
"-c",
"exec 3<&0; { cat <&3 3<&- && echo eof; } & echo started; wait",
]);
cmd.contain_with(ContainMode::Strongest);
cmd.kill_on_drop(true);
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.stdout(crate::Stdio::pipe_out()).expect("stdout pipe");
let mut child = cmd.spawn().expect("spawn");
assert_eq!(child.containment(), Containment::CgroupV2);
let leaf = match &child.os.attached {
crate::containment::Attached::Cgroup(leaf) => leaf.path().to_path_buf(),
other => panic!("a cgroup child holds its leaf, got {other:?}"),
};
let _leaf_cleanup = cgroup_common::LeafGuard::new(leaf.clone());
let mut started = [0u8; 8];
let mut stdout = child.stdout().expect("stdout pipe");
stdout.read_exact(&mut started).await.expect("read `started`");
assert_eq!(
&started,
b"started
"
);
let stdin = child.stdin().expect("stdin pipe");
let mut member = crate::test_spawn::spawn(
std::process::Command::new("cat")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null()),
)
.expect("spawn a member");
let member_stdin = member.stdin.take().expect("the member's piped stdin");
std::fs::write(leaf.join("cgroup.procs"), member.id().to_string()).expect("place the member in the leaf");
assert_eq!(
crate::child::front_cgroup_tests::cgroup_dir_of(member.id()),
leaf.to_str().expect("a utf-8 path"),
"the member must be in the tree's leaf"
);
let release = crate::child::front_cgroup_tests::release_after_cgroup_kill((stdin, member_stdin));
release.observe(member.id());
release.observe(child.pid());
drop(child);
release.assert_killed();
member.wait().expect("reap the member");
let mut said = Vec::new();
stdout
.read_to_end(&mut said)
.await
.expect("the root and the cat it left are gone");
crate::child::front_cgroup_tests::assert_nothing_ended_by_itself(&said);
assert!(
!leaf.exists(),
"the drop must drain and remove the leaf: {}",
leaf.display()
);
}
#[track_caller]
fn assert_skipped_at_debug(mark: usize) {
let records = drop_warns_since(mark);
assert_eq!(records.len(), 1, "exactly one skip record, got {records:?}");
assert_eq!(records[0].0, log::Level::Debug, "{records:?}");
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn fd_marker_drop_after_wait_sweeps_the_marker_holders_but_names_nothing_by_the_roots_number() {
crate::tokio::test_runtime::assert_current_thread();
use ::tokio::io::AsyncReadExt as _;
crate::log_capture::install();
let recorder = record_kill_group();
let walks = record_walks();
let sweeps = crate::containment::fdmarker::fault::record_holder_kills();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(["sh", "-c", "exec 3<&0; cat <&3 3<&- >/dev/null & echo started; exit 0"]);
cmd.contain_with(ContainMode::Strongest);
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.stdout(crate::Stdio::pipe_out()).expect("stdout pipe");
cmd.stderr(crate::Stdio::pipe_out()).expect("stderr pipe");
let mut child = cmd.spawn().expect("spawn");
assert_eq!(child.containment(), Containment::FdMarker);
let mut out = String::new();
child
.stdout()
.expect("stdout pipe")
.read_to_string(&mut out)
.await
.expect("read stdout to the root's exit");
assert_eq!(out, "started\n");
let _stdin = child.stdin().expect("stdin pipe");
let mut stderr = child.stderr().expect("stderr pipe");
child.wait().await.expect("reap the root");
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(recorder.killed(), Vec::<i32>::new(), "no killpg after the reap");
assert_eq!(walks.walked(), Vec::<u32>::new(), "no ppid walk from the reaped root");
assert!(!sweeps.killed().is_empty(), "the marker-holder sweep still runs");
assert_eq!(drop_warns_since(mark).len(), 1);
let mut rest = Vec::new();
stderr
.read_to_end(&mut rest)
.await
.expect("the sweep ended the cat, closing stderr, with stdin still held");
}
#[cfg(target_os = "macos")]
async fn reaped_blocker_root(mode: ContainMode) -> (u32, crate::tokio::Child) {
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(crate::Stdio::pipe()).expect("stdin pipe");
cmd.contain_with(mode);
let mut child = cmd.spawn().expect("spawn");
let stdin = child.stdin().expect("stdin pipe");
assert_eq!(child.containment(), Containment::FdMarker);
let pid = child.pid();
child.kill().expect("kill the root");
drop(stdin);
crate::test_child::assert_killed("the root", child.wait().await.expect("reap the root"));
(pid, child)
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn fd_marker_without_a_group_drop_after_wait_only_sweeps_the_marker_holders() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let walks = record_walks();
let (pid, child) = reaped_blocker_root(ContainMode::TreeWalk).await;
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(walks.walked(), Vec::<u32>::new(), "no ppid walk from the reaped root");
let warns = drop_warns_since(mark);
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(warns[0].1.contains(&format!("root pid {pid}")), "{warns:?}");
}
#[cfg(target_os = "macos")]
#[skuld::test]
async fn fd_marker_with_a_group_drop_after_wait_neither_signals_the_group_nor_walks() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let recorder = record_kill_group();
let walks = record_walks();
let (pid, child) = reaped_blocker_root(ContainMode::Strongest).await;
let mark = crate::log_capture::mark();
drop(child);
assert_eq!(recorder.killed(), Vec::<i32>::new(), "no killpg after the reap");
assert_eq!(walks.walked(), Vec::<u32>::new(), "no ppid walk from the reaped root");
let warns = drop_warns_since(mark);
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(warns[0].1.contains(&format!("pgid {pid}")), "{warns:?}");
}