use std::os::windows::io::{AsRawHandle as _, FromRawHandle as _, OwnedHandle};
use windows::Win32::Foundation::{HANDLE, WAIT_OBJECT_0};
use windows::Win32::System::Threading::{
GetExitCodeProcess, OpenProcess, WaitForSingleObject, INFINITE, PROCESS_QUERY_LIMITED_INFORMATION,
PROCESS_SYNCHRONIZE,
};
use crate::error::ChildFate;
use crate::log_capture::warns_since;
use super::proc_source::{ProcSource, Waited};
fn exited_unreaped_child() -> (::tokio::process::Child, u32) {
let child = crate::test_spawn::spawn_tokio(
::tokio::process::Command::new("cmd")
.args(["/C", "exit 0"])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null()),
)
.expect("spawn");
let pid = child.id().expect("tokio owns an un-reaped child");
let handle = child.raw_handle().expect("tokio holds the handle");
unsafe { WaitForSingleObject(HANDLE(handle), INFINITE) };
(child, pid)
}
fn blocker_child() -> (
::tokio::process::Child,
u32,
crate::child::spawn::fault::TeardownBlocker,
) {
let mut cmd = ::tokio::process::Command::new(crate::test_child::windows_more());
cmd.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null());
let mut child = crate::test_spawn::spawn_tokio(&mut cmd).expect("spawn the blocker");
let pid = child.id().expect("tokio owns an un-reaped child");
let stdin = child.stdin.take().expect("piped stdin");
(
child,
pid,
crate::child::spawn::fault::release_between_kill_and_wait(stdin),
)
}
pub(super) fn observer_handle(pid: u32) -> OwnedHandle {
let handle = unsafe { OpenProcess(PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, false, pid) }
.expect("open the child to observe it");
unsafe { OwnedHandle::from_raw_handle(handle.0) }
}
#[skuld::test]
async fn reap_now_with_an_exit_under_way_and_a_failed_wait_is_killed() {
crate::tokio::test_runtime::assert_current_thread();
let (child, pid) = exited_unreaped_child();
let proc = ProcSource::new(child, pid);
let failed = super::fault::fail_handle_wait();
let fate = proc.reap_now(pid, None);
assert!(failed.taken(), "the teardown made the wait that failed");
assert_eq!(fate, ChildFate::Killed);
}
#[skuld::test]
async fn reap_now_with_a_delivered_kill_and_a_failed_wait_is_killed() {
crate::tokio::test_runtime::assert_current_thread();
let (child, pid, blocker) = blocker_child();
let observer = observer_handle(pid);
let proc = ProcSource::new(child, pid);
let failed = super::fault::fail_handle_wait();
let fate = proc.reap_now(pid, None);
assert!(failed.taken(), "the teardown made the wait that failed");
assert_eq!(fate, ChildFate::Killed);
assert_eq!(
unsafe { WaitForSingleObject(HANDLE(observer.as_raw_handle()), INFINITE) },
WAIT_OBJECT_0
);
let mut code = 0u32;
unsafe { GetExitCodeProcess(HANDLE(observer.as_raw_handle()), &mut code) }
.expect("read the exit code of a process that has exited");
assert_eq!(code, 1, "the teardown's kill must have ended the child");
assert!(
blocker.recorded().is_empty(),
"the wait failed, so no reap was made: {:?}",
blocker.recorded()
);
}
#[skuld::test]
async fn reap_now_of_an_exited_child_with_warnings_on_does_not_warn() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let mark = crate::log_capture::mark();
let (child, pid) = exited_unreaped_child();
let proc = ProcSource::new(child, pid);
let _warn = super::fault::warn_slow_teardowns();
let fate = proc.reap_now(pid, None);
assert_eq!(fate, ChildFate::Reaped);
assert_eq!(warns_since(mark, "still waiting"), Vec::<String>::new());
}
#[skuld::test]
async fn reap_now_of_a_running_child_with_warnings_on_warns_once_naming_it() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let mark = crate::log_capture::mark();
let (child, pid, _blocker) = blocker_child();
let proc = ProcSource::new(child, pid);
let _warn = super::fault::warn_slow_teardowns_after(std::time::Duration::ZERO);
let _refusal = crate::identity::windows_fault::refuse_next_terminate_as_exiting();
let fate = proc.reap_now(pid, None);
assert_eq!(fate, ChildFate::Reaped, "the wait goes on past the warn");
let warns = warns_since(mark, "still waiting");
assert_eq!(warns.len(), 1, "one warn: {warns:?}");
assert!(
warns[0].contains(&format!("child {pid} ")),
"the warn names the child: {warns:?}"
);
}
#[skuld::test]
fn a_wait_that_fails_on_a_real_handle_is_unverified() {
let pid = std::process::id();
let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid) }.expect("open ourselves");
let handle = unsafe { OwnedHandle::from_raw_handle(handle.0) };
let waited = super::proc_source::wait_for_exit(HANDLE(handle.as_raw_handle()), pid);
assert_eq!(waited, Waited::Unverified);
}