Skip to content
Merged
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
51 changes: 38 additions & 13 deletions crates/axis-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,10 @@ async fn run_command(
startup_trace: None,
};

// Registered before the create, which already makes the tmpdir on
// some backends, so a SIGTERM from then on stops the sandbox
// instead of ending AXIS without its cleanup.
let terminated = terminate_signal()?;
let mut sandbox =
axis_sandbox::Sandbox::create(config).map_err(|e| anyhow::anyhow!("{e}"))?;
sandbox.start().map_err(|e| anyhow::anyhow!("{e}"))?;
Expand All @@ -915,14 +919,18 @@ async fn run_command(
// Don't install a Ctrl+C handler, because it steals the
// TTY from the child process and breaks TUI apps like
// Claude Code (setRawMode fails).
let code = wait_for_interactive_standalone_sandbox(&mut sandbox).await?;
let code =
wait_for_interactive_standalone_sandbox(&mut sandbox, terminated).await?;
sandbox.destroy().ok();
std::process::exit(code);
} else {
// Non-interactive: wait for process or Ctrl+C. destroy()
// MUST run after the wait too: wait() only kills the local
// frontend (e.g. on timeout), so without an explicit
// destroy() the backend's VM/DomU would be orphaned.
// Non-interactive: wait for process, Ctrl+C or SIGTERM.
// destroy() MUST run after the wait too: wait() only kills
// the local frontend (e.g. on timeout), so without an
// explicit destroy() the backend's VM/DomU would be orphaned.
// A caller's timeout sends SIGTERM: destroy() then stops the
// command and removes the sandbox's tmpdir, which the next
// run in this workspace needs gone.
tokio::select! {
code = sandbox.wait() => {
let code = code.map_err(|e| anyhow::anyhow!("{e}"))?;
Expand All @@ -932,6 +940,10 @@ async fn run_command(
_ = tokio::signal::ctrl_c() => {
sandbox.destroy().map_err(|e| anyhow::anyhow!("{e}"))?;
}
() = terminated => {
Comment thread
ltsabadz marked this conversation as resolved.
Resolved
sandbox.destroy().map_err(|e| anyhow::anyhow!("{e}"))?;
std::process::exit(TERMINATED_EXIT_CODE);
}
}
}
}
Expand Down Expand Up @@ -984,25 +996,38 @@ fn resolve_policy_yaml(policy: &std::path::Path) -> anyhow::Result<String> {
Ok(yaml.to_string())
}

#[cfg(unix)]
async fn wait_for_interactive_standalone_sandbox(
sandbox: &mut axis_sandbox::Sandbox,
terminated: impl std::future::Future<Output = ()>,
) -> Result<i32> {
let mut terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
tokio::select! {
code = sandbox.wait() => Ok(code.map_err(|e| anyhow::anyhow!("{e}"))?),
_ = terminate.recv() => {
() = terminated => {
sandbox.destroy().map_err(|e| anyhow::anyhow!("{e}"))?;
Ok(128 + libc::SIGTERM)
Ok(TERMINATED_EXIT_CODE)
}
}
}

/// The status `axis run` exits with when SIGTERM stopped its sandbox.
#[cfg(unix)]
const TERMINATED_EXIT_CODE: i32 = 128 + libc::SIGTERM;
#[cfg(not(unix))]
async fn wait_for_interactive_standalone_sandbox(
sandbox: &mut axis_sandbox::Sandbox,
) -> Result<i32> {
sandbox.wait().await.map_err(|e| anyhow::anyhow!("{e}"))
const TERMINATED_EXIT_CODE: i32 = 1;

/// Settles when this process gets SIGTERM, from the call on; never where
/// there is no SIGTERM.
#[cfg(unix)]
fn terminate_signal() -> Result<impl std::future::Future<Output = ()>> {
let mut terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
Ok(async move {
terminate.recv().await;
})
}

#[cfg(not(unix))]
fn terminate_signal() -> Result<impl std::future::Future<Output = ()>> {
Ok(std::future::pending())
}

/// Git-style subcommand extension.
Expand Down
11 changes: 11 additions & 0 deletions crates/axis-sandbox/src/linux/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ fn ensure_owned_private_directory_fd(
Ok(())
}

/// Opens `path` as a directory without following a symlink in any component.
pub(crate) fn open_directory_no_symlinks(path: &Path) -> Result<std::fs::File, String> {
use std::os::fd::FromRawFd;

let fd = open_directory_tree_no_symlinks(path)?;
let raw = fd.raw();
std::mem::forget(fd);
// SAFETY: `raw` is an open descriptor that `fd` owned and no longer closes.
Ok(unsafe { std::fs::File::from_raw_fd(raw) })
}

struct DirectoryFd(RawFd);

impl DirectoryFd {
Expand Down
Loading
Loading