From 45fecacbb9e3ab213dc69c97a219c58a9883d79b Mon Sep 17 00:00:00 2001 From: Jeremi Joslin Date: Fri, 4 Dec 2026 08:18:57 +0000 Subject: [PATCH 1/3] test(rstack): isolate the guard lock lifetime from parallel tests Signed-off-by: Jeremi Joslin --- crates/registry-rstack-dev/src/guard.rs | 45 +++++++++---------------- 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/crates/registry-rstack-dev/src/guard.rs b/crates/registry-rstack-dev/src/guard.rs index 1173cf44d2..025d7e356b 100644 --- a/crates/registry-rstack-dev/src/guard.rs +++ b/crates/registry-rstack-dev/src/guard.rs @@ -675,7 +675,7 @@ 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; fn lock_file(root: &Path) -> PathBuf { let path = root.join("guard.lock"); @@ -833,51 +833,36 @@ 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 child = command.spawn().unwrap(); - let leader = wait_pid(&pid_file); + let leader = child.pid; 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(); + drop(child.controller.take()); + let deadline = Instant::now() + Duration::from_secs(4); + while !child.exited().unwrap() { + assert!(Instant::now() < deadline, "the process guard did not exit"); + thread::sleep(POLL); + } + assert!(child.stop().unwrap().success()); flock(&contender, FlockOperation::NonBlockingLockExclusive).unwrap(); assert_eq!( rustix::process::getpgid(Some(leader)).unwrap_err(), From 0c24ef6e1ce232a7efe7eeb453a7502262c457d4 Mon Sep 17 00:00:00 2001 From: Jeremi Joslin Date: Fri, 4 Dec 2026 08:42:00 +0000 Subject: [PATCH 2/3] test(rstack): reap the process guard after fixture failures Signed-off-by: Jeremi Joslin --- crates/registry-rstack-dev/src/guard.rs | 96 ++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 9 deletions(-) diff --git a/crates/registry-rstack-dev/src/guard.rs b/crates/registry-rstack-dev/src/guard.rs index 025d7e356b..58a793626f 100644 --- a/crates/registry-rstack-dev/src/guard.rs +++ b/crates/registry-rstack-dev/src/guard.rs @@ -677,6 +677,56 @@ mod tests { use super::*; 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()); + self.wait_for_exit(timeout) + } + + fn wait_for_exit(&mut self, timeout: Duration) -> Result { + 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) { + // Give the guard time to stop and reap the workload group after EOF. + if let Err(error) = self.shutdown(FIXTURE_SHUTDOWN_WAIT) { + eprintln!("fixture guard shutdown failed: {error:#}"); + if let Err(error) = self.guard.kill() { + eprintln!("cannot kill the fixture guard: {error}"); + } + if let Err(error) = self.wait_for_exit(FIXTURE_SHUTDOWN_WAIT) { + eprintln!("cannot reap the fixture guard after forced shutdown: {error:#}"); + } + } + } + } + fn lock_file(root: &Path) -> PathBuf { let path = root.join("guard.lock"); OpenOptions::new() @@ -844,9 +894,9 @@ mod tests { "guard-fixture", ]) .arg(&nested_file); - let mut child = command.spawn().unwrap(); + let mut fixture = GuardFixture::spawn(&mut command).unwrap(); - let leader = child.pid; + 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); @@ -856,13 +906,8 @@ mod tests { rustix::io::Errno::WOULDBLOCK ); - drop(child.controller.take()); - let deadline = Instant::now() + Duration::from_secs(4); - while !child.exited().unwrap() { - assert!(Instant::now() < deadline, "the process guard did not exit"); - thread::sleep(POLL); - } - assert!(child.stop().unwrap().success()); + 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(), @@ -899,6 +944,39 @@ 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 error = fixture.shutdown(Duration::ZERO).unwrap_err(); + drop(controller); + drop(fixture); + + assert_eq!(error.to_string(), "the process guard did not exit"); + 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(); + 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(); From aa5f954b4ad5fbc96150fd330a59de8f18a5353e Mon Sep 17 00:00:00 2001 From: Jeremi Joslin Date: Fri, 4 Dec 2026 08:57:03 +0000 Subject: [PATCH 3/3] test(rstack): reap the fixture guard cooperatively Signed-off-by: Jeremi Joslin --- crates/registry-rstack-dev/src/guard.rs | 27 ++++++++++++------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/crates/registry-rstack-dev/src/guard.rs b/crates/registry-rstack-dev/src/guard.rs index 58a793626f..332765fb92 100644 --- a/crates/registry-rstack-dev/src/guard.rs +++ b/crates/registry-rstack-dev/src/guard.rs @@ -692,10 +692,6 @@ mod tests { fn shutdown(&mut self, timeout: Duration) -> Result { drop(self.guard.stdin.take()); - self.wait_for_exit(timeout) - } - - fn wait_for_exit(&mut self, timeout: Duration) -> Result { let deadline = Instant::now() + timeout; loop { let status = self @@ -714,15 +710,11 @@ mod tests { impl Drop for GuardFixture { fn drop(&mut self) { - // Give the guard time to stop and reap the workload group after EOF. - if let Err(error) = self.shutdown(FIXTURE_SHUTDOWN_WAIT) { - eprintln!("fixture guard shutdown failed: {error:#}"); - if let Err(error) = self.guard.kill() { - eprintln!("cannot kill the fixture guard: {error}"); - } - if let Err(error) = self.wait_for_exit(FIXTURE_SHUTDOWN_WAIT) { - eprintln!("cannot reap the fixture guard after forced shutdown: {error:#}"); - } + // 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}"); } } } @@ -956,12 +948,18 @@ mod tests { // 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!(error.to_string(), "the process guard did not exit"); assert_eq!( waitid( WaitId::Pid(guard), @@ -971,6 +969,7 @@ mod tests { 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