#[cfg(any(feature = "async", feature = "sync"))]
use std::time::Duration;
#[cfg(feature = "async")]
use tokio::io::AsyncReadExt;
#[cfg(feature = "async")]
use tokio::process::Command;
#[cfg(any(feature = "async", feature = "sync"))]
use tracing::{debug, warn};
use crate::Claude;
#[cfg(any(feature = "async", feature = "sync"))]
use crate::error::{Error, Result};
pub(crate) fn full_command_args(claude: &Claude, args: Vec<String>) -> Vec<String> {
let mut command_args = claude.global_args.clone();
command_args.extend(args);
command_args
}
#[cfg(any(feature = "async", feature = "sync"))]
pub(crate) fn apply_child_environment(
cmd: &mut std::process::Command,
clear_env: bool,
env: &std::collections::HashMap<String, String>,
) {
if clear_env {
cmd.env_clear();
}
cmd.env_remove("CLAUDECODE");
cmd.env_remove("CLAUDE_CODE_ENTRYPOINT");
cmd.envs(env);
}
#[cfg(any(feature = "async", feature = "sync"))]
pub(crate) fn span_command(args: &[String]) -> &str {
args.first().map(String::as_str).unwrap_or("<none>")
}
#[cfg(any(feature = "async", feature = "sync"))]
pub(crate) fn exec_span(claude: &Claude, args: &[String], mode: &'static str) -> tracing::Span {
tracing::debug_span!(
"claude.exec",
command = span_command(args),
mode,
binary = %claude.binary.display(),
cwd = claude.working_dir.as_deref().map(|d| d.display().to_string()),
exit_code = tracing::field::Empty,
duration_ms = tracing::field::Empty,
)
}
#[cfg(any(feature = "async", feature = "sync"))]
pub(crate) fn record_exec_outcome(
span: &tracing::Span,
exit_code: i32,
started: std::time::Instant,
) {
span.record("exit_code", exit_code);
span.record("duration_ms", started.elapsed().as_millis() as u64);
}
#[derive(Debug, Clone)]
pub struct CommandOutput {
pub stdout: String,
pub stderr: String,
pub exit_code: i32,
pub success: bool,
}
#[cfg(any(feature = "async", feature = "sync"))]
#[derive(Clone, Copy)]
pub(crate) struct SpawnPolicy<'a> {
pub(crate) process_group: bool,
pub(crate) kill_grace: Option<Duration>,
pub(crate) die_with_parent: bool,
pub(crate) on_spawn: Option<&'a crate::SpawnObserver>,
}
#[cfg(any(feature = "async", feature = "sync"))]
impl SpawnPolicy<'_> {
pub(crate) fn of(claude: &Claude) -> SpawnPolicy<'_> {
SpawnPolicy {
process_group: claude.process_group,
kill_grace: claude.kill_grace,
die_with_parent: claude.die_with_parent,
on_spawn: claude.on_spawn.as_ref(),
}
}
}
#[cfg(any(feature = "async", feature = "sync"))]
pub(crate) fn arm_and_notify(
process_group: bool,
pid: Option<u32>,
on_spawn: Option<&crate::SpawnObserver>,
) -> GroupKillGuard {
if let (Some(pid), Some(observer)) = (pid, on_spawn) {
observer(crate::SpawnInfo {
pid,
pgid: process_group.then_some(pid),
});
}
GroupKillGuard::new_if(process_group, pid)
}
#[cfg(any(feature = "async", feature = "sync"))]
pub(crate) struct GroupKillGuard {
#[cfg(unix)]
pgid: Option<i32>,
}
#[cfg(any(feature = "async", feature = "sync"))]
impl GroupKillGuard {
pub(crate) fn new_if(enabled: bool, pid: Option<u32>) -> Self {
Self::new(if enabled { pid } else { None })
}
pub(crate) fn new(pid: Option<u32>) -> Self {
#[cfg(unix)]
{
Self {
pgid: pid.and_then(|p| i32::try_from(p).ok()),
}
}
#[cfg(not(unix))]
{
let _ = pid;
Self {}
}
}
pub(crate) fn disarm(&mut self) {
#[cfg(unix)]
{
self.pgid = None;
}
}
pub(crate) fn is_armed(&self) -> bool {
#[cfg(unix)]
{
self.pgid.is_some()
}
#[cfg(not(unix))]
{
false
}
}
pub(crate) fn term_now(&self) {
#[cfg(unix)]
if let Some(pgid) = self.pgid {
let _ = unsafe { libc::killpg(pgid, libc::SIGTERM) };
}
}
pub(crate) fn kill_now(&mut self) {
#[cfg(unix)]
if let Some(pgid) = self.pgid.take() {
let _ = unsafe { libc::killpg(pgid, libc::SIGKILL) };
}
}
}
#[cfg(any(feature = "async", feature = "sync"))]
impl Drop for GroupKillGuard {
fn drop(&mut self) {
self.kill_now();
}
}
#[must_use]
pub const fn die_with_parent_supported() -> bool {
cfg!(target_os = "linux")
}
#[cfg(all(unix, any(feature = "async", feature = "sync")))]
fn pdeathsig_hook() -> impl FnMut() -> std::io::Result<()> + Send + Sync + 'static {
let parent = std::process::id();
move || {
#[cfg(target_os = "linux")]
{
unsafe {
if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) != 0 {
return Err(std::io::Error::last_os_error());
}
if libc::getppid() as u32 != parent {
libc::_exit(1);
}
}
}
#[cfg(not(target_os = "linux"))]
{
let _ = parent;
}
Ok(())
}
}
#[cfg(feature = "async")]
pub(crate) fn apply_die_with_parent(cmd: &mut Command, enabled: bool) {
#[cfg(unix)]
if enabled {
unsafe {
cmd.pre_exec(pdeathsig_hook());
}
}
#[cfg(not(unix))]
{
let _ = (cmd, enabled);
}
}
#[cfg(feature = "sync")]
pub(crate) fn apply_die_with_parent_sync(cmd: &mut std::process::Command, enabled: bool) {
#[cfg(unix)]
if enabled {
use std::os::unix::process::CommandExt;
unsafe {
cmd.pre_exec(pdeathsig_hook());
}
}
#[cfg(not(unix))]
{
let _ = (cmd, enabled);
}
}
#[cfg(feature = "async")]
pub(crate) fn apply_process_group(cmd: &mut Command, enabled: bool) {
#[cfg(unix)]
if enabled {
cmd.process_group(0);
}
#[cfg(not(unix))]
{
let _ = (cmd, enabled);
}
}
#[cfg(feature = "sync")]
pub(crate) fn apply_process_group_sync(cmd: &mut std::process::Command, enabled: bool) {
#[cfg(unix)]
if enabled {
use std::os::unix::process::CommandExt;
cmd.process_group(0);
}
#[cfg(not(unix))]
{
let _ = (cmd, enabled);
}
}
#[cfg(feature = "async")]
pub(crate) async fn kill_group_with_grace(group: &mut GroupKillGuard, grace: Option<Duration>) {
if let Some(g) = grace
&& !g.is_zero()
&& group.is_armed()
{
group.term_now();
tokio::time::sleep(g).await;
}
group.kill_now();
}
#[cfg(feature = "sync")]
pub(crate) fn kill_group_with_grace_sync(group: &mut GroupKillGuard, grace: Option<Duration>) {
if let Some(g) = grace
&& !g.is_zero()
&& group.is_armed()
{
group.term_now();
std::thread::sleep(g);
}
group.kill_now();
}
#[cfg(feature = "async")]
pub async fn run_claude(claude: &Claude, args: Vec<String>) -> Result<CommandOutput> {
run_claude_with_retry(claude, args, None).await
}
#[cfg(feature = "async")]
pub async fn run_claude_with_retry(
claude: &Claude,
args: Vec<String>,
retry_override: Option<&crate::retry::RetryPolicy>,
) -> Result<CommandOutput> {
let policy = retry_override.or(claude.retry_policy.as_ref());
match policy {
Some(policy) => {
crate::retry::with_retry(policy, || run_claude_once(claude, args.clone())).await
}
None => run_claude_once(claude, args).await,
}
}
#[cfg(feature = "async")]
pub async fn run_claude_with_stdin_prompt(
claude: &Claude,
args: Vec<String>,
stdin_content: String,
) -> Result<CommandOutput> {
run_claude_with_stdin_prompt_internal(claude, args, stdin_content).await
}
#[cfg(feature = "async")]
async fn run_claude_with_stdin_prompt_internal(
claude: &Claude,
args: Vec<String>,
stdin_content: String,
) -> Result<CommandOutput> {
let command_args = full_command_args(claude, args);
let span = exec_span(claude, &command_args, "stdin");
let _enter = span.enter();
let started = std::time::Instant::now();
debug!(binary = %claude.binary.display(), args = ?command_args, "executing claude command (stdin prompt)");
let binary = &claude.binary;
let env = &claude.env;
let clear_env = claude.clear_env;
let working_dir = claude.working_dir.as_deref();
let result = if let Some(timeout) = claude.timeout {
run_with_timeout_stdin(
binary,
&command_args,
env,
clear_env,
working_dir,
timeout,
stdin_content,
SpawnPolicy::of(claude),
)
.await
} else {
run_internal_stdin(
binary,
&command_args,
env,
clear_env,
working_dir,
stdin_content,
SpawnPolicy::of(claude),
)
.await
};
if let Ok(output) = &result {
record_exec_outcome(&span, output.exit_code, started);
}
result
}
#[cfg(feature = "async")]
async fn run_internal_stdin(
binary: &std::path::Path,
args: &[String],
env: &std::collections::HashMap<String, String>,
clear_env: bool,
working_dir: Option<&std::path::Path>,
stdin_content: String,
policy: SpawnPolicy<'_>,
) -> Result<CommandOutput> {
let SpawnPolicy {
process_group,
kill_grace: _, die_with_parent,
on_spawn,
} = policy;
use tokio::io::AsyncWriteExt;
let mut cmd = Command::new(binary);
cmd.args(args);
cmd.stdin(std::process::Stdio::piped());
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
cmd.kill_on_drop(true);
apply_process_group(&mut cmd, process_group);
apply_die_with_parent(&mut cmd, die_with_parent);
apply_child_environment(cmd.as_std_mut(), clear_env, env);
if let Some(dir) = working_dir {
cmd.current_dir(dir);
}
let mut child = spawn_retrying_txtbsy(&mut cmd)
.await
.map_err(|e| Error::Io {
message: format!("failed to spawn claude: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
let mut group = arm_and_notify(process_group, child.id(), on_spawn);
if let Some(mut stdin) = child.stdin.take() {
stdin
.write_all(stdin_content.as_bytes())
.await
.map_err(|e| Error::Io {
message: format!("failed to write to claude stdin: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
}
let mut stdout_handle = child.stdout.take().expect("stdout was piped");
let mut stderr_handle = child.stderr.take().expect("stderr was piped");
let (status, stdout_str, stderr_str) = tokio::join!(
child.wait(),
drain(&mut stdout_handle),
drain(&mut stderr_handle),
);
let status = status.map_err(|e| Error::Io {
message: "failed to wait for claude process".to_string(),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
group.disarm();
let exit_code = status.code().unwrap_or(-1);
if !status.success() {
return Err(Error::from_command_failure(
format!("{} {}", binary.display(), args.join(" ")),
exit_code,
stdout_str,
stderr_str,
working_dir.map(|p| p.to_path_buf()),
));
}
Ok(CommandOutput {
stdout: stdout_str,
stderr: stderr_str,
exit_code,
success: true,
})
}
#[cfg(feature = "async")]
#[allow(clippy::too_many_arguments)]
async fn run_with_timeout_stdin(
binary: &std::path::Path,
args: &[String],
env: &std::collections::HashMap<String, String>,
clear_env: bool,
working_dir: Option<&std::path::Path>,
timeout: Duration,
stdin_content: String,
policy: SpawnPolicy<'_>,
) -> Result<CommandOutput> {
let SpawnPolicy {
process_group,
kill_grace,
die_with_parent,
on_spawn,
} = policy;
use tokio::io::AsyncWriteExt;
let mut cmd = Command::new(binary);
cmd.args(args);
cmd.stdin(std::process::Stdio::piped());
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
cmd.kill_on_drop(true);
apply_process_group(&mut cmd, process_group);
apply_die_with_parent(&mut cmd, die_with_parent);
apply_child_environment(cmd.as_std_mut(), clear_env, env);
if let Some(dir) = working_dir {
cmd.current_dir(dir);
}
let mut child = spawn_retrying_txtbsy(&mut cmd)
.await
.map_err(|e| Error::Io {
message: format!("failed to spawn claude: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
let mut group = arm_and_notify(process_group, child.id(), on_spawn);
if let Some(mut stdin) = child.stdin.take() {
stdin
.write_all(stdin_content.as_bytes())
.await
.map_err(|e| Error::Io {
message: format!("failed to write to claude stdin: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
}
let mut stdout_handle = child.stdout.take().expect("stdout was piped");
let mut stderr_handle = child.stderr.take().expect("stderr was piped");
let wait_and_drain = async {
let (status, stdout_str, stderr_str) = tokio::join!(
child.wait(),
drain(&mut stdout_handle),
drain(&mut stderr_handle),
);
(status, stdout_str, stderr_str)
};
match tokio::time::timeout(timeout, wait_and_drain).await {
Ok((Ok(status), stdout, stderr)) => {
group.disarm();
let exit_code = status.code().unwrap_or(-1);
if !status.success() {
return Err(Error::from_command_failure(
format!("{} {}", binary.display(), args.join(" ")),
exit_code,
stdout,
stderr,
working_dir.map(|p| p.to_path_buf()),
));
}
Ok(CommandOutput {
stdout,
stderr,
exit_code,
success: true,
})
}
Ok((Err(e), _stdout, _stderr)) => Err(Error::Io {
message: "failed to wait for claude process".to_string(),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
}),
Err(_) => {
kill_group_with_grace(&mut group, kill_grace).await;
let _ = child.kill().await;
let drain_budget = Duration::from_millis(200);
let stdout_str = tokio::time::timeout(drain_budget, drain(&mut stdout_handle))
.await
.unwrap_or_default();
let stderr_str = tokio::time::timeout(drain_budget, drain(&mut stderr_handle))
.await
.unwrap_or_default();
if !stdout_str.is_empty() || !stderr_str.is_empty() {
warn!(
stdout = %stdout_str,
stderr = %stderr_str,
"partial output from timed-out process",
);
}
Err(Error::Timeout {
timeout_seconds: timeout.as_secs(),
})
}
}
}
#[cfg(feature = "async")]
async fn run_claude_once(claude: &Claude, args: Vec<String>) -> Result<CommandOutput> {
let command_args = full_command_args(claude, args);
let span = exec_span(claude, &command_args, "oneshot");
let _enter = span.enter();
let started = std::time::Instant::now();
debug!(binary = %claude.binary.display(), args = ?command_args, "executing claude command");
let output = if let Some(timeout) = claude.timeout {
run_with_timeout(
&claude.binary,
&command_args,
&claude.env,
claude.clear_env,
claude.working_dir.as_deref(),
timeout,
SpawnPolicy::of(claude),
)
.await?
} else {
run_internal(
&claude.binary,
&command_args,
&claude.env,
claude.clear_env,
claude.working_dir.as_deref(),
SpawnPolicy::of(claude),
)
.await?
};
record_exec_outcome(&span, output.exit_code, started);
Ok(output)
}
#[cfg(feature = "async")]
pub async fn run_claude_allow_exit_codes(
claude: &Claude,
args: Vec<String>,
allowed_codes: &[i32],
) -> Result<CommandOutput> {
let output = run_claude(claude, args).await;
match output {
Err(Error::CommandFailed {
exit_code,
stdout,
stderr,
..
}) if allowed_codes.contains(&exit_code) => Ok(CommandOutput {
stdout,
stderr,
exit_code,
success: false,
}),
other => other,
}
}
#[cfg(feature = "async")]
async fn run_internal(
binary: &std::path::Path,
args: &[String],
env: &std::collections::HashMap<String, String>,
clear_env: bool,
working_dir: Option<&std::path::Path>,
policy: SpawnPolicy<'_>,
) -> Result<CommandOutput> {
let SpawnPolicy {
process_group,
kill_grace: _, die_with_parent,
on_spawn,
} = policy;
let mut cmd = Command::new(binary);
cmd.args(args);
cmd.stdin(std::process::Stdio::null());
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
cmd.kill_on_drop(true);
apply_process_group(&mut cmd, process_group);
apply_die_with_parent(&mut cmd, die_with_parent);
apply_child_environment(cmd.as_std_mut(), clear_env, env);
if let Some(dir) = working_dir {
cmd.current_dir(dir);
}
let mut child = spawn_retrying_txtbsy(&mut cmd)
.await
.map_err(|e| Error::Io {
message: format!("failed to spawn claude: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
let mut group = arm_and_notify(process_group, child.id(), on_spawn);
let mut stdout_handle = child.stdout.take().expect("stdout was piped");
let mut stderr_handle = child.stderr.take().expect("stderr was piped");
let (status, stdout, stderr) = tokio::join!(
child.wait(),
drain(&mut stdout_handle),
drain(&mut stderr_handle),
);
let status = status.map_err(|e| Error::Io {
message: "failed to wait for claude process".to_string(),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
group.disarm();
let exit_code = status.code().unwrap_or(-1);
if !status.success() {
return Err(Error::from_command_failure(
format!("{} {}", binary.display(), args.join(" ")),
exit_code,
stdout,
stderr,
working_dir.map(|p| p.to_path_buf()),
));
}
Ok(CommandOutput {
stdout,
stderr,
exit_code,
success: true,
})
}
#[cfg(feature = "async")]
async fn run_with_timeout(
binary: &std::path::Path,
args: &[String],
env: &std::collections::HashMap<String, String>,
clear_env: bool,
working_dir: Option<&std::path::Path>,
timeout: Duration,
policy: SpawnPolicy<'_>,
) -> Result<CommandOutput> {
let SpawnPolicy {
process_group,
kill_grace,
die_with_parent,
on_spawn,
} = policy;
let mut cmd = Command::new(binary);
cmd.args(args);
cmd.stdin(std::process::Stdio::null());
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
cmd.kill_on_drop(true);
apply_process_group(&mut cmd, process_group);
apply_die_with_parent(&mut cmd, die_with_parent);
apply_child_environment(cmd.as_std_mut(), clear_env, env);
if let Some(dir) = working_dir {
cmd.current_dir(dir);
}
let mut child = spawn_retrying_txtbsy(&mut cmd)
.await
.map_err(|e| Error::Io {
message: format!("failed to spawn claude: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
let mut group = arm_and_notify(process_group, child.id(), on_spawn);
let mut stdout = child.stdout.take().expect("stdout was piped");
let mut stderr = child.stderr.take().expect("stderr was piped");
let wait_and_drain = async {
let (status, stdout_str, stderr_str) =
tokio::join!(child.wait(), drain(&mut stdout), drain(&mut stderr));
(status, stdout_str, stderr_str)
};
match tokio::time::timeout(timeout, wait_and_drain).await {
Ok((Ok(status), stdout, stderr)) => {
group.disarm();
let exit_code = status.code().unwrap_or(-1);
if !status.success() {
return Err(Error::from_command_failure(
format!("{} {}", binary.display(), args.join(" ")),
exit_code,
stdout,
stderr,
working_dir.map(|p| p.to_path_buf()),
));
}
Ok(CommandOutput {
stdout,
stderr,
exit_code,
success: true,
})
}
Ok((Err(e), _stdout, _stderr)) => Err(Error::Io {
message: "failed to wait for claude process".to_string(),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
}),
Err(_) => {
kill_group_with_grace(&mut group, kill_grace).await;
let _ = child.kill().await;
let drain_budget = Duration::from_millis(200);
let stdout_str = tokio::time::timeout(drain_budget, drain(&mut stdout))
.await
.unwrap_or_default();
let stderr_str = tokio::time::timeout(drain_budget, drain(&mut stderr))
.await
.unwrap_or_default();
if !stdout_str.is_empty() || !stderr_str.is_empty() {
warn!(
stdout = %stdout_str,
stderr = %stderr_str,
"partial output from timed-out process",
);
}
Err(Error::Timeout {
timeout_seconds: timeout.as_secs(),
})
}
}
}
#[cfg(feature = "async")]
async fn drain<R: AsyncReadExt + Unpin>(reader: &mut R) -> String {
let mut buf = Vec::new();
let _ = reader.read_to_end(&mut buf).await;
String::from_utf8_lossy(&buf).into_owned()
}
#[cfg(any(feature = "async", feature = "sync"))]
const TXTBSY_RETRY_BUDGET: Duration = Duration::from_secs(3);
#[cfg(any(feature = "async", feature = "sync"))]
const TXTBSY_MAX_BACKOFF: Duration = Duration::from_millis(25);
#[cfg(feature = "async")]
async fn spawn_retrying_txtbsy(cmd: &mut Command) -> std::io::Result<tokio::process::Child> {
let start = std::time::Instant::now();
let mut backoff = Duration::from_millis(1);
loop {
match cmd.spawn() {
Err(e)
if e.kind() == std::io::ErrorKind::ExecutableFileBusy
&& start.elapsed() < TXTBSY_RETRY_BUDGET =>
{
tokio::time::sleep(backoff).await;
backoff = (backoff * 2).min(TXTBSY_MAX_BACKOFF);
}
other => return other,
}
}
}
#[cfg(feature = "sync")]
pub fn run_claude_sync(claude: &Claude, args: Vec<String>) -> Result<CommandOutput> {
run_claude_with_retry_sync(claude, args, None)
}
#[cfg(feature = "sync")]
pub fn run_claude_with_retry_sync(
claude: &Claude,
args: Vec<String>,
retry_override: Option<&crate::retry::RetryPolicy>,
) -> Result<CommandOutput> {
let policy = retry_override.or(claude.retry_policy.as_ref());
match policy {
Some(policy) => {
crate::retry::with_retry_sync(policy, || run_claude_once_sync(claude, args.clone()))
}
None => run_claude_once_sync(claude, args),
}
}
#[cfg(feature = "sync")]
pub fn run_claude_with_stdin_prompt_sync(
claude: &Claude,
args: Vec<String>,
stdin_content: String,
) -> Result<CommandOutput> {
let command_args = full_command_args(claude, args);
let span = exec_span(claude, &command_args, "stdin-sync");
let _enter = span.enter();
let started = std::time::Instant::now();
debug!(binary = %claude.binary.display(), args = ?command_args, "executing claude command (stdin prompt, sync)");
let result = if let Some(timeout) = claude.timeout {
run_with_timeout_stdin_sync(
&claude.binary,
&command_args,
&claude.env,
claude.clear_env,
claude.working_dir.as_deref(),
timeout,
stdin_content,
SpawnPolicy::of(claude),
)
} else {
run_internal_stdin_sync(
&claude.binary,
&command_args,
&claude.env,
claude.clear_env,
claude.working_dir.as_deref(),
stdin_content,
SpawnPolicy::of(claude),
)
};
if let Ok(output) = &result {
record_exec_outcome(&span, output.exit_code, started);
}
result
}
#[cfg(feature = "sync")]
fn run_internal_stdin_sync(
binary: &std::path::Path,
args: &[String],
env: &std::collections::HashMap<String, String>,
clear_env: bool,
working_dir: Option<&std::path::Path>,
stdin_content: String,
policy: SpawnPolicy<'_>,
) -> Result<CommandOutput> {
let SpawnPolicy {
process_group,
kill_grace: _, die_with_parent,
on_spawn,
} = policy;
use std::io::Write;
use std::process::{Command as StdCommand, Stdio};
let mut cmd = StdCommand::new(binary);
cmd.args(args);
cmd.stdin(Stdio::piped());
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
apply_process_group_sync(&mut cmd, process_group);
apply_die_with_parent_sync(&mut cmd, die_with_parent);
apply_child_environment(&mut cmd, clear_env, env);
if let Some(dir) = working_dir {
cmd.current_dir(dir);
}
let mut child = spawn_retrying_txtbsy_sync(&mut cmd).map_err(|e| Error::Io {
message: format!("failed to spawn claude: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
let mut group = arm_and_notify(process_group, Some(child.id()), on_spawn);
if let Some(mut stdin) = child.stdin.take() {
stdin
.write_all(stdin_content.as_bytes())
.map_err(|e| Error::Io {
message: format!("failed to write to claude stdin: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
stdin.flush().map_err(|e| Error::Io {
message: format!("failed to flush claude stdin: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
}
let output = child.wait_with_output().map_err(|e| Error::Io {
message: "failed to wait for claude process".to_string(),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
group.disarm();
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
let exit_code = output.status.code().unwrap_or(-1);
if !output.status.success() {
return Err(Error::from_command_failure(
format!("{} {}", binary.display(), args.join(" ")),
exit_code,
stdout,
stderr,
working_dir.map(|p| p.to_path_buf()),
));
}
Ok(CommandOutput {
stdout,
stderr,
exit_code,
success: true,
})
}
#[cfg(feature = "sync")]
#[allow(clippy::too_many_arguments)]
fn run_with_timeout_stdin_sync(
binary: &std::path::Path,
args: &[String],
env: &std::collections::HashMap<String, String>,
clear_env: bool,
working_dir: Option<&std::path::Path>,
timeout: Duration,
stdin_content: String,
policy: SpawnPolicy<'_>,
) -> Result<CommandOutput> {
let SpawnPolicy {
process_group,
kill_grace,
die_with_parent,
on_spawn,
} = policy;
use std::io::Write;
use std::process::{Command as StdCommand, Stdio};
use std::thread;
use wait_timeout::ChildExt;
let mut cmd = StdCommand::new(binary);
cmd.args(args);
cmd.stdin(Stdio::piped());
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
apply_process_group_sync(&mut cmd, process_group);
apply_die_with_parent_sync(&mut cmd, die_with_parent);
apply_child_environment(&mut cmd, clear_env, env);
if let Some(dir) = working_dir {
cmd.current_dir(dir);
}
let mut child = spawn_retrying_txtbsy_sync(&mut cmd).map_err(|e| Error::Io {
message: format!("failed to spawn claude: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
let mut group = arm_and_notify(process_group, Some(child.id()), on_spawn);
if let Some(mut stdin) = child.stdin.take() {
stdin
.write_all(stdin_content.as_bytes())
.map_err(|e| Error::Io {
message: format!("failed to write to claude stdin: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
stdin.flush().map_err(|e| Error::Io {
message: format!("failed to flush claude stdin: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
}
let stdout = child.stdout.take().expect("stdout was piped");
let stderr = child.stderr.take().expect("stderr was piped");
let stdout_thread = thread::spawn(move || drain_sync(stdout));
let stderr_thread = thread::spawn(move || drain_sync(stderr));
match child.wait_timeout(timeout).map_err(|e| Error::Io {
message: "failed to wait for claude process".to_string(),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})? {
Some(status) => {
group.disarm();
let stdout = stdout_thread.join().unwrap_or_default();
let stderr = stderr_thread.join().unwrap_or_default();
let exit_code = status.code().unwrap_or(-1);
if !status.success() {
return Err(Error::from_command_failure(
format!("{} {}", binary.display(), args.join(" ")),
exit_code,
stdout,
stderr,
working_dir.map(|p| p.to_path_buf()),
));
}
Ok(CommandOutput {
stdout,
stderr,
exit_code,
success: true,
})
}
None => {
kill_group_with_grace_sync(&mut group, kill_grace);
let _ = child.kill();
let _ = child.wait();
let (stdout_str, stderr_str) =
join_with_deadline(stdout_thread, stderr_thread, Duration::from_millis(200));
if !stdout_str.is_empty() || !stderr_str.is_empty() {
warn!(
stdout = %stdout_str,
stderr = %stderr_str,
"partial output from timed-out process",
);
}
Err(Error::Timeout {
timeout_seconds: timeout.as_secs(),
})
}
}
}
#[cfg(feature = "sync")]
fn run_claude_once_sync(claude: &Claude, args: Vec<String>) -> Result<CommandOutput> {
let command_args = full_command_args(claude, args);
let span = exec_span(claude, &command_args, "oneshot-sync");
let _enter = span.enter();
let started = std::time::Instant::now();
debug!(binary = %claude.binary.display(), args = ?command_args, "executing claude command (sync)");
let result = if let Some(timeout) = claude.timeout {
run_with_timeout_sync(
&claude.binary,
&command_args,
&claude.env,
claude.clear_env,
claude.working_dir.as_deref(),
timeout,
SpawnPolicy::of(claude),
)
} else {
run_internal_sync(
&claude.binary,
&command_args,
&claude.env,
claude.clear_env,
claude.working_dir.as_deref(),
SpawnPolicy::of(claude),
)
};
if let Ok(output) = &result {
record_exec_outcome(&span, output.exit_code, started);
}
result
}
#[cfg(feature = "sync")]
pub fn run_claude_allow_exit_codes_sync(
claude: &Claude,
args: Vec<String>,
allowed_codes: &[i32],
) -> Result<CommandOutput> {
match run_claude_sync(claude, args) {
Err(Error::CommandFailed {
exit_code,
stdout,
stderr,
..
}) if allowed_codes.contains(&exit_code) => Ok(CommandOutput {
stdout,
stderr,
exit_code,
success: false,
}),
other => other,
}
}
#[cfg(feature = "sync")]
fn run_internal_sync(
binary: &std::path::Path,
args: &[String],
env: &std::collections::HashMap<String, String>,
clear_env: bool,
working_dir: Option<&std::path::Path>,
policy: SpawnPolicy<'_>,
) -> Result<CommandOutput> {
let SpawnPolicy {
process_group,
kill_grace: _, die_with_parent,
on_spawn,
} = policy;
use std::process::{Command as StdCommand, Stdio};
let mut cmd = StdCommand::new(binary);
cmd.args(args);
cmd.stdin(Stdio::null());
apply_process_group_sync(&mut cmd, process_group);
apply_die_with_parent_sync(&mut cmd, die_with_parent);
apply_child_environment(&mut cmd, clear_env, env);
if let Some(dir) = working_dir {
cmd.current_dir(dir);
}
let output =
output_retrying_txtbsy_sync_observed(&mut cmd, process_group, on_spawn).map_err(|e| {
Error::Io {
message: format!("failed to spawn claude: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
}
})?;
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
let exit_code = output.status.code().unwrap_or(-1);
if !output.status.success() {
return Err(Error::from_command_failure(
format!("{} {}", binary.display(), args.join(" ")),
exit_code,
stdout,
stderr,
working_dir.map(|p| p.to_path_buf()),
));
}
Ok(CommandOutput {
stdout,
stderr,
exit_code,
success: true,
})
}
#[cfg(feature = "sync")]
fn run_with_timeout_sync(
binary: &std::path::Path,
args: &[String],
env: &std::collections::HashMap<String, String>,
clear_env: bool,
working_dir: Option<&std::path::Path>,
timeout: Duration,
policy: SpawnPolicy<'_>,
) -> Result<CommandOutput> {
let SpawnPolicy {
process_group,
kill_grace,
die_with_parent,
on_spawn,
} = policy;
use std::process::{Command as StdCommand, Stdio};
use std::thread;
use wait_timeout::ChildExt;
let mut cmd = StdCommand::new(binary);
cmd.args(args);
cmd.stdin(Stdio::null());
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
apply_process_group_sync(&mut cmd, process_group);
apply_die_with_parent_sync(&mut cmd, die_with_parent);
apply_child_environment(&mut cmd, clear_env, env);
if let Some(dir) = working_dir {
cmd.current_dir(dir);
}
let mut child = spawn_retrying_txtbsy_sync(&mut cmd).map_err(|e| Error::Io {
message: format!("failed to spawn claude: {e}"),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})?;
let mut group = arm_and_notify(process_group, Some(child.id()), on_spawn);
let stdout = child.stdout.take().expect("stdout was piped");
let stderr = child.stderr.take().expect("stderr was piped");
let stdout_thread = thread::spawn(move || drain_sync(stdout));
let stderr_thread = thread::spawn(move || drain_sync(stderr));
match child.wait_timeout(timeout).map_err(|e| Error::Io {
message: "failed to wait for claude process".to_string(),
source: e,
working_dir: working_dir.map(|p| p.to_path_buf()),
})? {
Some(status) => {
group.disarm();
let stdout = stdout_thread.join().unwrap_or_default();
let stderr = stderr_thread.join().unwrap_or_default();
let exit_code = status.code().unwrap_or(-1);
if !status.success() {
return Err(Error::from_command_failure(
format!("{} {}", binary.display(), args.join(" ")),
exit_code,
stdout,
stderr,
working_dir.map(|p| p.to_path_buf()),
));
}
Ok(CommandOutput {
stdout,
stderr,
exit_code,
success: true,
})
}
None => {
kill_group_with_grace_sync(&mut group, kill_grace);
let _ = child.kill();
let _ = child.wait();
let (stdout_str, stderr_str) =
join_with_deadline(stdout_thread, stderr_thread, Duration::from_millis(200));
if !stdout_str.is_empty() || !stderr_str.is_empty() {
warn!(
stdout = %stdout_str,
stderr = %stderr_str,
"partial output from timed-out process",
);
}
Err(Error::Timeout {
timeout_seconds: timeout.as_secs(),
})
}
}
}
#[cfg(feature = "sync")]
fn drain_sync<R: std::io::Read>(mut reader: R) -> String {
let mut buf = Vec::new();
let _ = reader.read_to_end(&mut buf);
String::from_utf8_lossy(&buf).into_owned()
}
#[cfg(feature = "sync")]
fn spawn_retrying_txtbsy_sync(
cmd: &mut std::process::Command,
) -> std::io::Result<std::process::Child> {
let start = std::time::Instant::now();
let mut backoff = Duration::from_millis(1);
loop {
match cmd.spawn() {
Err(e)
if e.kind() == std::io::ErrorKind::ExecutableFileBusy
&& start.elapsed() < TXTBSY_RETRY_BUDGET =>
{
std::thread::sleep(backoff);
backoff = (backoff * 2).min(TXTBSY_MAX_BACKOFF);
}
other => return other,
}
}
}
#[cfg(feature = "sync")]
#[cfg(feature = "sync")]
fn output_retrying_txtbsy_sync_observed(
cmd: &mut std::process::Command,
process_group: bool,
on_spawn: Option<&crate::SpawnObserver>,
) -> std::io::Result<std::process::Output> {
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
let start = std::time::Instant::now();
let mut backoff = Duration::from_millis(1);
loop {
let spawned = cmd.spawn().inspect(|child| {
if let Some(observer) = on_spawn {
let pid = child.id();
observer(crate::SpawnInfo {
pid,
pgid: process_group.then_some(pid),
});
}
});
match spawned.and_then(std::process::Child::wait_with_output) {
Err(e)
if e.kind() == std::io::ErrorKind::ExecutableFileBusy
&& start.elapsed() < TXTBSY_RETRY_BUDGET =>
{
std::thread::sleep(backoff);
backoff = (backoff * 2).min(TXTBSY_MAX_BACKOFF);
}
other => return other,
}
}
}
#[cfg(feature = "sync")]
fn join_with_deadline(
stdout_thread: std::thread::JoinHandle<String>,
stderr_thread: std::thread::JoinHandle<String>,
budget: Duration,
) -> (String, String) {
use std::sync::mpsc;
use std::thread;
let (tx, rx) = mpsc::channel::<(&'static str, String)>();
let tx_out = tx.clone();
let tx_err = tx;
thread::spawn(move || {
let s = stdout_thread.join().unwrap_or_default();
let _ = tx_out.send(("stdout", s));
});
thread::spawn(move || {
let s = stderr_thread.join().unwrap_or_default();
let _ = tx_err.send(("stderr", s));
});
let mut stdout = String::new();
let mut stderr = String::new();
let deadline = std::time::Instant::now() + budget;
for _ in 0..2 {
let now = std::time::Instant::now();
if now >= deadline {
break;
}
match rx.recv_timeout(deadline - now) {
Ok(("stdout", s)) => stdout = s,
Ok(("stderr", s)) => stderr = s,
Ok(_) => unreachable!(),
Err(_) => break,
}
}
(stdout, stderr)
}
#[cfg(all(test, unix, any(feature = "async", feature = "sync")))]
mod tests {
use super::*;
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
use crate::Claude;
fn fake_script(body: &str) -> (tempfile::TempDir, std::path::PathBuf) {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("fake-claude.sh");
{
let mut f = std::fs::File::create(&path).expect("create script");
write!(f, "#!/usr/bin/env bash\n{body}\n").expect("write script");
f.sync_all().expect("sync script");
}
let perms = std::fs::Permissions::from_mode(0o755);
std::fs::set_permissions(&path, perms).expect("chmod");
(dir, path)
}
fn client(path: &std::path::Path) -> Claude {
Claude::builder()
.binary(path)
.build()
.expect("build client")
}
#[test]
fn full_command_args_puts_global_args_first() {
let claude = Claude::builder()
.binary("/usr/local/bin/claude")
.arg("--debug")
.arg("--verbose")
.build()
.expect("build client");
let args = full_command_args(&claude, vec!["--print".to_string(), "hi".to_string()]);
assert_eq!(args, ["--debug", "--verbose", "--print", "hi"]);
}
#[test]
fn full_command_args_without_global_args_is_passthrough() {
let claude = Claude::builder()
.binary("/usr/local/bin/claude")
.build()
.expect("build client");
let args = full_command_args(&claude, vec!["--print".to_string()]);
assert_eq!(args, ["--print"]);
}
static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn set_scrub_vars() {
let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
unsafe {
std::env::set_var("CLAUDECODE", "1");
std::env::set_var("CLAUDE_CODE_ENTRYPOINT", "cli");
}
}
fn clear_scrub_vars() {
let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
unsafe {
std::env::remove_var("CLAUDECODE");
std::env::remove_var("CLAUDE_CODE_ENTRYPOINT");
}
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_success_maps_output() {
let (_dir, path) = fake_script(r#"echo "hi there"; exit 0"#);
let out = run_claude(&client(&path), vec!["--version".into()])
.await
.expect("success");
assert!(out.success);
assert_eq!(out.exit_code, 0);
assert!(out.stdout.contains("hi there"));
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_nonzero_exit_maps_command_failed() {
let (_dir, path) = fake_script(r#"echo "boom" >&2; exit 3"#);
let err = run_claude(&client(&path), vec![]).await.unwrap_err();
match err {
Error::CommandFailed {
exit_code, stderr, ..
} => {
assert_eq!(exit_code, 3);
assert!(stderr.contains("boom"));
}
other => panic!("expected CommandFailed, got {other:?}"),
}
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_rail_stop_maps_max_turns() {
let (_dir, path) = fake_script(
r#"echo '{"type":"result","subtype":"error_max_turns","is_error":true,"errors":["Reached maximum number of turns (2)"]}'; exit 1"#,
);
let err = run_claude(&client(&path), vec![]).await.unwrap_err();
assert!(
matches!(
err,
Error::MaxTurnsExceeded {
max_turns: Some(2),
..
}
),
"got: {err:?}"
);
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_auth_shaped_stderr_maps_auth() {
let (_dir, path) =
fake_script(r#"echo "Not authenticated. Run `claude login`." >&2; exit 1"#);
let err = run_claude(&client(&path), vec![]).await.unwrap_err();
assert!(matches!(err, Error::Auth { .. }), "got: {err:?}");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_scrubs_claude_env_vars() {
let (_dir, path) =
fake_script(r#"echo "CC=[${CLAUDECODE:-}] EP=[${CLAUDE_CODE_ENTRYPOINT:-}]""#);
set_scrub_vars();
let out = run_claude(&client(&path), vec![]).await.expect("success");
clear_scrub_vars();
assert!(out.stdout.contains("CC=[]"), "got: {}", out.stdout);
assert!(out.stdout.contains("EP=[]"), "got: {}", out.stdout);
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_applies_working_dir() {
let (_dir, path) = fake_script(r#"pwd"#);
let workdir = tempfile::tempdir().expect("workdir");
let claude = Claude::builder()
.binary(&path)
.working_dir(workdir.path())
.build()
.expect("build");
let out = run_claude(&claude, vec![]).await.expect("success");
let got = std::fs::canonicalize(out.stdout.trim()).expect("canonicalize pwd");
let want = std::fs::canonicalize(workdir.path()).expect("canonicalize workdir");
assert_eq!(got, want);
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_stdin_prompt_round_trips() {
let (_dir, path) = fake_script(r#"cat"#);
let out = run_claude_with_stdin_prompt(&client(&path), vec![], "hello via stdin".into())
.await
.expect("success");
assert!(out.stdout.contains("hello via stdin"));
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_spawn_retry_passes_through_non_txtbsy_error() {
let mut cmd = Command::new("/nonexistent/definitely-not-a-real-binary");
let err = spawn_retrying_txtbsy(&mut cmd)
.await
.expect_err("spawn of missing binary should fail");
assert_eq!(err.kind(), std::io::ErrorKind::NotFound, "got: {err:?}");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_allow_exit_codes_permits_listed_code() {
let (_dir, path) = fake_script(r#"echo out; exit 2"#);
let out = run_claude_allow_exit_codes(&client(&path), vec![], &[2])
.await
.expect("allowed code is Ok");
assert!(!out.success);
assert_eq!(out.exit_code, 2);
assert!(out.stdout.contains("out"));
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_allow_exit_codes_still_errors_on_unlisted_code() {
let (_dir, path) = fake_script(r#"exit 2"#);
let err = run_claude_allow_exit_codes(&client(&path), vec![], &[5])
.await
.unwrap_err();
assert!(
matches!(err, Error::CommandFailed { exit_code: 2, .. }),
"got: {err:?}"
);
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_timeout_fires_on_slow_child() {
let (_dir, path) = fake_script(r#"sleep 3; echo done"#);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_millis(300))
.build()
.expect("build");
let err = run_claude(&claude, vec![]).await.unwrap_err();
assert!(matches!(err, Error::Timeout { .. }), "got: {err:?}");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_timeout_path_returns_output_when_fast() {
let (_dir, path) = fake_script(r#"echo quick"#);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_secs(30))
.build()
.expect("build");
let out = run_claude(&claude, vec![]).await.expect("success");
assert!(out.stdout.contains("quick"));
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_timeout_path_maps_command_failed() {
let (_dir, path) = fake_script(r#"echo e >&2; exit 4"#);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_secs(30))
.build()
.expect("build");
let err = run_claude(&claude, vec![]).await.unwrap_err();
assert!(
matches!(err, Error::CommandFailed { exit_code: 4, .. }),
"got: {err:?}"
);
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_stdin_with_timeout_round_trips() {
let (_dir, path) = fake_script(r#"cat"#);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_secs(30))
.build()
.expect("build");
let out = run_claude_with_stdin_prompt(&claude, vec![], "piped under timeout".into())
.await
.expect("success");
assert!(out.stdout.contains("piped under timeout"));
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_stdin_timeout_fires_on_slow_child() {
let (_dir, path) = fake_script(r#"sleep 3"#);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_millis(300))
.build()
.expect("build");
let err = run_claude_with_stdin_prompt(&claude, vec![], "x".into())
.await
.unwrap_err();
assert!(matches!(err, Error::Timeout { .. }), "got: {err:?}");
}
#[cfg(feature = "async")]
async fn drop_in_flight_and_capture_pid<F>(fut: F, pid_path: &std::path::Path) -> u32
where
F: std::future::Future,
F::Output: std::fmt::Debug,
{
tokio::pin!(fut);
let deadline = std::time::Instant::now() + Duration::from_secs(10);
loop {
if let Some(pid) = std::fs::read_to_string(pid_path)
.ok()
.and_then(|s| s.trim().parse().ok())
{
return pid;
}
assert!(
std::time::Instant::now() < deadline,
"child never wrote its pid file"
);
tokio::select! {
out = &mut fut => panic!("future completed before drop: {out:?}"),
_ = tokio::time::sleep(Duration::from_millis(10)) => {}
}
}
}
fn assert_pid_killed(pid: u32) {
let deadline = std::time::Instant::now() + Duration::from_secs(10);
loop {
let out = std::process::Command::new("ps")
.args(["-o", "stat=", "-p", &pid.to_string()])
.output()
.expect("run ps");
let stat = String::from_utf8_lossy(&out.stdout).trim().to_string();
if !out.status.success() || stat.is_empty() || stat.starts_with('Z') {
return;
}
assert!(
std::time::Instant::now() < deadline,
"process {pid} still alive (stat {stat}) after kill"
);
std::thread::sleep(Duration::from_millis(25));
}
}
fn group_script(
pid_path: &std::path::Path,
gpid_path: &std::path::Path,
) -> (tempfile::TempDir, std::path::PathBuf) {
fake_script(&format!(
concat!(
"bash -c 'echo $$ > \"$0\"; exec sleep 300' \"{g}\" &\n",
"until [[ -s \"{g}\" ]]; do sleep 0.01; done\n",
"echo $$ > \"{p}\"\n",
"exec sleep 300",
),
g = gpid_path.display(),
p = pid_path.display(),
))
}
fn try_read_pid(path: &std::path::Path) -> Option<u32> {
std::fs::read_to_string(path).ok()?.trim().parse().ok()
}
#[cfg(feature = "async")]
fn read_pid(path: &std::path::Path) -> u32 {
try_read_pid(path).expect("pid file readable")
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_dropping_in_flight_future_kills_child() {
let workdir = tempfile::tempdir().expect("workdir");
let pid_path = workdir.path().join("pid");
let (_dir, path) = fake_script(&format!(
r#"echo $$ > "{}"; exec sleep 30"#,
pid_path.display()
));
let claude = client(&path);
let pid = drop_in_flight_and_capture_pid(run_claude(&claude, vec![]), &pid_path).await;
assert_pid_killed(pid);
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_dropping_in_flight_future_kills_child_with_timeout() {
let workdir = tempfile::tempdir().expect("workdir");
let pid_path = workdir.path().join("pid");
let (_dir, path) = fake_script(&format!(
r#"echo $$ > "{}"; exec sleep 30"#,
pid_path.display()
));
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_secs(120))
.build()
.expect("build");
let pid = drop_in_flight_and_capture_pid(run_claude(&claude, vec![]), &pid_path).await;
assert_pid_killed(pid);
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_dropping_in_flight_future_kills_process_group() {
let workdir = tempfile::tempdir().expect("workdir");
let pid_path = workdir.path().join("pid");
let gpid_path = workdir.path().join("gpid");
let (_dir, path) = group_script(&pid_path, &gpid_path);
let claude = client(&path);
let pid = drop_in_flight_and_capture_pid(run_claude(&claude, vec![]), &pid_path).await;
assert_pid_killed(pid);
assert_pid_killed(read_pid(&gpid_path));
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_timeout_kills_process_group() {
let mut observed = false;
for _ in 0..5 {
let workdir = tempfile::tempdir().expect("workdir");
let pid_path = workdir.path().join("pid");
let gpid_path = workdir.path().join("gpid");
let (_dir, path) = group_script(&pid_path, &gpid_path);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_millis(1000))
.build()
.expect("build");
let err = run_claude(&claude, vec![]).await.unwrap_err();
assert!(matches!(err, Error::Timeout { .. }), "got: {err:?}");
if let (Some(pid), Some(gpid)) = (try_read_pid(&pid_path), try_read_pid(&gpid_path)) {
assert_pid_killed(pid);
assert_pid_killed(gpid);
observed = true;
break;
}
}
assert!(observed, "child never recorded pids within 5 timeout runs");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_process_group_opt_out_kills_only_direct_child() {
let workdir = tempfile::tempdir().expect("workdir");
let pid_path = workdir.path().join("pid");
let gpid_path = workdir.path().join("gpid");
let (_dir, path) = group_script(&pid_path, &gpid_path);
let claude = Claude::builder()
.binary(&path)
.process_group(false)
.build()
.expect("build");
let pid = drop_in_flight_and_capture_pid(run_claude(&claude, vec![]), &pid_path).await;
assert_pid_killed(pid);
let gpid = read_pid(&gpid_path);
let out = std::process::Command::new("ps")
.args(["-o", "stat=", "-p", &gpid.to_string()])
.output()
.expect("run ps");
let stat = String::from_utf8_lossy(&out.stdout).trim().to_string();
assert!(
out.status.success() && !stat.is_empty() && !stat.starts_with('Z'),
"grandchild {gpid} should have survived the opt-out drop (stat {stat:?})"
);
let _ = std::process::Command::new("kill")
.args(["-9", &gpid.to_string()])
.status();
}
fn term_trap_script(marker: &std::path::Path) -> (tempfile::TempDir, std::path::PathBuf) {
fake_script(&format!(
concat!(
"trap 'echo term > \"{m}\"; exit 0' TERM\n",
"sleep 300 &\n",
"wait $!",
),
m = marker.display(),
))
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_timeout_with_grace_delivers_sigterm_first() {
let mut observed = false;
for _ in 0..5 {
let workdir = tempfile::tempdir().expect("workdir");
let marker = workdir.path().join("term-marker");
let (_dir, path) = term_trap_script(&marker);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_millis(500))
.kill_grace(Duration::from_secs(1))
.build()
.expect("build");
let err = run_claude(&claude, vec![]).await.unwrap_err();
assert!(matches!(err, Error::Timeout { .. }), "got: {err:?}");
if marker.exists() {
observed = true;
break;
}
}
assert!(observed, "TERM marker never appeared within 5 timeout runs");
}
#[cfg(feature = "sync")]
#[test]
fn sync_timeout_with_grace_delivers_sigterm_first() {
let mut observed = false;
for _ in 0..5 {
let workdir = tempfile::tempdir().expect("workdir");
let marker = workdir.path().join("term-marker");
let (_dir, path) = term_trap_script(&marker);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_millis(500))
.kill_grace(Duration::from_secs(1))
.build()
.expect("build");
let err = run_claude_sync(&claude, vec![]).unwrap_err();
assert!(matches!(err, Error::Timeout { .. }), "got: {err:?}");
if marker.exists() {
observed = true;
break;
}
}
assert!(observed, "TERM marker never appeared within 5 timeout runs");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_dropping_in_flight_stdin_future_kills_child() {
let workdir = tempfile::tempdir().expect("workdir");
let pid_path = workdir.path().join("pid");
let (_dir, path) = fake_script(&format!(
r#"echo $$ > "{}"; exec sleep 30"#,
pid_path.display()
));
let claude = client(&path);
let pid = drop_in_flight_and_capture_pid(
run_claude_with_stdin_prompt(&claude, vec![], "x".into()),
&pid_path,
)
.await;
assert_pid_killed(pid);
}
#[cfg(feature = "async")]
#[tokio::test]
async fn async_spawn_failure_maps_io() {
let claude = Claude::builder()
.binary("/nonexistent/definitely/not/here")
.build()
.expect("build");
let err = run_claude(&claude, vec![]).await.unwrap_err();
assert!(matches!(err, Error::Io { .. }), "got: {err:?}");
}
#[cfg(feature = "sync")]
#[test]
fn sync_success_maps_output() {
let (_dir, path) = fake_script(r#"echo "hi sync"; exit 0"#);
let out = run_claude_sync(&client(&path), vec![]).expect("success");
assert!(out.success);
assert!(out.stdout.contains("hi sync"));
}
#[cfg(feature = "sync")]
#[test]
fn sync_nonzero_exit_maps_command_failed() {
let (_dir, path) = fake_script(r#"echo "boom" >&2; exit 3"#);
let err = run_claude_sync(&client(&path), vec![]).unwrap_err();
match err {
Error::CommandFailed {
exit_code, stderr, ..
} => {
assert_eq!(exit_code, 3);
assert!(stderr.contains("boom"));
}
other => panic!("expected CommandFailed, got {other:?}"),
}
}
#[cfg(feature = "sync")]
#[test]
fn sync_scrubs_claude_env_vars() {
let (_dir, path) =
fake_script(r#"echo "CC=[${CLAUDECODE:-}] EP=[${CLAUDE_CODE_ENTRYPOINT:-}]""#);
set_scrub_vars();
let out = run_claude_sync(&client(&path), vec![]).expect("success");
clear_scrub_vars();
assert!(out.stdout.contains("CC=[]"), "got: {}", out.stdout);
assert!(out.stdout.contains("EP=[]"), "got: {}", out.stdout);
}
#[cfg(feature = "sync")]
#[test]
fn sync_stdin_prompt_round_trips() {
let (_dir, path) = fake_script(r#"cat"#);
let out = run_claude_with_stdin_prompt_sync(&client(&path), vec![], "sync stdin".into())
.expect("success");
assert!(out.stdout.contains("sync stdin"));
}
#[cfg(feature = "sync")]
#[test]
fn sync_spawn_retry_passes_through_non_txtbsy_error() {
let mut cmd = std::process::Command::new("/nonexistent/definitely-not-a-real-binary");
let err =
spawn_retrying_txtbsy_sync(&mut cmd).expect_err("spawn of missing binary should fail");
assert_eq!(err.kind(), std::io::ErrorKind::NotFound, "got: {err:?}");
}
#[cfg(feature = "sync")]
#[test]
fn sync_output_retry_passes_through_non_txtbsy_error() {
let mut cmd = std::process::Command::new("/nonexistent/definitely-not-a-real-binary");
let err = output_retrying_txtbsy_sync_observed(&mut cmd, false, None)
.expect_err("output of missing binary should fail");
assert_eq!(err.kind(), std::io::ErrorKind::NotFound, "got: {err:?}");
}
#[cfg(feature = "sync")]
#[test]
fn sync_allow_exit_codes_permits_listed_code() {
let (_dir, path) = fake_script(r#"echo out; exit 2"#);
let out = run_claude_allow_exit_codes_sync(&client(&path), vec![], &[2])
.expect("allowed code is Ok");
assert!(!out.success);
assert_eq!(out.exit_code, 2);
}
#[cfg(feature = "sync")]
#[test]
fn sync_timeout_fires_on_slow_child() {
let (_dir, path) = fake_script(r#"sleep 3; echo done"#);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_millis(300))
.build()
.expect("build");
let err = run_claude_sync(&claude, vec![]).unwrap_err();
assert!(matches!(err, Error::Timeout { .. }), "got: {err:?}");
}
#[cfg(feature = "sync")]
#[test]
fn sync_timeout_kills_process_group() {
let mut observed = false;
for _ in 0..5 {
let workdir = tempfile::tempdir().expect("workdir");
let pid_path = workdir.path().join("pid");
let gpid_path = workdir.path().join("gpid");
let (_dir, path) = group_script(&pid_path, &gpid_path);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_millis(1000))
.build()
.expect("build");
let err = run_claude_sync(&claude, vec![]).unwrap_err();
assert!(matches!(err, Error::Timeout { .. }), "got: {err:?}");
if let (Some(pid), Some(gpid)) = (try_read_pid(&pid_path), try_read_pid(&gpid_path)) {
assert_pid_killed(pid);
assert_pid_killed(gpid);
observed = true;
break;
}
}
assert!(observed, "child never recorded pids within 5 timeout runs");
}
#[cfg(feature = "sync")]
#[test]
fn sync_timeout_path_returns_output_when_fast() {
let (_dir, path) = fake_script(r#"echo quick"#);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_secs(30))
.build()
.expect("build");
let out = run_claude_sync(&claude, vec![]).expect("success");
assert!(out.stdout.contains("quick"));
}
#[cfg(feature = "sync")]
#[test]
fn sync_stdin_with_timeout_round_trips() {
let (_dir, path) = fake_script(r#"cat"#);
let claude = Claude::builder()
.binary(&path)
.timeout(Duration::from_secs(30))
.build()
.expect("build");
let out = run_claude_with_stdin_prompt_sync(&claude, vec![], "sync piped".into())
.expect("success");
assert!(out.stdout.contains("sync piped"));
}
}