diff --git a/crates/registry-rstack-dev/src/guard.rs b/crates/registry-rstack-dev/src/guard.rs index 1173cf44d2..332765fb92 100644 --- a/crates/registry-rstack-dev/src/guard.rs +++ b/crates/registry-rstack-dev/src/guard.rs @@ -675,7 +675,49 @@ pub(super) fn runtime_groups(root: &Path) -> Result> { #[cfg(test)] mod tests { use super::*; - use std::{os::unix::net::UnixStream, sync::mpsc}; + use std::os::unix::net::UnixStream; + + const FIXTURE_SHUTDOWN_WAIT: Duration = Duration::from_secs(4); + + struct GuardFixture { + guard: process::Child, + } + + impl GuardFixture { + fn spawn(command: &mut Command) -> Result { + let launch = serde_json::to_string(&command.test_launch)?; + let guard = command.command.env(TEST_GUARD_LAUNCH, launch).spawn()?; + Ok(Self { guard }) + } + + fn shutdown(&mut self, timeout: Duration) -> Result { + drop(self.guard.stdin.take()); + let deadline = Instant::now() + timeout; + loop { + let status = self + .guard + .try_wait() + .context("cannot reap the process guard")?; + if let Some(status) = status { + return Ok(status); + } + let remaining = deadline.saturating_duration_since(Instant::now()); + ensure!(!remaining.is_zero(), "the process guard did not exit"); + thread::sleep(POLL.min(remaining)); + } + } + } + + impl Drop for GuardFixture { + fn drop(&mut self) { + // Close the controller on unwind too. Even after an assertion's + // deadline, keep the child until the guard finishes group cleanup. + drop(self.guard.stdin.take()); + if let Err(error) = self.guard.wait() { + eprintln!("cannot reap the fixture guard after closing its controller: {error}"); + } + } + } fn lock_file(root: &Path) -> PathBuf { let path = root.join("guard.lock"); @@ -833,51 +875,31 @@ mod tests { #[test] fn guard_owns_and_reaps_the_workload_process_group() { let scratch = tempfile::tempdir().unwrap(); - let lock = lock_file(scratch.path()); - let pid_file = scratch.path().join("guard.pid"); - let state_file = scratch.path().join("guard.state"); let nested_file = scratch.path().join("nested.pid"); - let (mut controller, worker) = UnixStream::pair().unwrap(); - let (finished_tx, finished_rx) = mpsc::sync_channel(1); - let mut command = process::Command::new("/bin/sh"); + // Acquire the lock in the guard process, outside the parallel test process. + let mut command = + Command::new(scratch.path(), "guard-fixture", Path::new("/bin/sh")).unwrap(); command .args([ "-c", r#"sh -c 'trap "" TERM; printf "%s" "$$" > "$1.tmp" && mv "$1.tmp" "$1" && exec sleep 60' sh "$1" & wait"#, "guard-fixture", ]) - .arg(&nested_file) - .process_group(0) - .stdin(Stdio::null()); - let guard_lock = lock.clone(); - let guard_pid = pid_file.clone(); - let guard_state = state_file.clone(); - let guard_thread = thread::spawn(move || { - let status = supervise( - &guard_lock, - &guard_pid, - &guard_state, - &mut controller, - command, - ) - .unwrap(); - finished_tx.send(status).unwrap(); - }); + .arg(&nested_file); + let mut fixture = GuardFixture::spawn(&mut command).unwrap(); - let leader = wait_pid(&pid_file); + let leader = wait_pid(&command.test_launch.pid_file); let nested = wait_pid(&nested_file); assert_eq!(rustix::process::getpgid(Some(leader)).unwrap(), leader); assert_eq!(rustix::process::getpgid(Some(nested)).unwrap(), leader); - let contender = open_lock(&lock).unwrap(); + let contender = open_lock(&command.test_launch.lock).unwrap(); assert_eq!( flock(&contender, FlockOperation::NonBlockingLockExclusive).unwrap_err(), rustix::io::Errno::WOULDBLOCK ); - drop(worker); - finished_rx.recv_timeout(Duration::from_secs(4)).unwrap(); - guard_thread.join().unwrap(); - complete_state(&state_file).unwrap(); + assert!(fixture.shutdown(FIXTURE_SHUTDOWN_WAIT).unwrap().success()); + complete_state(&command.test_launch.state_file).unwrap(); flock(&contender, FlockOperation::NonBlockingLockExclusive).unwrap(); assert_eq!( rustix::process::getpgid(Some(leader)).unwrap_err(), @@ -914,6 +936,46 @@ mod tests { } } + #[test] + fn guard_fixture_reaps_after_a_shutdown_timeout() { + let scratch = tempfile::tempdir().unwrap(); + let mut command = + Command::new(scratch.path(), "guard-timeout", Path::new("/bin/sleep")).unwrap(); + command.arg("60"); + let mut fixture = GuardFixture::spawn(&mut command).unwrap(); + let guard = direct_child_pid(&fixture.guard).unwrap(); + let leader = wait_pid(&command.test_launch.pid_file); + // Keep the guard running until the shutdown attempt observes its deadline. + let controller = + rustix::io::fcntl_dupfd_cloexec(fixture.guard.stdin.as_ref().unwrap(), 0).unwrap(); + let contender = open_lock(&command.test_launch.lock).unwrap(); + + let error = fixture.shutdown(Duration::ZERO).unwrap_err(); + assert_eq!(error.to_string(), "the process guard did not exit"); + assert!(!direct_child_exited(&fixture.guard).unwrap()); + assert_eq!( + flock(&contender, FlockOperation::NonBlockingLockExclusive).unwrap_err(), + rustix::io::Errno::WOULDBLOCK + ); + drop(controller); + drop(fixture); + + assert_eq!( + waitid( + WaitId::Pid(guard), + WaitIdOptions::EXITED | WaitIdOptions::NOHANG | WaitIdOptions::NOWAIT, + ) + .unwrap_err(), + rustix::io::Errno::CHILD + ); + complete_state(&command.test_launch.state_file).unwrap(); + flock(&contender, FlockOperation::NonBlockingLockExclusive).unwrap(); + assert_eq!( + rustix::process::getpgid(Some(leader)).unwrap_err(), + rustix::io::Errno::SRCH + ); + } + #[test] fn runtime_group_report_excludes_provisioning_tools() { let scratch = tempfile::tempdir().unwrap();