Skip to content
Open
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
2 changes: 1 addition & 1 deletion crates/sandstorm-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ hyper = { version = "1", features = ["client", "http1"] }
hyper-util = { version = "0.1", features = ["tokio"] }
http-body = "1"
bytes = "1"
tempfile = "3"
# The front door hands API requests to the E2B router as a service.
tower = { version = "0.5", features = ["util"] }

[dev-dependencies]
tempfile = "3"
http-body-util = "0.1"
futures-util = "0.3"
104 changes: 83 additions & 21 deletions crates/sandstorm-core/src/node/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,22 @@ impl Runsc {
fn run(&self, args: &[&str], timeout: Duration) -> Result<String> {
let tmp = self.work_dir.join("tmp");
std::fs::create_dir_all(&tmp)?;
let stem = format!("runsc-{}", std::process::id());
let (out_path, err_path) = (
tmp.join(format!("{stem}.out")),
tmp.join(format!("{stem}.err")),
);
let out_file = std::fs::File::create(&out_path)?;
let err_file = std::fs::File::create(&err_path)?;
let stdout = tempfile::NamedTempFile::new_in(&tmp)?;
let stderr = tempfile::NamedTempFile::new_in(&tmp)?;
// Reopen for independent read and write offsets, then unlink the
// paths before spawning so even a timed-out call leaves no files.
let stdout_writer = stdout.reopen()?;
let stderr_writer = stderr.reopen()?;
let stdout_reader = stdout.into_file();
let stderr_reader = stderr.into_file();

let mut child = Exec::new(&self.bin)
.arg("--root")
.arg(&self.root)
.args(args)
.stdin(Stdio::null())
.stdout(out_file)
.stderr(err_file)
.stdout(stdout_writer)
.stderr(stderr_writer)
.spawn()
.with_context(|| format!("running {} {:?}", self.bin, args))?;

Expand All @@ -242,15 +243,11 @@ impl Runsc {
}
};

let stdout = std::fs::read_to_string(&out_path).unwrap_or_default();
let stderr = std::fs::read_to_string(&err_path).unwrap_or_default();
std::fs::remove_file(&out_path).ok();
std::fs::remove_file(&err_path).ok();

if !status.success() {
let stderr = std::io::read_to_string(stderr_reader).unwrap_or_default();
bail!("{} {:?} failed: {}", self.bin, args, stderr.trim());
}
Ok(stdout)
Ok(std::io::read_to_string(stdout_reader).unwrap_or_default())
}
}

Expand Down Expand Up @@ -433,17 +430,20 @@ mod tests {
use tempfile::TempDir;

/// A `runsc` that logs each subcommand and fails on `fail_on`.
///
/// Written by a child, not from here. A test forking on another thread
/// while this process holds the file open for writing keeps that
/// descriptor until it execs, and running the script then fails with
/// ETXTBSY, which looks like `runsc` itself failing.
fn runsc(dir: &TempDir, fail_on: &str) -> Runsc {
let bin = dir.path().join("runsc");
let script = format!(
"#!/bin/sh\nshift 2\necho \"$1\" >> {}/calls\n[ \"$1\" != \"{fail_on}\" ]\n",
dir.path().display()
);
runsc_script(dir, &script)
}

/// Written by a child, not from here. A test forking on another thread
/// while this process holds the file open for writing keeps that
/// descriptor until it execs, and running the script then fails with
/// ETXTBSY, which looks like `runsc` itself failing.
fn runsc_script(dir: &TempDir, script: &str) -> Runsc {
let bin = dir.path().join("runsc");
let written = Exec::new("sh")
.args(["-c", "printf %s \"$1\" > \"$2\" && chmod 755 \"$2\"", "sh"])
.arg(script)
Expand All @@ -464,6 +464,68 @@ mod tests {
.replace('\n', " ")
}

#[test]
fn concurrent_runsc_calls_keep_their_own_output() {
let dir = TempDir::new().unwrap();
let rt = runsc_script(
&dir,
r#"#!/bin/sh
shift 2
cd "$2" || exit 1
case "$1" in
first)
printf 'first stdout\n'
printf 'first stderr\n' >&2
: > first-ready
while [ ! -f second-ready ]; do sleep 0.01; done
;;
second)
printf 'second stdout\n'
printf 'second stderr\n' >&2
: > second-ready
while [ ! -f first-returned ]; do sleep 0.01; done
exit 1
;;
esac
"#,
);
let work_dir = dir.path().to_str().unwrap();
let timeout = Duration::from_secs(10);
// The second command writes before the first returns. It stays alive
// until the first caller has read its output.
let (first, second) = std::thread::scope(|scope| {
let second = scope.spawn(|| {
let ready = dir.path().join("first-ready");
let deadline = Instant::now() + timeout;
while !ready.exists() {
assert!(Instant::now() < deadline, "first command did not write");
std::thread::sleep(Duration::from_millis(10));
}
rt.run(&["second", work_dir], timeout)
});
let first = rt.run(&["first", work_dir], timeout);
std::fs::write(dir.path().join("first-returned"), []).unwrap();
(first, second.join().unwrap())
});

assert_eq!(first.unwrap(), "first stdout\n");
let err = second.unwrap_err().to_string();
assert!(err.ends_with("failed: second stderr"), "{err}");
}

#[test]
fn a_timed_out_runsc_call_leaves_no_output_files() {
let dir = TempDir::new().unwrap();
let rt = runsc_script(&dir, "#!/bin/sh\nexec sleep 30\n");
let err = rt.run(&["delete"], Duration::from_millis(50)).unwrap_err();

assert!(err.to_string().contains("did not finish within"), "{err}");
assert!(std::fs::read_dir(dir.path().join("tmp"))
.unwrap()
.next()
.is_none());
}

#[test]
fn a_start_that_fails_removes_the_container() {
let dir = TempDir::new().unwrap();
Expand Down
Loading