From 3f3cc93ce68652da4ff31c44ed9cc290a66f7a74 Mon Sep 17 00:00:00 2001 From: Eugene Volen Date: Tue, 27 Oct 2026 14:15:54 +0200 Subject: [PATCH] test(therock): hold the env lock while spawning the sleeping child wait_with_output_bounded_kills_a_slow_child_at_the_deadline and wait_with_output_bounded_waits_unbounded_when_no_timeout_is_given start a sleeping child by program name (`powershell` on Windows, `sleep` elsewhere), so the program is found through PATH. render_update_json_installs_progress_guard replaces the process-wide PATH with its own directory for the length of its body, under PROCESS_ENV_TEST_LOCK, which these two tests did not take. When they ran inside that window the spawn failed with "program not found" and the test panicked, intermittently and only in a full run, which also made the pre-push `cargo test` hook fail on unrelated changes. spawn_test_sleep now takes the same lock for the length of the spawn (poison-tolerant, so one failing test does not cascade). A new test holds the lock with PATH emptied, starts the child from another thread, and only then puts PATH back; without the lock in spawn_test_sleep it fails with the same "program not found" error. Signed-off-by: Eugene Volen --- apps/rocm/src/therock.rs | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/apps/rocm/src/therock.rs b/apps/rocm/src/therock.rs index d60e977404..5918af4e56 100644 --- a/apps/rocm/src/therock.rs +++ b/apps/rocm/src/therock.rs @@ -9263,8 +9263,20 @@ mod tests { Ok(()) } + /// Takes the lock that serializes tests which replace process-wide + /// environment variables. The program is started by name, so it is found + /// through `PATH`, and another test replaces `PATH` for the duration of its + /// body; a spawn that lands in that window fails with "program not found" + /// and the test that wanted a sleeping child panics. + fn lock_process_env_for_spawn() -> std::sync::MutexGuard<'static, ()> { + PROCESS_ENV_TEST_LOCK + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + } + #[cfg(not(windows))] fn spawn_test_sleep(seconds: u64) -> std::process::Child { + let _env = lock_process_env_for_spawn(); Command::new("sleep") .arg(seconds.to_string()) .stdin(Stdio::null()) @@ -9280,6 +9292,7 @@ mod tests { // wrong reason. `Start-Sleep` has no such requirement. #[cfg(windows)] fn spawn_test_sleep(seconds: u64) -> std::process::Child { + let _env = lock_process_env_for_spawn(); Command::new("powershell") .args([ "-NoProfile", @@ -9293,6 +9306,36 @@ mod tests { .expect("spawn Start-Sleep") } + /// The sleeping child is found through `PATH`, so starting it while another + /// test has replaced `PATH` must wait for that test rather than fail. This + /// holds the environment lock with `PATH` emptied, starts the child from + /// another thread, and only then puts `PATH` back: without the lock in + /// `spawn_test_sleep` the spawn runs at once and panics. + #[test] + #[allow(unsafe_code)] // std::env::set_var is unsafe in edition 2024 + fn spawning_the_test_sleep_waits_while_another_test_has_replaced_path() { + let holder = PROCESS_ENV_TEST_LOCK.lock().unwrap(); + let old_path = std::env::var_os("PATH"); + unsafe { + std::env::set_var("PATH", ""); + } + let spawner = std::thread::spawn(|| spawn_test_sleep(30)); + std::thread::sleep(Duration::from_millis(300)); + unsafe { + match old_path { + Some(value) => std::env::set_var("PATH", value), + None => std::env::remove_var("PATH"), + } + } + drop(holder); + + let mut child = spawner + .join() + .expect("the spawn must wait for the lock instead of failing while PATH is replaced"); + child.kill().ok(); + child.wait().ok(); + } + /// A budget-constrained caller (the startup update check) must get its /// child back, not blocked past the budget it asked for. #[test]