From e4b18eb632d8d65fbd8479c57eab3de356a2d84f Mon Sep 17 00:00:00 2001 From: Abir Maity Date: Thu, 3 Dec 2026 10:24:48 +0530 Subject: [PATCH 1/2] test(services): invoke /bin/sh directly in process tests Fixes #1351 (cherry picked from commit 46abd6903a50172f6fa2b54acf6625dc0348e836) --- crates/nostream-services/src/process.rs | 49 ++++++------------------- 1 file changed, 12 insertions(+), 37 deletions(-) diff --git a/crates/nostream-services/src/process.rs b/crates/nostream-services/src/process.rs index b02ce6ebf..93813a6b0 100644 --- a/crates/nostream-services/src/process.rs +++ b/crates/nostream-services/src/process.rs @@ -271,32 +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 { - use std::io::Write; - - let path = directory.join(name); - // Write to a uniquely named temp file in the same directory, close the write descriptor, - // mark it executable, and rename it into place so the target path never exists while - // still open for writing (narrows the ETXTBSY fork/exec window across parallel tests). - let mut temp = tempfile::NamedTempFile::new_in(directory).expect("a temporary shim file"); - temp.write_all(format!("#!/bin/bash\n{body}\n").as_bytes()) - .expect("the shim is written"); - let (file, temp_path) = temp.keep().expect("the temporary shim stays"); - drop(file); - let mut permissions = std::fs::metadata(&temp_path) - .expect("the shim exists") - .permissions(); - permissions.set_mode(0o755); - std::fs::set_permissions(&temp_path, permissions).expect("the shim is executable"); - std::fs::rename(&temp_path, &path).expect("the shim is moved into place"); - 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())) @@ -343,10 +319,12 @@ 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; + let captured = capture( + &argv("/bin/sh", &["-c", "exec sleep 30"]), + Some(Duration::from_millis(150)), + ) + .await; assert_eq!(captured, CapturedResult::TimedOut); assert!( started.elapsed() < Duration::from_secs(5), @@ -356,24 +334,21 @@ 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 + // The environment the caller adds reaches the child: the shell 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$'", - ); let code = run( - &argv(&env_check, &[]), + &argv("/bin/sh", &["-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/sh", &["-c", "printf '%s\\n' \"$@\"", "sh", "two"]), + None, + ) + .await; let CapturedResult::Exited { code, stdout, .. } = captured else { panic!("the child exits"); }; From fff97ca9505d585e05b8b6848c1324b4689933ee Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Thu, 3 Dec 2026 08:34:32 -0500 Subject: [PATCH 2/2] test(services): add a changeset for the direct-exec process tests --- .changeset/process-tests-direct-exec.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .changeset/process-tests-direct-exec.md diff --git a/.changeset/process-tests-direct-exec.md b/.changeset/process-tests-direct-exec.md new file mode 100644 index 000000000..235385c38 --- /dev/null +++ b/.changeset/process-tests-direct-exec.md @@ -0,0 +1,9 @@ +--- +--- + +test(services): invoke /bin/sh directly in the process tests + +The `shim()` helper wrote an executable script and then executed it, so another +test forking in the interval could let the child inherit the write descriptor and +make the exec fail with `ETXTBSY`. The tests now pass their program text to +`/bin/sh -c`, so nothing is written before it is run. No runtime behaviour changes.