Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .changeset/process-shim-etxtbsy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

test(services): run /bin/bash -c directly in process tests to eliminate ETXTBSY race (#1351)
46 changes: 18 additions & 28 deletions crates/nostream-services/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
std::iter::once(program.to_owned())
.chain(arguments.iter().map(|argument| (*argument).to_owned()))
Expand Down Expand Up @@ -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),
Expand All @@ -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");
};
Expand Down
Loading