use std::cell::Cell;
use crate::test_child::Console;
use std::rc::Rc;
use super::drop_fault;
use super::HANDED_TO_ORPHAN_REAPER as ORPHAN_REAPER;
use crate::child::spawn::identity_macos_tests::{has_not_exited, is_reaped, refuse_the_kill};
use crate::child::spawn::late_report_tests::{
assert_kill_sent, assert_one_warn, SAID_KILLED, SAID_NOT_SENT, SAID_REAPED, SAID_REFUSED_RUNNING,
SAID_STILL_RUNNING, ZOMBIE,
};
use crate::child::spawn::unique_report;
use crate::error::ChildFate;
use crate::identity::{uniq_fault, ReadPurpose, UniqRead};
use crate::wait::exit_only::seams::force_peek_once;
const LEAKS: &str = "forgetting tokio's handle for it leaks";
fn tokio_blocker() -> (crate::tokio::Command, crate::PipeWriter) {
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
(cmd, writer)
}
struct Late {
fate: ChildFate,
pid: u32,
id: crate::identity::ProcessId,
mark: usize,
forgets: u32,
}
fn late_spawn(arm: impl FnOnce() -> Vec<Box<dyn std::any::Any>>) -> (Late, crate::PipeWriter) {
crate::log_capture::install();
let (mut cmd, writer) = tokio_blocker();
let forgets = drop_fault::record();
let mark = crate::log_capture::mark();
let err = {
let _armed = arm();
let _late = unique_report::seams::find_the_report_unwritten();
cmd.spawn().expect_err("a late child is not adopted")
};
let (_err, fate) = crate::child::spawn::failure::expect_may_have_started_with(err);
let Some(crate::identity::Resolved::Found(id)) = crate::child::spawn::fault::take_captured() else {
panic!("the spawn captured the child");
};
let late = Late {
fate,
pid: id.pid(),
id,
mark,
forgets: forgets.forgets(),
};
(late, writer)
}
#[skuld::test]
async fn macos_tokio_spawn_of_a_late_reporting_child_kills_and_reaps_it_through_its_id() {
let sent = Rc::new(crate::send_log::Capture::start());
let kill_missing = Rc::new(Cell::new(false));
let (late, writer) = {
crate::log_capture::install();
let (mut cmd, writer) = tokio_blocker();
let release = Rc::new(Cell::new(Some(writer)));
let _hook = crate::child::spawn::fault::set_between_kill_and_wait({
let (sent, missing, release) = (Rc::clone(&sent), Rc::clone(&kill_missing), Rc::clone(&release));
move || {
missing.set(
sent.entries()
.iter()
.all(|(_, sig, _)| *sig != crate::signal::Sig::Kill),
);
drop(release.take());
}
});
let forgets = drop_fault::record();
let mark = crate::log_capture::mark();
let err = {
let _late = unique_report::seams::find_the_report_unwritten();
cmd.spawn().expect_err("a late child is not adopted")
};
assert!(!kill_missing.get(), "the kill must be sent before the wait");
let (_err, fate) = crate::child::spawn::failure::expect_may_have_started_with(err);
let Some(crate::identity::Resolved::Found(id)) = crate::child::spawn::fault::take_captured() else {
panic!("the spawn captured the child");
};
(
Late {
fate,
pid: id.pid(),
id,
mark,
forgets: forgets.forgets(),
},
release,
)
};
drop(writer);
assert_eq!(late.fate, ChildFate::Reaped);
assert_kill_sent(late.pid, &sent.entries());
assert!(is_reaped(late.pid), "nothing is left to wait for");
assert_eq!(late.forgets, 1, "tokio's `Child` is forgotten, once");
assert_one_warn(
late.pid,
late.mark,
&[SAID_REAPED, LEAKS],
&[SAID_REFUSED_RUNNING, SAID_NOT_SENT, ORPHAN_REAPER],
);
}
#[skuld::test]
fn macos_tokio_late_teardown_inside_a_local_set_on_a_multi_thread_runtime_reaps_the_child() {
let runtime = ::tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_all()
.build()
.unwrap();
let local = ::tokio::task::LocalSet::new();
let (flavor, fate) = runtime.block_on(local.run_until(async {
let flavor = ::tokio::runtime::Handle::current().runtime_flavor();
let (late, writer) = late_spawn(Vec::new);
drop(writer);
(flavor, late.fate)
}));
assert_eq!(flavor, ::tokio::runtime::RuntimeFlavor::MultiThread);
assert_eq!(fate, ChildFate::Reaped);
}
#[skuld::test]
async fn macos_tokio_spawn_of_a_late_child_whose_kill_is_refused_hands_it_to_the_orphan_reaper() {
let (late, writer) = late_spawn(|| vec![Box::new(refuse_the_kill())]);
assert_eq!(late.fate, ChildFate::Running { id: None });
assert_eq!(late.forgets, 0, "a running child is released, not forgotten");
assert!(has_not_exited(late.pid), "nothing ended the child");
assert_one_warn(
late.pid,
late.mark,
&[SAID_REFUSED_RUNNING, SAID_STILL_RUNNING, ORPHAN_REAPER],
&["was sent SIGKILL", "did not end it", LEAKS, ZOMBIE],
);
assert_eq!(unsafe { libc::kill(late.pid as libc::pid_t, libc::SIGKILL) }, 0);
while late.id.exists() == crate::identity::Existence::Present {
::tokio::task::yield_now().await;
}
assert_eq!(
late.id.exists(),
crate::identity::Existence::Gone,
"tokio's orphan reaper collects the zombie"
);
drop(writer);
}
#[skuld::test]
async fn macos_tokio_spawn_of_a_late_child_whose_pid_is_gone_sends_nothing_and_is_gone() {
let sent = crate::send_log::Capture::start();
let (late, writer) = late_spawn(|| {
vec![Box::new(uniq_fault::force_uniq_read_once(
ReadPurpose::Kill,
UniqRead::Gone,
))]
});
assert_eq!(late.fate, ChildFate::Gone);
assert_eq!(sent.entries(), [], "nothing is sent to a pid that is gone");
assert_eq!(late.forgets, 1);
assert_one_warn(
late.pid,
late.mark,
&[SAID_NOT_SENT, "it is gone", LEAKS],
&["was sent SIGKILL", "signalled"],
);
crate::child::spawn::identity_macos_tests::end_unsignalled_and_reap(late.pid);
drop(writer);
}
#[skuld::test]
async fn macos_tokio_spawn_of_a_late_child_with_a_refused_kill_and_read_is_unknown() {
let (late, writer) = late_spawn(|| {
vec![
Box::new(refuse_the_kill()),
Box::new(uniq_fault::force_uniq_read_once(
ReadPurpose::Running,
UniqRead::Refused(libc::EPERM),
)),
]
});
assert_eq!(late.fate, ChildFate::Unknown);
assert_eq!(late.forgets, 1);
assert_one_warn(
late.pid,
late.mark,
&[SAID_REFUSED_RUNNING, "what became of it is unknown", LEAKS],
&[ORPHAN_REAPER],
);
crate::child::spawn::identity_macos_tests::end_unsignalled_and_reap(late.pid);
drop(writer);
}
#[skuld::test]
async fn macos_tokio_spawn_of_a_late_child_whose_wait_fails_is_killed() {
let (late, writer) = late_spawn(|| {
vec![Box::new(force_peek_once(Err(std::io::Error::other(
"forced wait failure",
))))]
});
assert_eq!(late.fate, ChildFate::Killed);
assert_eq!(late.forgets, 1);
assert_one_warn(
late.pid,
late.mark,
&[SAID_KILLED, "forced wait failure", LEAKS],
&[SAID_REAPED],
);
crate::child::spawn::identity_macos_tests::reap_by_pid(late.pid);
drop(writer);
}
#[skuld::test]
async fn macos_tokio_spawn_of_a_late_front_leaves_it_running_and_says_so() {
use crate::elevation::{front::front, ElevatedVia};
crate::log_capture::install();
let front = front(Some(&ElevatedVia::MacosOsascript));
let (mut cmd, writer) = tokio_blocker();
cmd.set_elevation_front(front);
let sent = crate::send_log::Capture::start();
let forgets = drop_fault::record();
let mark = crate::log_capture::mark();
let err = {
let _late = unique_report::seams::find_the_report_unwritten();
cmd.spawn().expect_err("a late front is not adopted")
};
let (err, fate) = crate::child::spawn::failure::expect_may_have_started_with(err);
assert_eq!(fate, ChildFate::Running { id: None });
let Some(crate::identity::Resolved::Found(id)) = crate::child::spawn::fault::take_captured() else {
panic!("the spawn captured the child");
};
let pid = id.pid();
assert_eq!(sent.entries(), [], "a front is sent nothing");
assert_eq!(forgets.forgets(), 1);
assert!(has_not_exited(pid), "the front is left running");
assert_one_warn(
pid,
mark,
&["elevation front", "left unreaped", ZOMBIE, LEAKS],
&["was sent SIGKILL", SAID_REFUSED_RUNNING, SAID_NOT_SENT],
);
assert!(err.to_string().contains("; left unreaped"), "{err}");
crate::child::spawn::identity_macos_tests::end_unsignalled_and_reap(pid);
drop(writer);
}
#[skuld::test]
async fn macos_tokio_spawn_of_a_late_contained_child_kills_its_tree() {
use crate::child::spawn::late_report_tests::{tree_pipes, TREE_ARGV};
crate::log_capture::install();
let (tree, ends) = tree_pipes();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(TREE_ARGV).contain();
for (fd, end) in ends {
cmd.fd(fd, end).expect("set the fd");
}
let tree = Rc::new(std::cell::RefCell::new(Some(tree)));
let _hook = crate::child::spawn::fault::set_at(crate::child::spawn::fault::SpawnPoint::BeforeLateTeardown, {
let tree = Rc::clone(&tree);
move || tree.borrow_mut().as_mut().expect("the tree").await_grandchild()
});
let mark = crate::log_capture::mark();
let err = {
let _late = unique_report::seams::find_the_report_unwritten();
cmd.spawn().expect_err("a late child is not adopted")
};
let (_err, fate) = crate::child::spawn::failure::expect_may_have_started_with(err);
let Some(crate::identity::Resolved::Found(id)) = crate::child::spawn::fault::take_captured() else {
panic!("the spawn captured the child");
};
let tree = tree.borrow_mut().take().expect("the tree");
assert!(
!tree.grandchild_survived(),
"the late child's grandchild must be killed"
);
assert_eq!(fate, ChildFate::Reaped, "the root is killed and reaped");
assert_one_warn(id.pid(), mark, &[SAID_REAPED, LEAKS], &["tree was not", "fully killed"]);
}
#[skuld::test]
fn macos_tokio_spawn_that_pipes_a_slot_with_fds_0_and_1_closed_adopts_its_child() {
use crate::child::spawn::identity_macos_tests::RAN_ARGV;
use crate::child::spawn::late_report_tests::{close_fds_0_and_1, null_stdio};
use crate::test_own_process::{own_process, test_path};
use crate::test_spawn::spawn;
let Some(done) = own_process(
test_path!(macos_tokio_spawn_that_pipes_a_slot_with_fds_0_and_1_closed_adopts_its_child),
spawn,
) else {
return;
};
let runtime = ::tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(RAN_ARGV);
cmd.stdin(null_stdio()).expect("set stdin");
cmd.stdout(crate::stdio::Stdio::pipe_out()).expect("pipe stdout");
let restore = close_fds_0_and_1(&done);
runtime.block_on(async {
let mut child = cmd.spawn().expect("a child whose exec std saw is adopted");
drop(child.stdout().expect("the piped stdout"));
child.wait().await.expect("reap the child");
});
drop(restore);
}