diff --git a/.changeset/process-shim-etxtbsy.md b/.changeset/process-shim-etxtbsy.md new file mode 100644 index 000000000..3c7051a75 --- /dev/null +++ b/.changeset/process-shim-etxtbsy.md @@ -0,0 +1,4 @@ +--- +--- + +test(services): run /bin/bash -c directly in process tests to eliminate ETXTBSY race (#1351) diff --git a/crates/nostream-services/src/process.rs b/crates/nostream-services/src/process.rs index 5ac024f5b..1792a838a 100644 --- a/crates/nostream-services/src/process.rs +++ b/crates/nostream-services/src/process.rs @@ -271,22 +271,8 @@ fn signal_name(_status: &std::process::ExitStatus) -> String { mod tests { #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] - use std::os::unix::fs::PermissionsExt; - use super::*; - /// Writes an executable shell script into `directory` under `name`. - fn shim(directory: &std::path::Path, name: &str, body: &str) -> String { - let path = directory.join(name); - std::fs::write(&path, format!("#!/bin/bash\n{body}\n")).expect("the shim is written"); - let mut permissions = std::fs::metadata(&path) - .expect("the shim exists") - .permissions(); - permissions.set_mode(0o755); - std::fs::set_permissions(&path, permissions).expect("the shim is executable"); - path.display().to_string() - } - fn argv(program: &str, arguments: &[&str]) -> Vec { std::iter::once(program.to_owned()) .chain(arguments.iter().map(|argument| (*argument).to_owned())) @@ -333,10 +319,14 @@ mod tests { #[tokio::test] async fn a_slow_child_times_out() { - let directory = tempfile::tempdir().expect("a temporary directory"); - let slow = shim(directory.path(), "slow", "sleep 30"); let started = std::time::Instant::now(); - let captured = capture(&argv(&slow, &[]), Some(Duration::from_millis(150))).await; + // Run /bin/bash -c directly instead of writing a temporary script file so no parallel + // test's fork can inherit a writable fd on the exec'd binary and trigger ETXTBSY. + let captured = capture( + &argv("/bin/bash", &["-c", "exec sleep 30"]), + Some(Duration::from_millis(150)), + ) + .await; assert_eq!(captured, CapturedResult::TimedOut); assert!( started.elapsed() < Duration::from_secs(5), @@ -346,24 +336,24 @@ mod tests { #[tokio::test] async fn run_answers_the_child_exit_code_and_capture_answers_its_output() { - let directory = tempfile::tempdir().expect("a temporary directory"); - // The environment the caller adds reaches the child: the shim answers 0 only when it - // finds the variable it was handed. - let env_check = shim( - directory.path(), - "env-check", - "env | grep -q '^NOSTREAM_PROBE=yes$'", - ); + // The environment the caller adds reaches the child: bash answers 0 only when it finds + // the variable it was handed. let code = run( - &argv(&env_check, &[]), + &argv("/bin/bash", &["-c", "env | grep -q '^NOSTREAM_PROBE=yes$'"]), &[("NOSTREAM_PROBE", "yes".to_owned())], ) .await .expect("the child runs"); assert_eq!(code, 0, "the added environment reaches the child"); - let echo = shim(directory.path(), "echoer", "printf '%s\\n' \"$@\""); - let captured = capture(&argv(&echo, &["two"]), None).await; + let captured = capture( + &argv( + "/bin/bash", + &["-c", "printf '%s\\n' \"$@\"", "echoer", "two"], + ), + None, + ) + .await; let CapturedResult::Exited { code, stdout, .. } = captured else { panic!("the child exits"); };