diff --git a/.changeset/process-shim-atomic-write.md b/.changeset/process-shim-atomic-write.md new file mode 100644 index 000000000..a845151cc --- /dev/null +++ b/.changeset/process-shim-atomic-write.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/crates/nostream-services/src/process.rs b/crates/nostream-services/src/process.rs index 5ac024f5b..b02ce6ebf 100644 --- a/crates/nostream-services/src/process.rs +++ b/crates/nostream-services/src/process.rs @@ -277,13 +277,23 @@ mod tests { /// 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); - std::fs::write(&path, format!("#!/bin/bash\n{body}\n")).expect("the shim is written"); - let mut permissions = std::fs::metadata(&path) + // 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(&path, permissions).expect("the shim is executable"); + 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() }