Skip to content
Merged
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
43 changes: 43 additions & 0 deletions apps/rocm/src/therock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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",
Expand All @@ -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();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use the same poison-tolerant lock helper the new spawn_test_sleep uses here. With .unwrap(), one panicking env test poisons the mutex and this test then fails with a poison error instead of exercising the behavior. A small guard that restores PATH on drop would also keep a mid-test panic from leaking an empty PATH into other tests.

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]
Expand Down
Loading