use std::sync::Arc;
use std::time::{Duration, Instant};
use super::fixtures::{park_holder, spawn_std_blocker, Blocker};
use crate::child::shared::seams::{self, ForcedWait};
use crate::child::shared::SharedChild;
use crate::identity::ProcessId;
use crate::wait::backend::fault;
use crate::wait::exit_only::seams::{self as exit_seams, ForcedReap, HolderStep};
fn far() -> Instant {
Instant::now() + Duration::from_secs(3600)
}
#[skuld::test]
fn a_blocking_waitid_that_finds_no_record_is_a_contract_breach() {
let mut b = Blocker::spawn();
b.end_child_and_confirm_exit();
let target = b.shared.target().expect("a pidfd");
let forced = exit_seams::force_visible_none_once();
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
crate::wait::exit_only::wait_visible_exit(&target)
}));
drop(forced);
if cfg!(debug_assertions) {
assert!(outcome.is_err(), "a debug build asserts the contract");
} else {
let err = outcome.expect("release returns").expect_err("a breach is an error");
assert!(err.to_string().contains("no record"), "{err}");
}
}
#[skuld::test]
fn a_blocking_reap_that_finds_no_record_is_a_contract_breach() {
let mut b = Blocker::spawn();
b.end_child_and_confirm_exit();
let target = b.shared.target().expect("a pidfd");
let forced = exit_seams::force_visible_none_once();
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
crate::wait::exit_only::reap_blocking(&target)
}));
drop(forced);
if cfg!(debug_assertions) {
assert!(outcome.is_err(), "a debug build asserts the contract");
} else {
let err = outcome.expect("release returns").expect_err("a breach is an error");
assert!(err.to_string().contains("no record"), "{err}");
}
}
#[skuld::test]
fn an_unbounded_holder_whose_reap_finds_none_blocks_in_waitid() {
let mut b = Blocker::spawn();
b.end_child_and_confirm_exit();
let _none = exit_seams::force_reap_once(ForcedReap::None);
exit_seams::holder_steps();
b.shared.wait().expect("wait");
assert_eq!(
exit_seams::holder_steps(),
[
HolderStep::Poll,
HolderStep::Reap,
HolderStep::BlockingWaitid,
HolderStep::Reap
]
);
}
#[skuld::test]
fn a_deadline_holder_whose_reap_finds_none_backs_off() {
let mut b = Blocker::spawn();
b.end_child_and_confirm_exit();
let _none = exit_seams::force_reap_once(ForcedReap::None);
exit_seams::holder_steps();
b.shared.wait_deadline(far()).expect("wait_deadline").expect("a status");
assert_eq!(
exit_seams::holder_steps(),
[
HolderStep::Poll,
HolderStep::Reap,
HolderStep::Backoff,
HolderStep::Reap
]
);
}
#[skuld::test]
fn a_holder_in_its_blocking_waitid_holds_no_lock() {
let mut b = Blocker::spawn();
b.end_child_and_confirm_exit();
let (at_waitid_tx, at_waitid_rx) = std::sync::mpsc::channel();
let (release_tx, release_rx) = std::sync::mpsc::channel::<()>();
let holder = std::thread::spawn({
let shared = Arc::clone(&b.shared);
move || {
let _none = exit_seams::force_reap_once(ForcedReap::None);
let _hook = exit_seams::on_holder_step(HolderStep::BlockingWaitid, move || {
_ = at_waitid_tx.send(());
_ = release_rx.recv();
});
shared.wait()
}
});
at_waitid_rx.recv().expect("the holder must reach its blocking waitid");
let printed = format!("{:?}", b.shared);
assert!(printed.contains("W {"), "the holder is in W: {printed}");
assert!(
!printed.contains("locked"),
"the blocking waitid must hold no lock: {printed}"
);
b.shared.kill().expect("kill must not wait for the holder");
drop(release_tx);
holder.join().expect("holder").expect("wait");
}
#[skuld::test]
fn a_deadline_poll_arms_the_remaining_time_and_peeks_once_at_expiry() {
let b = Blocker::spawn();
let (_clock, at) = crate::wait::test_clock::FrozenClockGuard::install();
let deadline = at + Duration::from_millis(50);
crate::wait::block_probe::take();
exit_seams::holder_steps();
let peeked = exit_seams::force_peek_once(Ok(crate::wait::exit_only::Peek::Running));
assert_eq!(
b.shared
.wait_deadline(deadline)
.expect("a running child is not an error"),
None
);
assert!(
exit_seams::take_forced_peek().is_none(),
"the wait must take its final peek at expiry"
);
drop(peeked);
let armed = crate::wait::block_probe::take();
assert!(!armed.is_empty(), "the wait must have blocked at least once");
let mut previous = Duration::from_millis(50);
assert_eq!(
armed[0],
Some(previous),
"the first round must be armed with the whole remaining time"
);
for a in armed {
let a = a.expect("a deadline wait arms a bounded timeout");
assert!(a <= previous, "armed {a:?} with only {previous:?} remaining");
previous = a;
}
assert_eq!(exit_seams::holder_steps(), [HolderStep::Poll, HolderStep::FinalPeek]);
assert!(crate::wait::now() >= deadline);
}
#[skuld::test]
fn a_deadline_condvar_wait_is_clamped_to_the_remaining_time() {
let mut b = Blocker::spawn();
let holder = park_holder(&b.shared, None, || ());
let (blocked_tx, blocked_rx) = std::sync::mpsc::channel();
let (again_tx, again_rx) = std::sync::mpsc::channel();
let waiter = std::thread::spawn({
let shared = Arc::clone(&b.shared);
move || {
let (_clock, at) = crate::wait::test_clock::FrozenClockGuard::install();
let _first = seams::on_condvar_block(move || {
_ = blocked_tx.send(());
let _second = seams::on_condvar_block(move || {
_ = again_tx.send(());
});
std::mem::forget(_second);
});
let _forced = seams::force_unlocked_wait(ForcedWait::Panic);
crate::wait::block_probe::take();
let got = shared.wait_deadline(at + Duration::from_secs(3600));
(got, crate::wait::block_probe::take())
}
});
blocked_rx.recv().expect("the waiter must block in round one");
{
let _lock = b.shared.lock();
b.shared.condvar.notify_all();
}
again_rx
.recv()
.expect("the spurious wake must send the waiter round again");
b.end_child();
holder.release_and_join().expect("holder wait").expect("a status");
let (got, armed) = waiter.join().expect("waiter");
assert!(matches!(got, Ok(Some(_))), "{got:?}");
let armed: Vec<Duration> = armed.into_iter().map(|a| a.expect("bounded")).collect();
assert!(armed.len() >= 2, "a second round must have been armed: {armed:?}");
assert!(armed[0] <= Duration::from_secs(3600));
assert!(
armed[1] < armed[0],
"the second round must be armed from the recomputed remaining time: {armed:?}"
);
}
#[skuld::test]
fn a_deadline_beyond_the_block_limit_is_armed_clamped() {
let (child, stdin) = spawn_std_blocker();
let id = super::fixtures::identity_of(&child);
let shared = SharedChild::adopt(child, id).unwrap_or_else(|(e, _)| panic!("adopt: {e}"));
let mut stdin = Some(stdin);
let _end = exit_seams::on_holder_step(HolderStep::Poll, move || drop(stdin.take()));
crate::wait::block_probe::take();
let deadline = Instant::now() + Duration::from_secs(u64::from(u32::MAX));
shared
.wait_deadline(deadline)
.expect("a far deadline is not an error")
.expect("the child was ended, so the wait reports its exit");
let armed = crate::wait::block_probe::take();
assert!(!armed.is_empty(), "the wait must have polled");
for a in armed {
let a = a.expect("a deadline wait arms a bounded poll");
assert!(a <= crate::wait::MAX_BLOCK, "armed {a:?}");
}
}
fn adopt_gone_on(errno: rustix::io::Errno) {
super::fixtures::assert_adoption_is_gone(|| fault::force_pidfd_open_errno_once(errno));
}
#[skuld::test]
fn adopt_on_einval_takes_the_gone_path() {
adopt_gone_on(rustix::io::Errno::INVAL);
}
#[skuld::test]
fn adopt_on_enoent_takes_the_gone_path() {
adopt_gone_on(rustix::io::Errno::NOENT);
}
#[skuld::test]
fn adopt_treats_a_pidfd_whose_identity_is_gone_as_gone() {
super::fixtures::assert_adoption_is_gone(|| fault::force_exists_once(crate::identity::Existence::Gone));
}
#[skuld::test]
fn adopt_treats_a_pidfd_that_is_not_our_child_as_gone() {
let parent = unsafe { libc::getppid() } as u32;
let id = ProcessId::of(parent).found();
let opened = crate::wait::backend::open_own_child(parent, id).expect("open");
assert!(opened.is_none(), "our parent is never our child");
}
fn adopt_under_view_with_exists_gone(view: crate::identity::proc_view_fault::ForcedView) {
let (child, stdin) = spawn_std_blocker();
let id = super::fixtures::identity_of(&child);
let gone = fault::force_exists_once(crate::identity::Existence::Gone);
let forced_view = crate::identity::proc_view_fault::force_proc_view_once(view);
let shared = SharedChild::adopt(child, id).expect("adopt");
drop(forced_view);
drop(gone);
shared.kill().expect("the child is kept, with a pidfd, and killable");
drop(stdin);
shared.wait().expect("wait");
}
#[skuld::test]
fn adopt_skips_the_identity_check_on_a_diverged_proc() {
adopt_under_view_with_exists_gone(crate::identity::proc_view_fault::ForcedView::Diverged);
}
#[skuld::test]
fn adopt_on_an_unassessable_proc_view_skips_the_identity_check() {
adopt_under_view_with_exists_gone(crate::identity::proc_view_fault::ForcedView::Unassessable);
}
const SLOT_MARKER: &str = "COSCA_TEST_SHARED_STDIO_SLOT";
const SLOT_CASE_ENV: &str = "COSCA_TEST_SHARED_STDIO_SLOT_CASE";
#[skuld::test]
fn an_adopted_pidfd_never_sits_in_a_stdio_slot() {
if !crate::test_child::is_marked_fixture_reexec(SLOT_MARKER) {
for case in ["0", "1", "2"] {
crate::test_child::run_fixture_case(
crate::test_child::fixture_path!(an_adopted_pidfd_never_sits_in_a_stdio_slot),
SLOT_MARKER,
SLOT_CASE_ENV,
case,
);
}
return;
}
let slot: i32 = std::env::var(SLOT_CASE_ENV).expect("case").parse().expect("slot");
let (child, stdin) = spawn_std_blocker();
let id = super::fixtures::identity_of(&child);
let saved = std::os::fd::IntoRawFd::into_raw_fd(
rustix::io::fcntl_dupfd_cloexec(unsafe { rustix::fd::BorrowedFd::borrow_raw(slot) }, 10)
.expect("dup the slot aside"),
);
assert!(saved >= 10);
assert_eq!(unsafe { libc::close(slot) }, 0);
let shared = SharedChild::adopt(child, id).unwrap_or_else(|(e, _)| panic!("adopt: {e}"));
let fd = std::os::fd::AsRawFd::as_raw_fd(shared.pidfd.as_ref().expect("a pidfd"));
assert_eq!(unsafe { libc::dup2(saved, slot) }, slot);
assert!(fd >= 3, "the pidfd took the closed stdio slot: {fd}");
drop(stdin);
shared.wait().expect("wait");
}