Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 83 additions & 40 deletions crates/webcodex-environment/src/process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,95 @@ fn deadlines_and_output_bounds_are_enforced() {
}

#[cfg(target_os = "linux")]
fn running(pid: u32) -> bool {
std::fs::read_to_string(format!("/proc/{pid}/stat"))
.ok()
.and_then(|s| {
s.rsplit_once(')')
.map(|(_, s)| !s.trim_start().starts_with('Z'))
fn group_has_live_processes(pgid: u32) -> bool {
std::fs::read_dir("/proc")
.unwrap()
.filter_map(Result::ok)
.filter_map(|entry| std::fs::read_to_string(entry.path().join("stat")).ok())
.any(|stat| {
let Some((_, fields)) = stat.rsplit_once(')') else {
return false;
};
let mut fields = fields.split_whitespace();
let state = fields.next();
let _parent = fields.next();
let group = fields.next().and_then(|value| value.parse::<u32>().ok());
state != Some("Z") && group == Some(pgid)
})
.unwrap_or(false)
}

#[cfg(target_os = "linux")]
fn assert_group_cleanup(script: &str, expect_success: bool) {
use std::io::{Read, Seek, SeekFrom};
use std::os::fd::AsRawFd;
use std::os::unix::process::CommandExt;

// Publish the owned PGID before exec, while spawn still owns its handshake.
// A shell-created PID file can be truncated when the capture deadline kills
// the group before echo writes, so it is not a reliable fixture identity.
let mut marker = tempfile::tempfile().unwrap();
let fd = marker.as_raw_fd();
let mut command = Command::new("/bin/sh");
command.args(["-c", script]);
// SAFETY: the hook uses only async-signal-safe libc calls and an inherited
// descriptor kept alive until capture returns; it never allocates or locks.
unsafe {
command.pre_exec(move || {
let pgid = libc::getpgrp() as u32;
if pgid != libc::getpid() as u32 {
return Err(io::Error::from_raw_os_error(libc::EINVAL));
}
let bytes = pgid.to_ne_bytes();
if libc::write(fd, bytes.as_ptr().cast(), bytes.len()) != bytes.len() as isize {
return Err(io::Error::last_os_error());
}
Ok(())
});
}
let started = Instant::now();
let result = command.output_with_limits(Duration::from_millis(200), 1024);
marker.seek(SeekFrom::Start(0)).unwrap();
let mut bytes = [0; 4];
marker.read_exact(&mut bytes).unwrap();
let pgid = u32::from_ne_bytes(bytes);
let deadline = Instant::now() + Duration::from_secs(2);
while group_has_live_processes(pgid) && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(5));
}
let alive = group_has_live_processes(pgid);
if alive {
unsafe {
libc::kill(-(pgid as i32), libc::SIGKILL);
}
}
assert!(!alive, "capture returned with a live owned process group");
if expect_success {
assert!(result.unwrap().status.success());
} else {
assert!(result.is_err());
}
eprintln!(
"PROCESS_CLEANUP leader_exit=true descendant_alive=false reader_threads=0 elapsed_ms={:.3}",
started.elapsed().as_secs_f64() * 1000.0
);
}

#[cfg(target_os = "linux")]
#[test]
fn exited_leader_never_leaves_pipe_holding_descendants_or_reader_threads() {
let tmp = tempfile::tempdir().unwrap();
for script in [
"sleep 30 & echo $! > \"$PID_FILE\"; exit 0",
"sleep 30 >&- 2>&- & echo $! > \"$PID_FILE\"; exit 0",
"sleep 30 & echo $! > \"$PID_FILE\"; while :; do printf 123456789; done",
for (script, expect_success) in [
("sleep 30 & exit 0", false),
("sleep 30 >&- 2>&- & exit 0", true),
("sleep 30 & while :; do printf 123456789; done", false),
] {
let path = tmp.path().join("pid");
let started = Instant::now();
let result = Command::new("/bin/sh")
.args(["-c", script])
.env("PID_FILE", &path)
.output_with_limits(Duration::from_millis(200), 1024);
let pid: u32 = std::fs::read_to_string(&path)
.unwrap()
.trim()
.parse()
.unwrap();
let deadline = Instant::now() + Duration::from_secs(2);
while running(pid) && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(5));
}
let alive = running(pid);
if alive {
unsafe {
libc::kill(pid as i32, libc::SIGKILL);
}
}
assert!(!alive, "capture returned with a live owned descendant");
if script.contains(">&-") {
assert!(result.unwrap().status.success());
} else {
assert!(result.is_err());
}
eprintln!("PROCESS_CLEANUP leader_exit=true descendant_alive=false reader_threads=0 elapsed_ms={:.3}", started.elapsed().as_secs_f64()*1000.0);
assert_group_cleanup(script, expect_success);
}
}

#[cfg(target_os = "linux")]
#[test]
fn deadline_before_shell_fixture_publication_still_checks_group_cleanup() {
// Deterministically let the capture deadline arrive before shell-side
// fixture publication. Identity is already committed by the spawn hook.
assert_group_cleanup("sleep 30 & sleep 1; exit 0", false);
}
Loading