From 9a74539e45664f21ceb502992f1fdc0ce32195b2 Mon Sep 17 00:00:00 2001 From: Gautam Sirdeshmukh <54588697+gautamsirdeshmukh@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:40:32 -0500 Subject: [PATCH 1/4] fix: preserve stack space during recursive shell execution --- Cargo.lock | 42 ++++++++++++++++++++ Cargo.toml | 1 + src/exec.rs | 22 ++++++++++- src/exec/stack.rs | 39 ++++++++++++++++++ src/lib.rs | 4 ++ tests/command_check.rs | 52 ++++++++++++++++++++++++ tests/shell_integration.rs | 81 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 src/exec/stack.rs diff --git a/Cargo.lock b/Cargo.lock index 19e728d..d50f827 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,6 +70,15 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "ar_archive_writer" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523e5e187fff36fcb8d1d19ccf0ca4ca43ec3d158316057f66dc6f72716bc5ec" +dependencies = [ + "object", +] + [[package]] name = "async-trait" version = "0.1.92" @@ -1051,6 +1060,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "object" +version = "0.39.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e5a6c098c7a3b6547378093f5cc30bc54fd361ce711e05293a5cc589562739b" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -1137,6 +1155,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "psm" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dcd034599e63b970727f70d79e02d62390a4a84f7c6b827c27c46d5ac3fa622" +dependencies = [ + "ar_archive_writer", + "cc", +] + [[package]] name = "pyo3" version = "0.30.1" @@ -1603,6 +1631,19 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "stacker" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "707f49d46706bacf8a2b00d51dace3f9de527c13eec3778f570c411f89e69967" +dependencies = [ + "cc", + "cfg-if", + "libc", + "psm", + "windows-sys 0.61.2", +] + [[package]] name = "strands-shell" version = "0.0.0" @@ -1627,6 +1668,7 @@ dependencies = [ "rustyline", "serde", "serde_json", + "stacker", "strands-shell-macros", "tokio", "toml", diff --git a/Cargo.toml b/Cargo.toml index fc02367..c4be98c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,7 @@ napi-derive = { version = "3", optional = true } pyo3 = { version = "0.30", features = ["extension-module"], optional = true } reqwest = { version = "0.12", features = ["rustls-tls"], default-features = false } rustyline = { version = "19", optional = true } +stacker = "0.1" tokio = { version = "1", features = ["rt", "macros", "sync", "io-util", "io-std", "fs", "time", "process"] } # ----- Unix-only dependencies ----- diff --git a/src/exec.rs b/src/exec.rs index f7c8850..cd3e0ca 100644 --- a/src/exec.rs +++ b/src/exec.rs @@ -14,6 +14,8 @@ use crate::os::{ }; use crate::parser::{self, Connector, Item, Redirect, Word, WordPart}; +mod stack; + /// Foreground tasks that abort on drop when the kernel has a command fallback or the /// process is a background job. struct ForegroundTasks { @@ -1918,7 +1920,15 @@ fn captured_status(result: Result, last_exit: i32) -> i32 { /// Execute a command line, capturing stdout along with the exit status of the last /// item it ran or the control flow that stopped it. -async fn run_capturing_status( +fn run_capturing_status<'a>( + os: Arc, + proc: &'a mut Process, + command_line: &'a parser::CommandLine, +) -> impl std::future::Future)> + 'a { + stack::run(run_capturing_status_inner(os, proc, command_line)) +} + +async fn run_capturing_status_inner( os: Arc, proc: &mut Process, command_line: &parser::CommandLine, @@ -2354,7 +2364,15 @@ async fn execute_command_line_inner( Ok(last_exit) } -async fn execute_item( +fn execute_item<'a>( + os: Arc, + proc: &'a mut Process, + item: &'a parser::Item, +) -> impl std::future::Future> + 'a { + stack::run(execute_item_inner(os, proc, item)) +} + +async fn execute_item_inner( os: Arc, proc: &mut Process, item: &parser::Item, diff --git a/src/exec/stack.rs b/src/exec/stack.rs new file mode 100644 index 0000000..1a21fbd --- /dev/null +++ b/src/exec/stack.rs @@ -0,0 +1,39 @@ +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; + +/// A recursive execution future with stack space for polling and cancellation. +struct Execution { + future: Option>>, +} + +pub(super) fn run(future: F) -> impl Future { + Execution { + future: Some(Box::pin(future)), + } +} + +impl Future for Execution { + type Output = F::Output; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + with_stack(|| self.future.as_mut().unwrap().as_mut().poll(cx)) + } +} + +impl Drop for Execution { + fn drop(&mut self) { + with_stack(|| drop(self.future.take())); + } +} + +fn with_stack(f: impl FnOnce() -> R) -> R { + #[cfg(not(target_arch = "wasm32"))] + { + stacker::maybe_grow(1024 * 1024, 4 * 1024 * 1024, f) + } + #[cfg(target_arch = "wasm32")] + { + f() + } +} diff --git a/src/lib.rs b/src/lib.rs index f56789c..ec2bf31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,6 +48,10 @@ //! } //! ``` //! +//! On native targets, recursive shell execution grows the stack as needed when +//! polling or cancelling a future. It stays on the caller's thread and retains +//! the configured recursion limit. WASM uses the stack supplied by its host. +//! //! ## Sandboxing with Bind Mounts //! //! The shell starts with an empty virtual filesystem. Use bind mounts to diff --git a/tests/command_check.rs b/tests/command_check.rs index 3c4099d..2d73566 100644 --- a/tests/command_check.rs +++ b/tests/command_check.rs @@ -142,6 +142,58 @@ struct CheckingKernel { fail_record: bool, } +#[cfg(not(target_arch = "wasm32"))] +struct StackCheckingKernel { + inner: Arc, +} + +#[cfg(not(target_arch = "wasm32"))] +forwarding_kernel!(StackCheckingKernel { + async fn check_command(&self, request: &CommandRequest) -> io::Result<()> { + if stacker::remaining_stack().is_some_and(|remaining| remaining < 512 * 1024) { + return Err(io::Error::other("not enough stack for the command policy")); + } + self.inner.check_command(request).await + } +}); + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn recursive_functions_leave_stack_for_command_checks() { + std::thread::Builder::new() + .stack_size(2 * 1024 * 1024) + .spawn(|| { + block_on(async { + let (checked, _) = checking_shell(); + let inner = checked.kernel().clone(); + let mut shell = Shell::with_kernel(Arc::new(StackCheckingKernel { inner })); + let out = shell + .run( + "f() { if [ \"$1\" -gt 0 ]; then f \"$(($1 - 1))\"; \ + else echo bottom; fi; }; f 60", + ) + .await; + assert_eq!(out.status, 0, "{}", out.stderr); + assert_eq!(out.stdout, "bottom\n"); + assert!(out.stderr.is_empty(), "{}", out.stderr); + + assert_eq!(shell.run("echo keep > /tmp/keep").await.status, 0); + let out = shell + .run( + "f() { if [ \"$1\" -gt 0 ]; then f \"$(($1 - 1))\"; \ + else rm /tmp/keep; fi; }; f 60", + ) + .await; + assert_eq!(out.status, 126); + assert_eq!(out.stderr, "strands-shell: rm: denied by policy\n"); + assert_eq!(shell.run("cat /tmp/keep").await.stdout, "keep\n"); + }); + }) + .unwrap() + .join() + .unwrap(); +} + forwarding_kernel!(CheckingKernel { async fn check_command(&self, request: &CommandRequest) -> io::Result<()> { self.events.lock().unwrap().push(Event::Check( diff --git a/tests/shell_integration.rs b/tests/shell_integration.rs index b4ef615..8b3bc87 100644 --- a/tests/shell_integration.rs +++ b/tests/shell_integration.rs @@ -5969,6 +5969,87 @@ fn default_max_depth_is_nonzero() { })); } +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn recursive_execution_on_worker_stack() { + std::thread::Builder::new() + .stack_size(2 * 1024 * 1024) + .spawn(|| { + let (rt, local) = rt(); + rt.block_on(local.run_until(async { + let mut shell = Shell::builder().build().unwrap(); + let out = shell + .run( + "f() { local n=$1; if [ \"$n\" -gt 0 ]; then f \"$((n - 1))\"; \ + else answer=bottom; fi; }; f 60; echo \"$answer\"", + ) + .await; + assert_eq!(out.status, 0, "{}", out.stderr); + assert_eq!(out.stdout, "bottom\n"); + assert!(out.stderr.is_empty(), "{}", out.stderr); + + let captured = format!( + "x=$({}echo captured; {}); echo \"$x\"", + "{ ".repeat(60), + "}; ".repeat(60), + ); + let out = shell.run(&captured).await; + assert_eq!(out.status, 0, "{}", out.stderr); + assert_eq!(out.stdout, "captured\n"); + + let out = shell.run("f 64").await; + assert_ne!(out.status, 0); + assert!( + out.stderr.contains("maximum recursion depth exceeded"), + "{}", + out.stderr + ); + })); + }) + .unwrap() + .join() + .unwrap(); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn recursive_execution_can_be_cancelled_on_worker_stack() { + std::thread::Builder::new() + .stack_size(2 * 1024 * 1024) + .spawn(|| { + let (rt, local) = rt(); + rt.block_on(local.run_until(async { + let mut shell = Shell::builder().max_depth(0).build().unwrap(); + let kernel = shell.kernel().clone(); + let proc = kernel.new_process(); + let mut run = Box::pin(shell.run( + "f() { if [ \"$1\" -gt 0 ]; then f \"$(($1 - 1))\"; \ + else touch /tmp/ready; sleep 60; echo leaked > /tmp/leaked; fi; }; f 500", + )); + let ready = async { + while !kernel.stat(&proc, "/tmp/ready").await.exists { + tokio::task::yield_now().await; + } + }; + tokio::select! { + out = run.as_mut() => panic!( + "run ended before cancellation: status={}, stderr={}", + out.status, out.stderr + ), + result = tokio::time::timeout(std::time::Duration::from_secs(10), ready) => { + result.expect("recursive call did not reach the bottom"); + } + } + stacker::grow(64 * 1024, || drop(run)); + assert_eq!(shell.run("test ! -e /tmp/leaked").await.status, 0); + assert_eq!(shell.run("echo alive").await.stdout, "alive\n"); + })); + }) + .unwrap() + .join() + .unwrap(); +} + #[test] fn max_depth_blocks_deep_eval() { let (rt, local) = rt(); From a1233bbbaa95a87bcdb5807fa4dbeac81c2a3111 Mon Sep 17 00:00:00 2001 From: Gautam Sirdeshmukh <54588697+gautamsirdeshmukh@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:16:43 -0500 Subject: [PATCH 2/4] fix: reduce recursive executor stack usage --- Cargo.toml | 4 +- src/exec.rs | 1647 +++++++++++++++++++----------------- src/exec/stack.rs | 39 - src/lib.rs | 4 - src/os.rs | 3 +- tests/command_check.rs | 12 +- tests/shell_integration.rs | 6 +- 7 files changed, 876 insertions(+), 839 deletions(-) delete mode 100644 src/exec/stack.rs diff --git a/Cargo.toml b/Cargo.toml index c4be98c..50e4431 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,7 +54,6 @@ napi-derive = { version = "3", optional = true } pyo3 = { version = "0.30", features = ["extension-module"], optional = true } reqwest = { version = "0.12", features = ["rustls-tls"], default-features = false } rustyline = { version = "19", optional = true } -stacker = "0.1" tokio = { version = "1", features = ["rt", "macros", "sync", "io-util", "io-std", "fs", "time", "process"] } # ----- Unix-only dependencies ----- @@ -77,6 +76,9 @@ members = [".", "strands-shell-macros", "xtask"] [dev-dependencies] axum = "0.8" +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +stacker = "0.1" + [[test]] name = "mcp_integration" required-features = ["cli"] diff --git a/src/exec.rs b/src/exec.rs index cd3e0ca..7580d7e 100644 --- a/src/exec.rs +++ b/src/exec.rs @@ -14,7 +14,13 @@ use crate::os::{ }; use crate::parser::{self, Connector, Item, Redirect, Word, WordPart}; -mod stack; +type Execution<'a, T> = std::pin::Pin + 'a>>; + +// Construct large futures outside recursive poll frames. +#[inline(never)] +fn box_future(make: impl FnOnce() -> F) -> std::pin::Pin> { + Box::pin(make()) +} /// Foreground tasks that abort on drop when the kernel has a command fallback or the /// process is a background job. @@ -1847,7 +1853,7 @@ async fn execute_captured_control_flow( pipeline: &[parser::Command], ) -> Result { // Expand the command arguments before the redirection targets, as run_pipeline does. - let result = Box::pin(execute_pipeline_checked(os.clone(), proc, pipeline)).await; + let result = execute_pipeline_checked(os.clone(), proc, pipeline).await; let mut redirects = Vec::new(); for redir in &pipeline[0].redirects { let target = match redir { @@ -1902,7 +1908,7 @@ async fn capture_pipeline_checked( let saved_output = std::mem::take(&mut proc.captured_output); let mut saved_stdout = Process::empty(); proc.transfer_fd(STDOUT, &mut saved_stdout); - let result = Box::pin(execute_pipeline_checked(os, proc, pipeline)).await; + let result = execute_pipeline_checked(os, proc, pipeline).await; proc.capture = saved_capture; proc.close(STDOUT); saved_stdout.transfer_fd(STDOUT, proc); @@ -1920,12 +1926,13 @@ fn captured_status(result: Result, last_exit: i32) -> i32 { /// Execute a command line, capturing stdout along with the exit status of the last /// item it ran or the control flow that stopped it. +#[inline(never)] fn run_capturing_status<'a>( os: Arc, proc: &'a mut Process, command_line: &'a parser::CommandLine, -) -> impl std::future::Future)> + 'a { - stack::run(run_capturing_status_inner(os, proc, command_line)) +) -> Execution<'a, (String, Result)> { + Box::pin(run_capturing_status_inner(os, proc, command_line)) } async fn run_capturing_status_inner( @@ -1985,7 +1992,7 @@ async fn run_capturing_status_inner( } } Item::Group(cl) => { - let (s, result) = Box::pin(run_capturing_status(os.clone(), proc, cl)).await; + let (s, result) = run_capturing_status(os.clone(), proc, cl).await; match result { Ok(exit) => (exit, s), Err(control) => { @@ -2002,8 +2009,7 @@ async fn run_capturing_status_inner( (1, String::new()) } else { proc.transfer_fd(STDIN, &mut sub); - let (s, result) = - Box::pin(run_capturing_status(os.clone(), &mut sub, cl)).await; + let (s, result) = run_capturing_status(os.clone(), &mut sub, cl).await; sub.transfer_fd(STDIN, proc); (captured_status(result, sub.last_exit), s) }; @@ -2019,13 +2025,11 @@ async fn run_capturing_status_inner( for (cond, body) in branches { let saved = proc.opt_errexit; proc.opt_errexit = false; - let cond = - Box::pin(execute_command_line_inner(os.clone(), proc, cond)).await; + let cond = execute_command_line_inner(os.clone(), proc, cond).await; proc.opt_errexit = saved; match cond { Ok(0) => { - result = - Box::pin(run_capturing_status(os.clone(), proc, body)).await; + result = run_capturing_status(os.clone(), proc, body).await; } Ok(_) => continue, Err(control) => result = (String::new(), Err(control)), @@ -2034,7 +2038,7 @@ async fn run_capturing_status_inner( break; } if !matched && let Some(body) = else_body { - result = Box::pin(run_capturing_status(os.clone(), proc, body)).await; + result = run_capturing_status(os.clone(), proc, body).await; } let (s, result) = result; match result { @@ -2052,8 +2056,7 @@ async fn run_capturing_status_inner( loop { let saved = proc.opt_errexit; proc.opt_errexit = false; - let cond = - Box::pin(execute_command_line_inner(os.clone(), proc, condition)).await; + let cond = execute_command_line_inner(os.clone(), proc, condition).await; proc.opt_errexit = saved; let result = match cond { Ok(cond_exit) => { @@ -2061,7 +2064,7 @@ async fn run_capturing_status_inner( break; } let (out, result) = - Box::pin(run_capturing_status(os.clone(), proc, body)).await; + run_capturing_status(os.clone(), proc, body).await; s.push_str(&out); result } @@ -2087,8 +2090,7 @@ async fn run_capturing_status_inner( let mut exit = 0; for val in &expanded { proc.set_env(var, val); - let (out, result) = - Box::pin(run_capturing_status(os.clone(), proc, body)).await; + let (out, result) = run_capturing_status(os.clone(), proc, body).await; s.push_str(&out); match result.map_err(loop_control) { Ok(body_exit) => exit = body_exit, @@ -2117,8 +2119,7 @@ async fn run_capturing_status_inner( } if matched { let result; - (s, result) = - Box::pin(run_capturing_status(os.clone(), proc, &arm.body)).await; + (s, result) = run_capturing_status(os.clone(), proc, &arm.body).await; match result { Ok(body_exit) => exit = body_exit, Err(control) => { @@ -2139,8 +2140,7 @@ async fn run_capturing_status_inner( let compound_cl = vec![(*compound.clone(), None)]; let ignored = u32::from(*negated); proc.errexit_ignored += ignored; - let (s, result) = - Box::pin(run_capturing_status(os.clone(), proc, &compound_cl)).await; + let (s, result) = run_capturing_status(os.clone(), proc, &compound_cl).await; proc.errexit_ignored -= ignored; match result { Ok(exit) if *negated => (i32::from(exit == 0), s), @@ -2158,7 +2158,7 @@ async fn run_capturing_status_inner( } => { let compound_cl = vec![(*compound.clone(), None)]; let (captured, result) = - Box::pin(run_capturing_status(os.clone(), proc, &compound_cl)).await; + run_capturing_status(os.clone(), proc, &compound_cl).await; let compound_exit = captured_status(result, proc.last_exit); // The stage's refusal must not mask an exit in a later function. proc.refused_array = false; @@ -2235,8 +2235,7 @@ async fn run_capturing_status_inner( } else { let inner_cl = vec![(*item.clone(), None)]; let (s, result) = - Box::pin(run_capturing_status(os.clone(), &mut sub, &inner_cl)) - .await; + run_capturing_status(os.clone(), &mut sub, &inner_cl).await; if let Err(control) = result { flow = Some(control); } @@ -2250,7 +2249,7 @@ async fn run_capturing_status_inner( }; let saved_capture = std::mem::replace(&mut proc.capture, true); let saved_output = std::mem::take(&mut proc.captured_output); - let result = Box::pin(execute_item(os.clone(), proc, &cr)).await; + let result = execute_item(os.clone(), proc, &cr).await; proc.capture = saved_capture; let out = std::mem::replace(&mut proc.captured_output, saved_output); match result { @@ -2263,7 +2262,7 @@ async fn run_capturing_status_inner( } else { let inner_cl = vec![(*item.clone(), None)]; let (s, result) = - Box::pin(run_capturing_status(os.clone(), &mut sub, &inner_cl)).await; + run_capturing_status(os.clone(), &mut sub, &inner_cl).await; if let Err(control) = result { flow = Some(control); } @@ -2289,7 +2288,16 @@ async fn run_capturing_status_inner( } /// Inner execute that propagates ControlFlow. -async fn execute_command_line_inner( +#[inline(never)] +fn execute_command_line_inner<'a>( + os: Arc, + proc: &'a mut Process, + command_line: &'a parser::CommandLine, +) -> Execution<'a, Result> { + Box::pin(execute_command_line_body(os, proc, command_line)) +} + +async fn execute_command_line_body( os: Arc, proc: &mut Process, command_line: &parser::CommandLine, @@ -2364,12 +2372,13 @@ async fn execute_command_line_inner( Ok(last_exit) } +#[inline(never)] fn execute_item<'a>( os: Arc, proc: &'a mut Process, item: &'a parser::Item, -) -> impl std::future::Future> + 'a { - stack::run(execute_item_inner(os, proc, item)) +) -> Execution<'a, Result> { + Box::pin(execute_item_inner(os, proc, item)) } async fn execute_item_inner( @@ -2400,31 +2409,34 @@ async fn execute_item_inner( } if tail.is_empty() => { let ignored = u32::from(*negated); proc.errexit_ignored += ignored; - let result = Box::pin(execute_item(os, proc, compound)).await; + let result = execute_item(os, proc, compound).await; proc.errexit_ignored -= ignored; let exit = result?; Ok(if *negated { i32::from(exit == 0) } else { exit }) } - Item::Group(cl) => Box::pin(execute_command_line_inner(os, proc, cl)).await, + Item::Group(cl) => execute_command_line_inner(os, proc, cl).await, Item::Subshell(cl) => { - let mut sub = proc.fork(); - sub.depth += 1; - if let Some(msg) = sub.check_limits() { - sub.err_msg(msg); - return Ok(1); - } - proc.transfer_fd(STDIN, &mut sub); - let r = Box::pin(execute_command_line_inner(os, &mut sub, cl)).await; - sub.transfer_fd(STDIN, proc); - if proc.capture { - proc.captured_output.push_str(&sub.captured_output); - proc.captured_stderr.push_str(&sub.captured_stderr); - } - match r { - Ok(n) => Ok(n), - Err(ControlFlow::Exit(n)) => Ok(n), - Err(e) => Err(e), - } + box_future(|| async { + let mut sub = proc.fork(); + sub.depth += 1; + if let Some(msg) = sub.check_limits() { + sub.err_msg(msg); + return Ok(1); + } + proc.transfer_fd(STDIN, &mut sub); + let r = execute_command_line_inner(os, &mut sub, cl).await; + sub.transfer_fd(STDIN, proc); + if proc.capture { + proc.captured_output.push_str(&sub.captured_output); + proc.captured_stderr.push_str(&sub.captured_stderr); + } + match r { + Ok(n) => Ok(n), + Err(ControlFlow::Exit(n)) => Ok(n), + Err(e) => Err(e), + } + }) + .await } Item::If { branches, @@ -2447,200 +2459,207 @@ async fn execute_item_inner( tail, negated, } => { - // Capture the compound command's output, then pipe it as - // stdin into the tail pipeline. - let compound_cl = vec![(*compound.clone(), None)]; - let mut sub = proc.fork(); - proc.transfer_fd(STDIN, &mut sub); - let os2 = os.clone(); + box_future(|| async { + // Capture the compound command's output, then pipe it as + // stdin into the tail pipeline. + let compound_cl = vec![(*compound.clone(), None)]; + let mut sub = proc.fork(); + proc.transfer_fd(STDIN, &mut sub); + let os2 = os.clone(); - let (tx, rx) = os::pipe(64); - let mut tasks = ForegroundTasks::new(os.as_ref(), proc); - let handle = tokio::task::spawn_local(async move { - let (output, result) = - Box::pin(run_capturing_status(os2, &mut sub, &compound_cl)).await; - if !output.is_empty() { - let _ = tx.send(Bytes::from(output.into_bytes())).await; + let (tx, rx) = os::pipe(64); + let mut tasks = ForegroundTasks::new(os.as_ref(), proc); + let handle = tokio::task::spawn_local(async move { + let (output, result) = run_capturing_status(os2, &mut sub, &compound_cl).await; + if !output.is_empty() { + let _ = tx.send(Bytes::from(output.into_bytes())).await; + } + ( + captured_status(result, sub.last_exit), + std::mem::take(&mut sub.captured_stderr), + take_stdin(&mut sub), + ) + }); + tasks.own(&handle); + + // Set stdin on proc so the tail pipeline inherits it. + proc.set_channel_reader(STDIN, rx); + let mut exit = execute_pipeline(os, proc, tail).await; + // The tail is a pipeline stage, so `exit` in it ends only that stage. + proc.pending_exit = false; + let (compound_exit, mut stderr, unread) = handle.await.unwrap_or_default(); + // The compound's stderr is not piped; pass it on without dropping any, + // except a limit message that this process will report itself. + if let Some(msg) = proc.check_limits() + && stderr.ends_with(&format!("{msg}\n")) + { + stderr.truncate(stderr.len() - msg.len() - 1); } - ( - captured_status(result, sub.last_exit), - std::mem::take(&mut sub.captured_stderr), - take_stdin(&mut sub), - ) - }); - tasks.own(&handle); - - // Set stdin on proc so the tail pipeline inherits it. - proc.set_channel_reader(STDIN, rx); - let mut exit = execute_pipeline(os, proc, tail).await; - // The tail is a pipeline stage, so `exit` in it ends only that stage. - proc.pending_exit = false; - let (compound_exit, mut stderr, unread) = handle.await.unwrap_or_default(); - // The compound's stderr is not piped; pass it on without dropping any, - // except a limit message that this process will report itself. - if let Some(msg) = proc.check_limits() - && stderr.ends_with(&format!("{msg}\n")) - { - stderr.truncate(stderr.len() - msg.len() - 1); - } - if !stderr.is_empty() { - if let Some(tx) = proc.take_err_tx() { - let _ = tx.send(bytes::Bytes::from(stderr)).await; - proc.set_err_tx(tx); - } else { - proc.write_stderr(&stderr); + if !stderr.is_empty() { + if let Some(tx) = proc.take_err_tx() { + let _ = tx.send(bytes::Bytes::from(stderr)).await; + proc.set_err_tx(tx); + } else { + proc.write_stderr(&stderr); + } } - } - proc.close(STDIN); - give_stdin(proc, unread); - stop_at_deadline(proc)?; - if proc.opt_pipefail && exit == 0 { - exit = compound_exit; - } - if *negated { - exit = if exit == 0 { 1 } else { 0 }; - } - Ok(exit) + proc.close(STDIN); + give_stdin(proc, unread); + stop_at_deadline(proc)?; + if proc.opt_pipefail && exit == 0 { + exit = compound_exit; + } + if *negated { + exit = if exit == 0 { 1 } else { 0 }; + } + Ok(exit) + }) + .await } Item::CompoundRedirect { item, redirects } => { - let mut expanded = Vec::new(); - for redir in redirects { - let target = match redir { - Redirect::Write(_, w) - | Redirect::Append(_, w) - | Redirect::Read(_, w) - | Redirect::ReadWrite(_, w) - | Redirect::Clobber(_, w) - | Redirect::DupWrite(_, w) - | Redirect::DupRead(_, w) => expand_word(os.clone(), proc, w).await, - Redirect::HereDoc(..) => String::new(), - }; - expanded.push((redir.clone(), target)); - } - if let Some(msg) = proc.check_limits() { - proc.err_msg(msg); - return Err(ControlFlow::Exit(1)); - } - // For stdin redirects, apply to proc so inner commands can read - let mut stdin_expanded = Vec::new(); - let mut stdout_expanded = Vec::new(); - for (redir, target) in &expanded { - match redir { - Redirect::Read(..) - | Redirect::ReadWrite(..) - | Redirect::DupRead(..) - | Redirect::HereDoc(..) => stdin_expanded.push((redir.clone(), target.clone())), - _ => stdout_expanded.push((redir.clone(), target.clone())), + box_future(|| async { + let mut expanded = Vec::new(); + for redir in redirects { + let target = match redir { + Redirect::Write(_, w) + | Redirect::Append(_, w) + | Redirect::Read(_, w) + | Redirect::ReadWrite(_, w) + | Redirect::Clobber(_, w) + | Redirect::DupWrite(_, w) + | Redirect::DupRead(_, w) => expand_word(os.clone(), proc, w).await, + Redirect::HereDoc(..) => String::new(), + }; + expanded.push((redir.clone(), target)); } - } - // Apply stdin redirects to proc - let mut saved_stdin = Process::empty(); - if !stdin_expanded.is_empty() { - proc.transfer_fd(STDIN, &mut saved_stdin); - if let Err(msg) = apply_redirects(&os, proc, &stdin_expanded).await { - saved_stdin.transfer_fd(STDIN, proc); - proc.err_msg(&format!("strands-shell: {msg}")); - return Ok(1); + if let Some(msg) = proc.check_limits() { + proc.err_msg(msg); + return Err(ControlFlow::Exit(1)); } - } - // For stdout/stderr redirects, capture output and write to target - if !stdout_expanded.is_empty() { - // Open the targets before the body runs. fd 1 and fd 2 each end at one sink, - // decided by the redirects in order. - let mut sinks = [GroupSink::Stdout, GroupSink::Stderr]; - let mut writers = Vec::new(); - for (redir, target) in &stdout_expanded { - let index = match redir { - Redirect::Write(fd, _) - | Redirect::Append(fd, _) - | Redirect::Clobber(fd, _) - | Redirect::DupWrite(fd, _) => match *fd { - STDOUT => 0, - STDERR => 1, - _ => continue, - }, - _ => continue, - }; - if let Redirect::DupWrite(..) = redir { - sinks[index] = match target.as_str() { - "1" => sinks[0], - "2" => sinks[1], + // For stdin redirects, apply to proc so inner commands can read + let mut stdin_expanded = Vec::new(); + let mut stdout_expanded = Vec::new(); + for (redir, target) in &expanded { + match redir { + Redirect::Read(..) + | Redirect::ReadWrite(..) + | Redirect::DupRead(..) + | Redirect::HereDoc(..) => { + stdin_expanded.push((redir.clone(), target.clone())) + } + _ => stdout_expanded.push((redir.clone(), target.clone())), + } + } + // Apply stdin redirects to proc + let mut saved_stdin = Process::empty(); + if !stdin_expanded.is_empty() { + proc.transfer_fd(STDIN, &mut saved_stdin); + if let Err(msg) = apply_redirects(&os, proc, &stdin_expanded).await { + saved_stdin.transfer_fd(STDIN, proc); + proc.err_msg(&format!("strands-shell: {msg}")); + return Ok(1); + } + } + // For stdout/stderr redirects, capture output and write to target + if !stdout_expanded.is_empty() { + // Open the targets before the body runs. fd 1 and fd 2 each end at one sink, + // decided by the redirects in order. + let mut sinks = [GroupSink::Stdout, GroupSink::Stderr]; + let mut writers = Vec::new(); + for (redir, target) in &stdout_expanded { + let index = match redir { + Redirect::Write(fd, _) + | Redirect::Append(fd, _) + | Redirect::Clobber(fd, _) + | Redirect::DupWrite(fd, _) => match *fd { + STDOUT => 0, + STDERR => 1, + _ => continue, + }, _ => continue, }; - continue; - } - let flags = match redir { - Redirect::Append(..) => OpenFlags::append(), - _ => OpenFlags::write(), - }; - match os.open(proc, target, flags).await { - Ok(fd) => { - if let Ok(w) = proc.take_writer(fd) { - sinks[index] = GroupSink::File(writers.len()); - writers.push((target, w, String::new())); - } + if let Redirect::DupWrite(..) = redir { + sinks[index] = match target.as_str() { + "1" => sinks[0], + "2" => sinks[1], + _ => continue, + }; + continue; } - Err(e) => { - if !stdin_expanded.is_empty() { - proc.close(STDIN); - saved_stdin.transfer_fd(STDIN, proc); + let flags = match redir { + Redirect::Append(..) => OpenFlags::append(), + _ => OpenFlags::write(), + }; + match os.open(proc, target, flags).await { + Ok(fd) => { + if let Ok(w) = proc.take_writer(fd) { + sinks[index] = GroupSink::File(writers.len()); + writers.push((target, w, String::new())); + } + } + Err(e) => { + if !stdin_expanded.is_empty() { + proc.close(STDIN); + saved_stdin.transfer_fd(STDIN, proc); + } + proc.err_msg(&format!("strands-shell: {target}: {e}")); + return Ok(1); } - proc.err_msg(&format!("strands-shell: {target}: {e}")); - return Ok(1); } } - } - // A redirected stream is captured while the body runs, even when the caller - // installed a writer on it. - let mut outer_fds = Process::empty(); - let unredirected = [GroupSink::Stdout, GroupSink::Stderr]; - for ((fd, sink), default) in - [STDOUT, STDERR].into_iter().zip(sinks).zip(unredirected) - { - if sink != default { - proc.transfer_fd(fd, &mut outer_fds); + // A redirected stream is captured while the body runs, even when the caller + // installed a writer on it. + let mut outer_fds = Process::empty(); + let unredirected = [GroupSink::Stdout, GroupSink::Stderr]; + for ((fd, sink), default) in + [STDOUT, STDERR].into_iter().zip(sinks).zip(unredirected) + { + if sink != default { + proc.transfer_fd(fd, &mut outer_fds); + } } - } - let outer_stderr = std::mem::take(&mut proc.captured_stderr); - let inner_cl = vec![(*item.clone(), None)]; - let (captured, result) = - Box::pin(run_capturing_status(os.clone(), proc, &inner_cl)).await; - let stderr_text = std::mem::replace(&mut proc.captured_stderr, outer_stderr); - for fd in [STDOUT, STDERR] { - if outer_fds.has_fd(fd) { - proc.close(fd); - outer_fds.transfer_fd(fd, proc); + let outer_stderr = std::mem::take(&mut proc.captured_stderr); + let inner_cl = vec![(*item.clone(), None)]; + let (captured, result) = + run_capturing_status(os.clone(), proc, &inner_cl).await; + let stderr_text = std::mem::replace(&mut proc.captured_stderr, outer_stderr); + for fd in [STDOUT, STDERR] { + if outer_fds.has_fd(fd) { + proc.close(fd); + outer_fds.transfer_fd(fd, proc); + } } - } - if !stdin_expanded.is_empty() { - proc.close(STDIN); - saved_stdin.transfer_fd(STDIN, proc); - } - for (sink, text) in [(sinks[0], &captured), (sinks[1], &stderr_text)] { - match sink { - GroupSink::Stdout => proc.write_stdout(text), - GroupSink::Stderr => proc.write_stderr(text), - GroupSink::File(index) => writers[index].2.push_str(text), + if !stdin_expanded.is_empty() { + proc.close(STDIN); + saved_stdin.transfer_fd(STDIN, proc); } - } - for (target, mut w, text) in writers { - if let Err(e) = w.write_all(text.as_bytes()).await { - proc.err_msg(&format!("strands-shell: {target}: {e}")); - proc.last_exit = 1; + for (sink, text) in [(sinks[0], &captured), (sinks[1], &stderr_text)] { + match sink { + GroupSink::Stdout => proc.write_stdout(text), + GroupSink::Stderr => proc.write_stderr(text), + GroupSink::File(index) => writers[index].2.push_str(text), + } } + for (target, mut w, text) in writers { + if let Err(e) = w.write_all(text.as_bytes()).await { + proc.err_msg(&format!("strands-shell: {target}: {e}")); + proc.last_exit = 1; + } + } + // Yield to let write-back tasks flush + tokio::task::yield_now().await; + let exit = settle_host_writes(proc, proc.last_exit).await; + result.map(|_| exit) + } else { + let result = execute_item(os, proc, item).await; + if !stdin_expanded.is_empty() { + proc.close(STDIN); + saved_stdin.transfer_fd(STDIN, proc); + } + result } - // Yield to let write-back tasks flush - tokio::task::yield_now().await; - let exit = settle_host_writes(proc, proc.last_exit).await; - result.map(|_| exit) - } else { - let result = Box::pin(execute_item(os, proc, item)).await; - if !stdin_expanded.is_empty() { - proc.close(STDIN); - saved_stdin.transfer_fd(STDIN, proc); - } - result - } + }) + .await } } } @@ -2966,8 +2985,10 @@ async fn run_special_redirected( return Err(ControlFlow::Exit(2)); } let body_os = os.clone(); - run_redirected(os, proc, &redirects, move |proc| { - Box::pin(run_special(body_os, proc, special, args)) + box_future(|| { + run_redirected(os, proc, &redirects, move |proc| { + Box::pin(run_special(body_os, proc, special, args)) + }) }) .await .unwrap_or(Ok(1)) @@ -3098,7 +3119,16 @@ fn xtrace(proc: &mut Process, args: &[String]) { } /// Execute a pipeline, checking for break/continue/exit builtins. -async fn execute_pipeline_checked( +#[inline(never)] +fn execute_pipeline_checked<'a>( + os: Arc, + proc: &'a mut Process, + pipeline: &'a [parser::Command], +) -> Execution<'a, Result> { + Box::pin(execute_pipeline_checked_inner(os, proc, pipeline)) +} + +async fn execute_pipeline_checked_inner( os: Arc, proc: &mut Process, pipeline: &[parser::Command], @@ -3133,114 +3163,136 @@ async fn execute_pipeline_checked( args: pipeline[0].args[1..].to_vec(), redirects: pipeline[0].redirects.clone(), }; - return Box::pin(execute_pipeline_checked(os, proc, &[unprefixed])).await; + return execute_pipeline_checked(os, proc, &[unprefixed]).await; } match name.as_str() { "break" => { - let n = get_numeric_arg(os.clone(), proc, pipeline).await; - return Err(ControlFlow::Break(n)); + return box_future(|| async { + let n = get_numeric_arg(os.clone(), proc, pipeline).await; + Err(ControlFlow::Break(n)) + }) + .await; } "continue" => { - let n = get_numeric_arg(os.clone(), proc, pipeline).await; - return Err(ControlFlow::Continue(n)); + return box_future(|| async { + let n = get_numeric_arg(os.clone(), proc, pipeline).await; + Err(ControlFlow::Continue(n)) + }) + .await; } "exit" => { - let n = if pipeline[0].args.len() > 1 { - let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; - // The arg can expand to nothing (e.g. `exit $UNSET`), - // so index defensively rather than `args[0]`. - args.first().and_then(|s| s.parse().ok()).unwrap_or(1) - } else { - proc.last_exit - }; - return Err(ControlFlow::Exit(n)); + return box_future(|| async { + let n = if pipeline[0].args.len() > 1 { + let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; + // The arg can expand to nothing (e.g. `exit $UNSET`), + // so index defensively rather than `args[0]`. + args.first().and_then(|s| s.parse().ok()).unwrap_or(1) + } else { + proc.last_exit + }; + Err(ControlFlow::Exit(n)) + }) + .await; } "return" => { - let n = if pipeline[0].args.len() > 1 { - let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; - args.first().and_then(|s| s.parse().ok()).unwrap_or(1) - } else { - proc.last_exit - }; - return Err(ControlFlow::Return(n)); + return box_future(|| async { + let n = if pipeline[0].args.len() > 1 { + let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; + args.first().and_then(|s| s.parse().ok()).unwrap_or(1) + } else { + proc.last_exit + }; + Err(ControlFlow::Return(n)) + }) + .await; } "eval" => { - let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; - xtrace(proc, &[&["eval".into()], &args[..]].concat()); - return run_special_redirected(os, proc, &pipeline[0], Special::Eval, args) - .await; + return box_future(|| async { + let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; + xtrace(proc, &[&["eval".into()], &args[..]].concat()); + run_special_redirected(os, proc, &pipeline[0], Special::Eval, args).await + }) + .await; } "." | "source" => { - let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; - xtrace(proc, &[&[".".into()], &args[..]].concat()); - return run_special_redirected(os, proc, &pipeline[0], Special::Source, args) - .await; + return box_future(|| async { + let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; + xtrace(proc, &[&[".".into()], &args[..]].concat()); + run_special_redirected(os, proc, &pipeline[0], Special::Source, args).await + }) + .await; } "exec" => { - let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; - if args.is_empty() { - // `exec` with no args — just apply redirects - // TODO: apply redirects to the shell process - return Ok(0); - } - // exec with args — try to run as a command - // For now, treat it like running the command directly - let fake_cmd = parser::Command { - env: pipeline[0].env.clone(), - args: pipeline[0].args[1..].to_vec(), - redirects: pipeline[0].redirects.clone(), - }; - let exit = execute_pipeline(os, proc, &[fake_cmd]).await; - proc.pending_exit = false; - return Err(ControlFlow::Exit(exit)); + return box_future(|| async { + let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; + if args.is_empty() { + // `exec` with no args — just apply redirects + // TODO: apply redirects to the shell process + return Ok(0); + } + // exec with args — try to run as a command + // For now, treat it like running the command directly + let fake_cmd = parser::Command { + env: pipeline[0].env.clone(), + args: pipeline[0].args[1..].to_vec(), + redirects: pipeline[0].redirects.clone(), + }; + let exit = execute_pipeline(os, proc, &[fake_cmd]).await; + proc.pending_exit = false; + Err(ControlFlow::Exit(exit)) + }) + .await; } "command" => { - if pipeline[0].args.len() < 2 { - return Ok(0); - } - let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; - if args.first().map(|s| s.as_str()) == Some("-v") - || args.first().map(|s| s.as_str()) == Some("-V") - { - let verbose = args[0] == "-V"; - let mut status = 0; - for name in &args[1..] { - match lookup_command(&*os, proc, name, true).await { - Some(CommandLookup::Builtin) => { - if verbose { - let label = if is_special(name) { - "a special shell builtin" + return box_future(|| async { + if pipeline[0].args.len() < 2 { + return Ok(0); + } + let args = expand_words(os.clone(), proc, &pipeline[0].args[1..]).await; + if args.first().map(|s| s.as_str()) == Some("-v") + || args.first().map(|s| s.as_str()) == Some("-V") + { + let verbose = args[0] == "-V"; + let mut status = 0; + for name in &args[1..] { + match lookup_command(&*os, proc, name, true).await { + Some(CommandLookup::Builtin) => { + if verbose { + let label = if is_special(name) { + "a special shell builtin" + } else { + "a shell builtin" + }; + proc.out_msg(&format!("{name} is {label}")); } else { - "a shell builtin" - }; - proc.out_msg(&format!("{name} is {label}")); - } else { - proc.out_msg(name); + proc.out_msg(name); + } } - } - Some(CommandLookup::Function) => { - if verbose { - proc.out_msg(&format!("{name} is a shell function")); - } else { - proc.out_msg(name); + Some(CommandLookup::Function) => { + if verbose { + proc.out_msg(&format!("{name} is a shell function")); + } else { + proc.out_msg(name); + } } + Some(CommandLookup::Path(path)) => proc.out_msg(&path), + None => status = 1, } - Some(CommandLookup::Path(path)) => proc.out_msg(&path), - None => status = 1, } + return Ok(status); } - return Ok(status); - } - // `command name args...` — run name bypassing functions - let fake_cmd = parser::Command { - env: pipeline[0].env.clone(), - args: pipeline[0].args[1..].to_vec(), - redirects: pipeline[0].redirects.clone(), - }; - let exit = execute_pipeline(os, proc, &[fake_cmd]).await; - proc.pending_exit = false; - stop_at_deadline(proc)?; - return Ok(exit); + // `command name args...` — run name bypassing functions + let fake_cmd = parser::Command { + env: pipeline[0].env.clone(), + args: pipeline[0].args[1..].to_vec(), + redirects: pipeline[0].redirects.clone(), + }; + let exit = execute_pipeline(os, proc, &[fake_cmd]).await; + proc.pending_exit = false; + stop_at_deadline(proc)?; + Ok(exit) + }) + .await; } _ => {} } @@ -3417,7 +3469,17 @@ fn run_stage( }) } -async fn run_pipeline( +#[inline(never)] +fn run_pipeline<'a>( + os: Arc, + proc: &'a mut Process, + pipeline: &'a [parser::Command], + capture: bool, +) -> Execution<'a, (i32, String)> { + Box::pin(run_pipeline_inner(os, proc, pipeline, capture)) +} + +async fn run_pipeline_inner( os: Arc, shell_proc: &mut Process, pipeline: &[parser::Command], @@ -3516,11 +3578,11 @@ async fn run_pipeline( // Resolve executables: for each stage, if the command is not a builtin, // function, or registered command, try to resolve it as an executable file. - for i in 0..len { - if expanded_args[i].is_empty() { + for args in &mut expanded_args { + if args.is_empty() { continue; } - let cmd = &expanded_args[i][0]; + let cmd = &args[0]; if builtins::lookup(cmd).is_some() || shell_proc.get_function(cmd).is_some() || commands::lookup(cmd).is_some() @@ -3528,27 +3590,29 @@ async fn run_pipeline( { continue; } - if let Some((_path, target)) = resolve_executable(os.as_ref(), shell_proc, cmd).await { + if let Some((_path, target)) = + box_future(|| resolve_executable(os.as_ref(), shell_proc, cmd)).await + { match target { ExecTarget::Multicall(name) => { - expanded_args[i][0] = name; + args[0] = name; } ExecTarget::Shebang(interp) => { // Check if the interpreter resolves to a multicall binary if let Some((_, ExecTarget::Multicall(mcname))) = - resolve_executable(os.as_ref(), shell_proc, &interp[0]).await + box_future(|| resolve_executable(os.as_ref(), shell_proc, &interp[0])).await { // Multicall: rewrite to [orig_args...] let script_path = interp.last().unwrap().clone(); - let orig_args = expanded_args[i][1..].to_vec(); + let orig_args = args[1..].to_vec(); let mut new_args = vec![mcname, script_path]; new_args.extend(orig_args); - expanded_args[i] = new_args; + *args = new_args; } else { - let orig_args = expanded_args[i][1..].to_vec(); + let orig_args = args[1..].to_vec(); let mut new_args = interp; new_args.extend(orig_args); - expanded_args[i] = new_args; + *args = new_args; } } } @@ -3599,147 +3663,152 @@ async fn run_pipeline( // Single command: check for builtins if len == 1 { if let Some(f) = builtins::lookup(&expanded_args[0][0]) { - let name = expanded_args[0][0].clone(); - let args: Vec = expanded_args[0][1..].to_vec(); - - let mut io_proc = shell_proc.fork(); - // Inherit stdin from shell_proc if present (e.g. CompoundPipeline). A stdin - // redirect sets the caller's stdin aside until the builtin ends. - shell_proc.transfer_fd(STDIN, &mut io_proc); - let redirects_stdin = replaces_stdin(&expanded_redirects[0]); - let mut kept_stdin = redirects_stdin.then(|| take_stdin(&mut io_proc)).flatten(); - let (out_tx, out_rx) = os::pipe(64); - let (err_tx, err_rx) = os::pipe(64); - // A writer the caller installed takes the place of the local pipe. - if shell_proc.inherit_channel_writer(STDOUT, &mut io_proc) { - drop(out_tx); - } else { - io_proc.set_channel_writer(STDOUT, out_tx); - } - let caller_stderr = shell_proc.inherit_channel_writer(STDERR, &mut io_proc); - if !caller_stderr { - io_proc.set_channel_writer(STDERR, err_tx.clone()); - } - shell_proc.set_err_tx(err_tx); - - // Apply redirects to the io_proc so builtins see them - if let Err(e) = apply_redirects(&os, &mut io_proc, &expanded_redirects[0]).await { - shell_proc.clear_err_tx(); - if caller_stderr && let Some(tx) = shell_proc.stderr_channel() { - shell_proc.set_err_tx(tx); + return box_future(|| async { + let name = expanded_args[0][0].clone(); + let args: Vec = expanded_args[0][1..].to_vec(); + + let mut io_proc = shell_proc.fork(); + // Inherit stdin from shell_proc if present (e.g. CompoundPipeline). A stdin + // redirect sets the caller's stdin aside until the builtin ends. + shell_proc.transfer_fd(STDIN, &mut io_proc); + let redirects_stdin = replaces_stdin(&expanded_redirects[0]); + let mut kept_stdin = redirects_stdin.then(|| take_stdin(&mut io_proc)).flatten(); + let (out_tx, out_rx) = os::pipe(64); + let (err_tx, err_rx) = os::pipe(64); + // A writer the caller installed takes the place of the local pipe. + if shell_proc.inherit_channel_writer(STDOUT, &mut io_proc) { + drop(out_tx); + } else { + io_proc.set_channel_writer(STDOUT, out_tx); } - shell_proc.err_msg(&format!("strands-shell: {e}")); - shell_proc.clear_err_tx(); - give_stdin(shell_proc, kept_stdin); - return (1, String::new()); - } - // Builtin diagnostics follow the command's own stderr redirection. - if let Some(tx) = io_proc.stderr_channel() { - shell_proc.set_err_tx(tx); - } + let caller_stderr = shell_proc.inherit_channel_writer(STDERR, &mut io_proc); + if !caller_stderr { + io_proc.set_channel_writer(STDERR, err_tx.clone()); + } + shell_proc.set_err_tx(err_tx); - // Drain stdout/stderr concurrently with the builtin to avoid - // deadlock when the builtin produces more output than the channel - // capacity. - let max_output = shell_proc.max_output; - enum Drain { - Capture(tokio::task::JoinHandle), - Copy(tokio::task::JoinHandle<()>), - } - let stdout_drain = if capture { - Drain::Capture(tokio::task::spawn_local(async move { - let mut reader = FdReader::from_receiver(out_rx); - os::read_to_string_lossy_limited(&mut reader, max_output) - .await - .unwrap_or_default() - })) - } else { - Drain::Copy(tokio::task::spawn_local(async move { - let mut reader = FdReader::from_receiver(out_rx); - #[cfg(not(target_arch = "wasm32"))] - { - let _ = tokio::io::copy(&mut reader, &mut tokio::io::stdout()).await; - } - #[cfg(target_arch = "wasm32")] - { - let mut buf = Vec::new(); - let _ = reader.read_to_end(&mut buf).await; - let _ = std::io::Write::write_all(&mut std::io::stdout(), &buf); + // Apply redirects to the io_proc so builtins see them + if let Err(e) = apply_redirects(&os, &mut io_proc, &expanded_redirects[0]).await { + shell_proc.clear_err_tx(); + if caller_stderr && let Some(tx) = shell_proc.stderr_channel() { + shell_proc.set_err_tx(tx); } - })) - }; - let stderr_drain = if capture { - Drain::Capture(tokio::task::spawn_local(async move { - let mut reader = FdReader::from_receiver(err_rx); - os::read_to_string_lossy_limited(&mut reader, max_output) - .await - .unwrap_or_default() - })) - } else { - Drain::Copy(tokio::task::spawn_local(async move { - let mut reader = FdReader::from_receiver(err_rx); - #[cfg(not(target_arch = "wasm32"))] - { - let _ = tokio::io::copy(&mut reader, &mut tokio::io::stderr()).await; - } - #[cfg(target_arch = "wasm32")] - { - let mut buf = Vec::new(); - let _ = reader.read_to_end(&mut buf).await; - let _ = std::io::Write::write_all(&mut std::io::stderr(), &buf); - } - })) - }; - for drain in [&stdout_drain, &stderr_drain] { - match drain { - Drain::Capture(h) => tasks.own(h), - Drain::Copy(h) => tasks.own(h), + shell_proc.err_msg(&format!("strands-shell: {e}")); + shell_proc.clear_err_tx(); + give_stdin(shell_proc, kept_stdin); + return (1, String::new()); + } + // Builtin diagnostics follow the command's own stderr redirection. + if let Some(tx) = io_proc.stderr_channel() { + shell_proc.set_err_tx(tx); } - } - let io_cell = RefCell::new(io_proc); - let result = CURRENT_KERNEL - .scope( - os.clone(), - CURRENT_PROCESS.scope(io_cell, async { - let r = f(os.as_ref(), shell_proc, &args).await; - // Transfer stdin back so it survives across loop iterations, or give - // back the caller's own when a redirect replaced it. - if redirects_stdin { - give_stdin(shell_proc, kept_stdin.take()); - } else { - CURRENT_PROCESS.with(|p| p.borrow_mut().transfer_fd(STDIN, shell_proc)); + // Drain stdout/stderr concurrently with the builtin to avoid + // deadlock when the builtin produces more output than the channel + // capacity. + let max_output = shell_proc.max_output; + enum Drain { + Capture(tokio::task::JoinHandle), + Copy(tokio::task::JoinHandle<()>), + } + let stdout_drain = if capture { + Drain::Capture(tokio::task::spawn_local(async move { + let mut reader = FdReader::from_receiver(out_rx); + os::read_to_string_lossy_limited(&mut reader, max_output) + .await + .unwrap_or_default() + })) + } else { + Drain::Copy(tokio::task::spawn_local(async move { + let mut reader = FdReader::from_receiver(out_rx); + #[cfg(not(target_arch = "wasm32"))] + { + let _ = tokio::io::copy(&mut reader, &mut tokio::io::stdout()).await; } - r - }), - ) - .await; - if let Err(e) = &result { - shell_proc.err_msg(&format!("strands-shell: {name}: {e}")); - } - shell_proc.clear_err_tx(); + #[cfg(target_arch = "wasm32")] + { + let mut buf = Vec::new(); + let _ = reader.read_to_end(&mut buf).await; + let _ = std::io::Write::write_all(&mut std::io::stdout(), &buf); + } + })) + }; + let stderr_drain = if capture { + Drain::Capture(tokio::task::spawn_local(async move { + let mut reader = FdReader::from_receiver(err_rx); + os::read_to_string_lossy_limited(&mut reader, max_output) + .await + .unwrap_or_default() + })) + } else { + Drain::Copy(tokio::task::spawn_local(async move { + let mut reader = FdReader::from_receiver(err_rx); + #[cfg(not(target_arch = "wasm32"))] + { + let _ = tokio::io::copy(&mut reader, &mut tokio::io::stderr()).await; + } + #[cfg(target_arch = "wasm32")] + { + let mut buf = Vec::new(); + let _ = reader.read_to_end(&mut buf).await; + let _ = std::io::Write::write_all(&mut std::io::stderr(), &buf); + } + })) + }; + for drain in [&stdout_drain, &stderr_drain] { + match drain { + Drain::Capture(h) => tasks.own(h), + Drain::Copy(h) => tasks.own(h), + } + } - let stdout_str = match stdout_drain { - Drain::Capture(h) => h.await.unwrap_or_default(), - Drain::Copy(h) => { - let _ = h.await; - String::new() + let io_cell = RefCell::new(io_proc); + let result = CURRENT_KERNEL + .scope( + os.clone(), + CURRENT_PROCESS.scope(io_cell, async { + let r = f(os.as_ref(), shell_proc, &args).await; + // Transfer stdin back so it survives across loop iterations, or give + // back the caller's own when a redirect replaced it. + if redirects_stdin { + give_stdin(shell_proc, kept_stdin.take()); + } else { + CURRENT_PROCESS + .with(|p| p.borrow_mut().transfer_fd(STDIN, shell_proc)); + } + r + }), + ) + .await; + if let Err(e) = &result { + shell_proc.err_msg(&format!("strands-shell: {name}: {e}")); } - }; - let stderr_str = match stderr_drain { - Drain::Capture(h) => h.await.unwrap_or_default(), - Drain::Copy(h) => { - let _ = h.await; - String::new() + shell_proc.clear_err_tx(); + + let stdout_str = match stdout_drain { + Drain::Capture(h) => h.await.unwrap_or_default(), + Drain::Copy(h) => { + let _ = h.await; + String::new() + } + }; + let stderr_str = match stderr_drain { + Drain::Capture(h) => h.await.unwrap_or_default(), + Drain::Copy(h) => { + let _ = h.await; + String::new() + } + }; + if capture && !stderr_str.is_empty() { + shell_proc.captured_stderr.push_str(&stderr_str); } - }; - if capture && !stderr_str.is_empty() { - shell_proc.captured_stderr.push_str(&stderr_str); - } - let status = result.unwrap_or(1); - let status = report_finished(os.as_ref(), shell_proc, requests[0].take(), status).await; - return (status, stdout_str); + let status = result.unwrap_or(1); + let status = + report_finished(os.as_ref(), shell_proc, requests[0].take(), status).await; + (status, stdout_str) + }) + .await; } // Single command: check for functions @@ -3749,26 +3818,21 @@ async fn run_pipeline( shell_proc.push_local_scope(); shell_proc.depth += 1; let result = if expanded_redirects[0].is_empty() { - Some( - Box::pin(execute_command_line_inner( - os.clone(), - shell_proc, - &func_body, - )) - .await, - ) + Some(execute_command_line_inner(os.clone(), shell_proc, &func_body).await) } else { let body_os = os.clone(); - run_redirected( - os.clone(), - shell_proc, - &expanded_redirects[0], - move |proc| { - Box::pin(async move { - execute_command_line_inner(body_os, proc, &func_body).await - }) - }, - ) + box_future(|| { + run_redirected( + os.clone(), + shell_proc, + &expanded_redirects[0], + move |proc| { + Box::pin(async move { + execute_command_line_inner(body_os, proc, &func_body).await + }) + }, + ) + }) .await }; // A failed redirect leaves the function's check unreported, so it is dropped. @@ -3801,361 +3865,364 @@ async fn run_pipeline( } } - // Build a process for each stage - let mut procs: Vec = (0..len).map(|_| shell_proc.fork()).collect(); - // Track each earlier stage's writes separately from the last stage. - let early_host_writes: Vec = procs[..len - 1] - .iter_mut() - .map(Process::isolate_host_writes) - .collect(); - - // Inherit stdin from shell_proc if present (used by CompoundPipeline). The first stage hands - // back what it leaves unread when it ends, or a stdin redirect sets it aside. - shell_proc.transfer_fd(STDIN, &mut procs[0]); - let redirects_stdin = replaces_stdin(&expanded_redirects[0]); - let kept_stdin = redirects_stdin.then(|| take_stdin(&mut procs[0])).flatten(); - let (stdin_back, mut stdin_returned) = tokio::sync::oneshot::channel(); - let mut stdin_back = (!redirects_stdin).then_some(stdin_back); - - // Apply per-command env prefixes - for (i, env) in expanded_env.iter().enumerate() { - for (k, v) in env { - procs[i].set_env(k, v); + box_future(|| async { + // Build a process for each stage + let mut procs: Vec = (0..len).map(|_| shell_proc.fork()).collect(); + // Track each earlier stage's writes separately from the last stage. + let early_host_writes: Vec = procs[..len - 1] + .iter_mut() + .map(Process::isolate_host_writes) + .collect(); + + // Inherit stdin from shell_proc if present (used by CompoundPipeline). The first stage hands + // back what it leaves unread when it ends, or a stdin redirect sets it aside. + shell_proc.transfer_fd(STDIN, &mut procs[0]); + let redirects_stdin = replaces_stdin(&expanded_redirects[0]); + let kept_stdin = redirects_stdin.then(|| take_stdin(&mut procs[0])).flatten(); + let (stdin_back, mut stdin_returned) = tokio::sync::oneshot::channel(); + let mut stdin_back = (!redirects_stdin).then_some(stdin_back); + + // Apply per-command env prefixes + for (i, env) in expanded_env.iter().enumerate() { + for (k, v) in env { + procs[i].set_env(k, v); + } } - } - - // Connect pipes between adjacent stages - for i in 0..len - 1 { - let (tx, rx) = os::pipe(64); - procs[i].set_channel_writer(STDOUT, tx); - procs[i + 1].set_channel_reader(STDIN, rx); - } - // Last stage stdout: the caller's writer, or a channel back to us - let (last_tx, last_rx) = os::pipe(64); - if shell_proc.inherit_channel_writer(STDOUT, &mut procs[len - 1]) { - drop(last_tx); - } else { - procs[len - 1].set_channel_writer(STDOUT, last_tx); - } + // Connect pipes between adjacent stages + for i in 0..len - 1 { + let (tx, rx) = os::pipe(64); + procs[i].set_channel_writer(STDOUT, tx); + procs[i + 1].set_channel_reader(STDIN, rx); + } - // Every stage gets the caller's stderr writer, or a stderr channel back to us - let mut stderr_rxs = Vec::with_capacity(len); - for p in &mut procs { - let (tx, rx) = os::pipe(64); - if !shell_proc.inherit_channel_writer(STDERR, p) { - p.set_channel_writer(STDERR, tx.clone()); + // Last stage stdout: the caller's writer, or a channel back to us + let (last_tx, last_rx) = os::pipe(64); + if shell_proc.inherit_channel_writer(STDOUT, &mut procs[len - 1]) { + drop(last_tx); + } else { + procs[len - 1].set_channel_writer(STDOUT, last_tx); } - p.set_err_tx(tx); - stderr_rxs.push(rx); - } - // Apply redirections to each stage - for (i, redirs) in expanded_redirects.iter().enumerate() { - if let Err(e) = apply_redirects(&os, &mut procs[i], redirs).await { - shell_proc.err_msg(&format!("strands-shell: {e}")); - if redirects_stdin { - give_stdin(shell_proc, kept_stdin); - } else { - procs[0].transfer_fd(STDIN, shell_proc); + // Every stage gets the caller's stderr writer, or a stderr channel back to us + let mut stderr_rxs = Vec::with_capacity(len); + for p in &mut procs { + let (tx, rx) = os::pipe(64); + if !shell_proc.inherit_channel_writer(STDERR, p) { + p.set_channel_writer(STDERR, tx.clone()); } - return (1, String::new()); + p.set_err_tx(tx); + stderr_rxs.push(rx); } - if let Some(tx) = procs[i].stderr_channel() { - procs[i].set_err_tx(tx); - } - } - // Spawn each stage as a tokio task with CURRENT_PROCESS set - // Use spawn_local so builtins (non-Send futures) can run in pipeline stages - let mut handles = Vec::with_capacity(len); - for (i, child) in procs.into_iter().enumerate() { - let back = if i == 0 { stdin_back.take() } else { None }; - if expanded_args[i].is_empty() { - if let Some(back) = back { - let mut child = child; - let _ = back.send(take_stdin(&mut child)); + // Apply redirections to each stage + for (i, redirs) in expanded_redirects.iter().enumerate() { + if let Err(e) = apply_redirects(&os, &mut procs[i], redirs).await { + shell_proc.err_msg(&format!("strands-shell: {e}")); + if redirects_stdin { + give_stdin(shell_proc, kept_stdin); + } else { + procs[0].transfer_fd(STDIN, shell_proc); + } + return (1, String::new()); + } + if let Some(tx) = procs[i].stderr_channel() { + procs[i].set_err_tx(tx); } - continue; } - let name = expanded_args[i][0].clone(); - let args: Vec = expanded_args[i][1..].to_vec(); - let os = os.clone(); - - // Check for builtin, function, or script before spawning - let builtin = builtins::lookup(&name); - let func_body = child.get_function(&name).cloned(); - let is_script = name == "lash" || name == "sh"; - - if builtin.is_some() || func_body.is_some() || is_script { - // Builtins/functions: child has pipe fds (for I/O via CURRENT_PROCESS), - // state_proc is a fork with env/functions but no fds (for builtin &mut Process) - let mut state_proc = child.fork(); - if is_script { - // The child shell's argv[0], used as `$0` by `sh -c`. - state_proc.arg0 = name.clone(); + + // Spawn each stage as a tokio task with CURRENT_PROCESS set + // Use spawn_local so builtins (non-Send futures) can run in pipeline stages + let mut handles = Vec::with_capacity(len); + for (i, child) in procs.into_iter().enumerate() { + let back = if i == 0 { stdin_back.take() } else { None }; + if expanded_args[i].is_empty() { + if let Some(back) = back { + let mut child = child; + let _ = back.send(take_stdin(&mut child)); + } + continue; } - let handle = tokio::task::spawn_local(CURRENT_KERNEL.scope( - os.clone(), - run_stage(back, child, async move { - if is_script { - // Hand the stage's stdin to the script's commands. - CURRENT_PROCESS - .with(|p| p.borrow_mut().transfer_fd(STDIN, &mut state_proc)); - if args.is_empty() { - return 0; - } - // Capture both streams, so each reaches this stage's redirected fd. - state_proc.capture = true; - state_proc.clear_err_tx(); - let exit = run_script(os, &mut state_proc, &args[0], &args[1..]).await; - CURRENT_PROCESS - .with(|p| state_proc.transfer_fd(STDIN, &mut p.borrow_mut())); - let output = std::mem::take(&mut state_proc.captured_output); - if !output.is_empty() - && let Ok(mut w) = crate::io::stdout() - { - let _ = w.write_all(output.as_bytes()).await; + let name = expanded_args[i][0].clone(); + let args: Vec = expanded_args[i][1..].to_vec(); + let os = os.clone(); + + // Check for builtin, function, or script before spawning + let builtin = builtins::lookup(&name); + let func_body = child.get_function(&name).cloned(); + let is_script = name == "lash" || name == "sh"; + + if builtin.is_some() || func_body.is_some() || is_script { + // Builtins/functions: child has pipe fds (for I/O via CURRENT_PROCESS), + // state_proc is a fork with env/functions but no fds (for builtin &mut Process) + let mut state_proc = child.fork(); + if is_script { + // The child shell's argv[0], used as `$0` by `sh -c`. + state_proc.arg0 = name.clone(); + } + let handle = tokio::task::spawn_local(CURRENT_KERNEL.scope( + os.clone(), + run_stage(back, child, async move { + if is_script { + // Hand the stage's stdin to the script's commands. + CURRENT_PROCESS + .with(|p| p.borrow_mut().transfer_fd(STDIN, &mut state_proc)); + if args.is_empty() { + return 0; + } + // Capture both streams, so each reaches this stage's redirected fd. + state_proc.capture = true; + state_proc.clear_err_tx(); + let exit = run_script(os, &mut state_proc, &args[0], &args[1..]).await; + CURRENT_PROCESS + .with(|p| state_proc.transfer_fd(STDIN, &mut p.borrow_mut())); + let output = std::mem::take(&mut state_proc.captured_output); + if !output.is_empty() + && let Ok(mut w) = crate::io::stdout() + { + let _ = w.write_all(output.as_bytes()).await; + } + let errors = std::mem::take(&mut state_proc.captured_stderr); + if !errors.is_empty() + && let Ok(mut w) = crate::io::stderr() + { + let _ = w.write_all(errors.as_bytes()).await; + } + return exit; } - let errors = std::mem::take(&mut state_proc.captured_stderr); - if !errors.is_empty() - && let Ok(mut w) = crate::io::stderr() - { - let _ = w.write_all(errors.as_bytes()).await; + if let Some(f) = builtin { + return match f(os.as_ref(), &mut state_proc, &args).await { + Ok(code) => code, + // A reader that went away stops the writer quietly, as SIGPIPE does. + Err(e) + if e.downcast_ref::().is_some_and(|io| { + io.kind() == std::io::ErrorKind::BrokenPipe + }) => + { + 1 + } + Err(e) => { + if let Ok(mut w) = crate::io::stderr() { + let _ = w + .write_all( + format!("strands-shell: {name}: {e}\n").as_bytes(), + ) + .await; + } + 1 + } + }; } - return exit; - } - if let Some(f) = builtin { - return match f(os.as_ref(), &mut state_proc, &args).await { - Ok(code) => code, - // A reader that went away stops the writer quietly, as SIGPIPE does. - Err(e) - if e.downcast_ref::().is_some_and(|io| { - io.kind() == std::io::ErrorKind::BrokenPipe - }) => + if let Some(body) = func_body { + state_proc.args = args; + state_proc.push_local_scope(); + state_proc.depth += 1; + state_proc.capture = true; + // Transfer stdin from child (CURRENT_PROCESS) so the + // function body's commands can read pipeline input. + CURRENT_PROCESS.with(|p| { + p.borrow_mut().transfer_fd(STDIN, &mut state_proc); + }); + let exit = match execute_command_line_inner( + os.clone(), + &mut state_proc, + &body, + ) + .await + { + Ok(code) => code, + Err(ControlFlow::Return(code)) => code, + Err(ControlFlow::Exit(code)) => code, + Err(_) => state_proc.last_exit, + }; + state_proc.pop_local_scope(); + CURRENT_PROCESS.with(|p| { + state_proc.transfer_fd(STDIN, &mut p.borrow_mut()); + }); + // Write captured output to the pipeline's stdout + let output = std::mem::take(&mut state_proc.captured_output); + if !output.is_empty() + && let Ok(mut w) = crate::io::stdout() { - 1 + let _ = w.write_all(output.as_bytes()).await; } - Err(e) => { - if let Ok(mut w) = crate::io::stderr() { - let _ = w - .write_all( - format!("strands-shell: {name}: {e}\n").as_bytes(), - ) - .await; - } - 1 + let errors = std::mem::take(&mut state_proc.captured_stderr); + if !errors.is_empty() + && let Ok(mut w) = crate::io::stderr() + { + let _ = w.write_all(errors.as_bytes()).await; } - }; - } - if let Some(body) = func_body { - state_proc.args = args; - state_proc.push_local_scope(); - state_proc.depth += 1; - state_proc.capture = true; - // Transfer stdin from child (CURRENT_PROCESS) so the - // function body's commands can read pipeline input. - CURRENT_PROCESS.with(|p| { - p.borrow_mut().transfer_fd(STDIN, &mut state_proc); - }); - let exit = match Box::pin(execute_command_line_inner( - os.clone(), - &mut state_proc, - &body, - )) - .await - { - Ok(code) => code, - Err(ControlFlow::Return(code)) => code, - Err(ControlFlow::Exit(code)) => code, - Err(_) => state_proc.last_exit, - }; - state_proc.pop_local_scope(); - CURRENT_PROCESS.with(|p| { - state_proc.transfer_fd(STDIN, &mut p.borrow_mut()); - }); - // Write captured output to the pipeline's stdout - let output = std::mem::take(&mut state_proc.captured_output); - if !output.is_empty() - && let Ok(mut w) = crate::io::stdout() - { - let _ = w.write_all(output.as_bytes()).await; - } - let errors = std::mem::take(&mut state_proc.captured_stderr); - if !errors.is_empty() - && let Ok(mut w) = crate::io::stderr() - { - let _ = w.write_all(errors.as_bytes()).await; + return exit; } - return exit; - } - unreachable!() - }), - )); - tasks.own(&handle); - handles.push((i, handle)); - } else { - // External commands - let handle = tokio::task::spawn_local(CURRENT_KERNEL.scope( - os.clone(), - run_stage(back, child, async move { - match commands::lookup(&name) { - Some(f) => match f(os.as_ref(), &args).await { - Ok(code) => code, - Err(e) => { + unreachable!() + }), + )); + tasks.own(&handle); + handles.push((i, handle)); + } else { + // External commands + let handle = tokio::task::spawn_local(CURRENT_KERNEL.scope( + os.clone(), + run_stage(back, child, async move { + match commands::lookup(&name) { + Some(f) => match f(os.as_ref(), &args).await { + Ok(code) => code, + Err(e) => { + if let Ok(mut w) = crate::io::stderr() { + let _ = w + .write_all( + format!("strands-shell: {name}: {e}\n").as_bytes(), + ) + .await; + } + 1 + } + }, + None => { + if let Some(fallback) = os.command_fallback() { + let context = CURRENT_PROCESS.with(|p| { + command_fallback::CommandContext::new( + &mut p.borrow_mut(), + name, + args, + ) + }); + return command_fallback::run(fallback, context).await; + } if let Ok(mut w) = crate::io::stderr() { let _ = w .write_all( - format!("strands-shell: {name}: {e}\n").as_bytes(), + format!("strands-shell: {name}: command not found\n") + .as_bytes(), ) .await; } - 1 - } - }, - None => { - if let Some(fallback) = os.command_fallback() { - let context = CURRENT_PROCESS.with(|p| { - command_fallback::CommandContext::new( - &mut p.borrow_mut(), - name, - args, - ) - }); - return command_fallback::run(fallback, context).await; + 127 } - if let Ok(mut w) = crate::io::stderr() { - let _ = w - .write_all( - format!("strands-shell: {name}: command not found\n") - .as_bytes(), - ) - .await; - } - 127 } - } - }), - )); - tasks.own(&handle); - handles.push((i, handle)); - } - } - - // Drain last stage stdout - let max_output = shell_proc.max_output; - let stdout_drain = if capture { - let handle = tokio::spawn(async move { - let mut reader = FdReader::from_receiver(last_rx); - os::read_to_string_lossy_limited(&mut reader, max_output) - .await - .unwrap_or_default() - }); - tasks.own(&handle); - Some(handle) - } else { - let handle = tokio::spawn(async move { - let mut reader = FdReader::from_receiver(last_rx); - #[cfg(not(target_arch = "wasm32"))] - { - let mut stdout = tokio::io::stdout(); - let _ = tokio::io::copy(&mut reader, &mut stdout).await; - } - #[cfg(target_arch = "wasm32")] - { - let mut buf = Vec::new(); - let _ = reader.read_to_end(&mut buf).await; - let _ = std::io::Write::write_all(&mut std::io::stdout(), &buf); + }), + )); + tasks.own(&handle); + handles.push((i, handle)); } - String::new() - }); - tasks.own(&handle); - Some(handle) - }; + } - // Drain stderr in stage order, or one task per stage with a fallback. - let stderr_groups = if tasks.concurrent_stderr() { - stderr_rxs.into_iter().map(|rx| vec![rx]).collect() - } else { - vec![stderr_rxs] - }; - let mut stderr_drains = Vec::with_capacity(len); - for group in stderr_groups { - let handle = tokio::spawn(async move { - let mut all = String::new(); - for rx in group { - let mut reader = FdReader::from_receiver(rx); - if capture { - all.push_str( - &os::read_to_string_lossy_limited(&mut reader, max_output) - .await - .unwrap_or_default(), - ); - continue; - } + // Drain last stage stdout + let max_output = shell_proc.max_output; + let stdout_drain = if capture { + let handle = tokio::spawn(async move { + let mut reader = FdReader::from_receiver(last_rx); + os::read_to_string_lossy_limited(&mut reader, max_output) + .await + .unwrap_or_default() + }); + tasks.own(&handle); + Some(handle) + } else { + let handle = tokio::spawn(async move { + let mut reader = FdReader::from_receiver(last_rx); #[cfg(not(target_arch = "wasm32"))] { - let mut stderr = tokio::io::stderr(); - let _ = tokio::io::copy(&mut reader, &mut stderr).await; + let mut stdout = tokio::io::stdout(); + let _ = tokio::io::copy(&mut reader, &mut stdout).await; } #[cfg(target_arch = "wasm32")] { let mut buf = Vec::new(); let _ = reader.read_to_end(&mut buf).await; - let _ = std::io::Write::write_all(&mut std::io::stderr(), &buf); + let _ = std::io::Write::write_all(&mut std::io::stdout(), &buf); } + String::new() + }); + tasks.own(&handle); + Some(handle) + }; + + // Drain stderr in stage order, or one task per stage with a fallback. + let stderr_groups = if tasks.concurrent_stderr() { + stderr_rxs.into_iter().map(|rx| vec![rx]).collect() + } else { + vec![stderr_rxs] + }; + let mut stderr_drains = Vec::with_capacity(len); + for group in stderr_groups { + let handle = tokio::spawn(async move { + let mut all = String::new(); + for rx in group { + let mut reader = FdReader::from_receiver(rx); + if capture { + all.push_str( + &os::read_to_string_lossy_limited(&mut reader, max_output) + .await + .unwrap_or_default(), + ); + continue; + } + #[cfg(not(target_arch = "wasm32"))] + { + let mut stderr = tokio::io::stderr(); + let _ = tokio::io::copy(&mut reader, &mut stderr).await; + } + #[cfg(target_arch = "wasm32")] + { + let mut buf = Vec::new(); + let _ = reader.read_to_end(&mut buf).await; + let _ = std::io::Write::write_all(&mut std::io::stderr(), &buf); + } + } + all + }); + tasks.own(&handle); + stderr_drains.push(handle); + } + + // Wait for all command tasks. + let mut stage_exits = vec![0; len]; + for (stage, handle) in handles { + if let Ok(exit) = handle.await { + stage_exits[stage] = + report_finished(os.as_ref(), shell_proc, requests[stage].take(), exit).await; } - all - }); - tasks.own(&handle); - stderr_drains.push(handle); - } - - // Wait for all command tasks. - let mut stage_exits = vec![0; len]; - for (stage, handle) in handles { - if let Ok(exit) = handle.await { - stage_exits[stage] = - report_finished(os.as_ref(), shell_proc, requests[stage].take(), exit).await; } - } - if redirects_stdin { - give_stdin(shell_proc, kept_stdin); - } else if let Ok(unread) = stdin_returned.try_recv() { - give_stdin(shell_proc, unread); - } + if redirects_stdin { + give_stdin(shell_proc, kept_stdin); + } else if let Ok(unread) = stdin_returned.try_recv() { + give_stdin(shell_proc, unread); + } - let stdout_str = if let Some(h) = stdout_drain { - h.await.unwrap_or_default() - } else { - String::new() - }; - let mut stderr_str = String::new(); - for handle in stderr_drains { - stderr_str.push_str(&handle.await.unwrap_or_default()); - } - if capture && !stderr_str.is_empty() { - shell_proc.captured_stderr.push_str(&stderr_str); - } - for (stage, writes) in early_host_writes.into_iter().enumerate() { - let failures = writes.settle().await; - for failure in &failures { - shell_proc.err_msg(&format!("strands-shell: {failure}")); + let stdout_str = if let Some(h) = stdout_drain { + h.await.unwrap_or_default() + } else { + String::new() + }; + let mut stderr_str = String::new(); + for handle in stderr_drains { + stderr_str.push_str(&handle.await.unwrap_or_default()); } - if stage_exits[stage] == 0 && !failures.is_empty() { - stage_exits[stage] = 1; + if capture && !stderr_str.is_empty() { + shell_proc.captured_stderr.push_str(&stderr_str); } - } - stage_exits[len - 1] = settle_host_writes(shell_proc, stage_exits[len - 1]).await; - let last_exit = if shell_proc.opt_pipefail { - stage_exits - .into_iter() - .rev() - .find(|&exit| exit != 0) - .unwrap_or(0) - } else { - stage_exits[len - 1] - }; + for (stage, writes) in early_host_writes.into_iter().enumerate() { + let failures = writes.settle().await; + for failure in &failures { + shell_proc.err_msg(&format!("strands-shell: {failure}")); + } + if stage_exits[stage] == 0 && !failures.is_empty() { + stage_exits[stage] = 1; + } + } + stage_exits[len - 1] = settle_host_writes(shell_proc, stage_exits[len - 1]).await; + let last_exit = if shell_proc.opt_pipefail { + stage_exits + .into_iter() + .rev() + .find(|&exit| exit != 0) + .unwrap_or(0) + } else { + stage_exits[len - 1] + }; - (last_exit, stdout_str) + (last_exit, stdout_str) + }) + .await } diff --git a/src/exec/stack.rs b/src/exec/stack.rs deleted file mode 100644 index 1a21fbd..0000000 --- a/src/exec/stack.rs +++ /dev/null @@ -1,39 +0,0 @@ -use std::future::Future; -use std::pin::Pin; -use std::task::{Context, Poll}; - -/// A recursive execution future with stack space for polling and cancellation. -struct Execution { - future: Option>>, -} - -pub(super) fn run(future: F) -> impl Future { - Execution { - future: Some(Box::pin(future)), - } -} - -impl Future for Execution { - type Output = F::Output; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - with_stack(|| self.future.as_mut().unwrap().as_mut().poll(cx)) - } -} - -impl Drop for Execution { - fn drop(&mut self) { - with_stack(|| drop(self.future.take())); - } -} - -fn with_stack(f: impl FnOnce() -> R) -> R { - #[cfg(not(target_arch = "wasm32"))] - { - stacker::maybe_grow(1024 * 1024, 4 * 1024 * 1024, f) - } - #[cfg(target_arch = "wasm32")] - { - f() - } -} diff --git a/src/lib.rs b/src/lib.rs index ec2bf31..f56789c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,10 +48,6 @@ //! } //! ``` //! -//! On native targets, recursive shell execution grows the stack as needed when -//! polling or cancelling a future. It stays on the caller's thread and retains -//! the configured recursion limit. WASM uses the stack supplied by its host. -//! //! ## Sandboxing with Bind Mounts //! //! The shell starts with an empty virtual filesystem. Use bind mounts to diff --git a/src/os.rs b/src/os.rs index 5b74b43..444f54a 100644 --- a/src/os.rs +++ b/src/os.rs @@ -1001,7 +1001,8 @@ pub async fn read_to_end_limited( return Ok(buf); } let mut buf = Vec::new(); - let mut tmp = [0u8; 8192]; + // The read buffer stays off the recursive executor's stack. + let mut tmp = vec![0u8; 8192]; loop { let n = tokio::io::AsyncReadExt::read(reader, &mut tmp).await?; if n == 0 { diff --git a/tests/command_check.rs b/tests/command_check.rs index 2d73566..8ec8d67 100644 --- a/tests/command_check.rs +++ b/tests/command_check.rs @@ -159,7 +159,7 @@ forwarding_kernel!(StackCheckingKernel { #[cfg(not(target_arch = "wasm32"))] #[test] -fn recursive_functions_leave_stack_for_command_checks() { +fn recursive_execution_leaves_stack_for_command_checks() { std::thread::Builder::new() .stack_size(2 * 1024 * 1024) .spawn(|| { @@ -167,6 +167,7 @@ fn recursive_functions_leave_stack_for_command_checks() { let (checked, _) = checking_shell(); let inner = checked.kernel().clone(); let mut shell = Shell::with_kernel(Arc::new(StackCheckingKernel { inner })); + shell.proc.max_depth = 64; let out = shell .run( "f() { if [ \"$1\" -gt 0 ]; then f \"$(($1 - 1))\"; \ @@ -177,6 +178,15 @@ fn recursive_functions_leave_stack_for_command_checks() { assert_eq!(out.stdout, "bottom\n"); assert!(out.stderr.is_empty(), "{}", out.stderr); + let out = shell + .run( + r#"code='if [ "$n" -gt 0 ]; then n=$((n - 1)); eval "$code"; else echo bottom; fi'; n=62; eval "$code""#, + ) + .await; + assert_eq!(out.status, 0, "{}", out.stderr); + assert_eq!(out.stdout, "bottom\n"); + assert!(out.stderr.is_empty(), "{}", out.stderr); + assert_eq!(shell.run("echo keep > /tmp/keep").await.status, 0); let out = shell .run( diff --git a/tests/shell_integration.rs b/tests/shell_integration.rs index 8b3bc87..5447de8 100644 --- a/tests/shell_integration.rs +++ b/tests/shell_integration.rs @@ -6019,12 +6019,12 @@ fn recursive_execution_can_be_cancelled_on_worker_stack() { .spawn(|| { let (rt, local) = rt(); rt.block_on(local.run_until(async { - let mut shell = Shell::builder().max_depth(0).build().unwrap(); + let mut shell = Shell::builder().build().unwrap(); let kernel = shell.kernel().clone(); let proc = kernel.new_process(); let mut run = Box::pin(shell.run( "f() { if [ \"$1\" -gt 0 ]; then f \"$(($1 - 1))\"; \ - else touch /tmp/ready; sleep 60; echo leaked > /tmp/leaked; fi; }; f 500", + else touch /tmp/ready; sleep 60; echo leaked > /tmp/leaked; fi; }; f 60", )); let ready = async { while !kernel.stat(&proc, "/tmp/ready").await.exists { @@ -6040,7 +6040,7 @@ fn recursive_execution_can_be_cancelled_on_worker_stack() { result.expect("recursive call did not reach the bottom"); } } - stacker::grow(64 * 1024, || drop(run)); + drop(run); assert_eq!(shell.run("test ! -e /tmp/leaked").await.status, 0); assert_eq!(shell.run("echo alive").await.stdout, "alive\n"); })); From 653a2795bb04b33be1bc199e169edc82b62287e8 Mon Sep 17 00:00:00 2001 From: Gautam Sirdeshmukh <54588697+gautamsirdeshmukh@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:43:17 -0500 Subject: [PATCH 3/4] fix: release setup frames before recursive execution --- src/exec.rs | 409 +++++++++++++++++++++++++++-------------- tests/command_check.rs | 9 + 2 files changed, 276 insertions(+), 142 deletions(-) diff --git a/src/exec.rs b/src/exec.rs index 7580d7e..d2e0a23 100644 --- a/src/exec.rs +++ b/src/exec.rs @@ -183,7 +183,16 @@ enum ExecTarget { Shebang(Vec), } -async fn resolve_executable( +#[inline(never)] +fn resolve_executable<'a>( + os: &'a dyn Kernel, + proc: &'a Process, + name: &'a str, +) -> Execution<'a, Option<(String, ExecTarget)>> { + Box::pin(resolve_executable_inner(os, proc, name)) +} + +async fn resolve_executable_inner( os: &dyn Kernel, proc: &Process, name: &str, @@ -2985,10 +2994,8 @@ async fn run_special_redirected( return Err(ControlFlow::Exit(2)); } let body_os = os.clone(); - box_future(|| { - run_redirected(os, proc, &redirects, move |proc| { - Box::pin(run_special(body_os, proc, special, args)) - }) + run_redirected(os, proc, &redirects, move |proc| { + Box::pin(run_special(body_os, proc, special, args)) }) .await .unwrap_or(Ok(1)) @@ -2997,7 +3004,20 @@ async fn run_special_redirected( /// Run `body` in the current shell with expanded redirections applied to its I/O. /// /// Returns `None`, without running `body`, when a redirection fails. -async fn run_redirected( +#[inline(never)] +fn run_redirected<'a, F>( + os: Arc, + proc: &'a mut Process, + redirects: &'a [(Redirect, String)], + body: F, +) -> Execution<'a, Option>> +where + F: for<'p> FnOnce(&'p mut Process) -> Execution<'p, Result> + 'a, +{ + Box::pin(run_redirected_inner(os, proc, redirects, body)) +} + +async fn run_redirected_inner( os: Arc, proc: &mut Process, redirects: &[(Redirect, String)], @@ -3469,29 +3489,40 @@ fn run_stage( }) } +async fn run_pipeline( + os: Arc, + shell_proc: &mut Process, + pipeline: &[parser::Command], + capture: bool, +) -> (i32, String) { + shell_proc.deadline_stop = false; + let mut tasks = ForegroundTasks::new(os.as_ref(), shell_proc); + let pipeline = match prepare_pipeline(os.clone(), shell_proc, pipeline).await { + Ok(pipeline) => pipeline, + Err(status) => return (status, String::new()), + }; + run_expanded_pipeline(os, shell_proc, pipeline, &mut tasks, capture).await +} + #[inline(never)] -fn run_pipeline<'a>( +fn prepare_pipeline<'a>( os: Arc, proc: &'a mut Process, pipeline: &'a [parser::Command], - capture: bool, -) -> Execution<'a, (i32, String)> { - Box::pin(run_pipeline_inner(os, proc, pipeline, capture)) +) -> Execution<'a, Result> { + Box::pin(prepare_pipeline_inner(os, proc, pipeline)) } -async fn run_pipeline_inner( +async fn prepare_pipeline_inner( os: Arc, shell_proc: &mut Process, pipeline: &[parser::Command], - capture: bool, -) -> (i32, String) { - shell_proc.deadline_stop = false; +) -> Result { let len = pipeline.len(); - let mut tasks = ForegroundTasks::new(os.as_ref(), shell_proc); if shell_proc.max_pipeline > 0 && len > shell_proc.max_pipeline { shell_proc.err_msg("strands-shell: pipeline too long"); - return (1, String::new()); + return Err(1); } // Expand all args/env/redirects up front @@ -3540,19 +3571,19 @@ async fn run_pipeline_inner( shell_proc.err_msg(msg); } shell_proc.deadline_stop = true; - return (1, String::new()); + return Err(1); } // Abort if nounset error occurred during expansion if shell_proc.nounset_error { shell_proc.last_exit = 2; - return (2, String::new()); + return Err(2); } // A command substitution can run past the deadline, so do not run the command it fed. if let Some(msg) = shell_proc.check_limits() { shell_proc.err_msg(msg); - return (1, String::new()); + return Err(1); } // xtrace: print expanded commands to stderr @@ -3573,7 +3604,7 @@ async fn run_pipeline_inner( shell_proc.set_env(k, v); } // With no command name, the status is that of the last command substitution. - return (shell_proc.last_subst_status.unwrap_or(0), String::new()); + return Err(shell_proc.last_subst_status.unwrap_or(0)); } // Resolve executables: for each stage, if the command is not a builtin, @@ -3590,9 +3621,7 @@ async fn run_pipeline_inner( { continue; } - if let Some((_path, target)) = - box_future(|| resolve_executable(os.as_ref(), shell_proc, cmd)).await - { + if let Some((_path, target)) = resolve_executable(os.as_ref(), shell_proc, cmd).await { match target { ExecTarget::Multicall(name) => { args[0] = name; @@ -3600,7 +3629,7 @@ async fn run_pipeline_inner( ExecTarget::Shebang(interp) => { // Check if the interpreter resolves to a multicall binary if let Some((_, ExecTarget::Multicall(mcname))) = - box_future(|| resolve_executable(os.as_ref(), shell_proc, &interp[0])).await + resolve_executable(os.as_ref(), shell_proc, &interp[0]).await { // Multicall: rewrite to [orig_args...] let script_path = interp.last().unwrap().clone(); @@ -3643,7 +3672,7 @@ async fn run_pipeline_inner( let request = CommandRequest::new(program, args, &shell_proc.cwd); if let Err(e) = os.check_fallback(&request).await { shell_proc.err_msg(&format!("strands-shell: {program}: {e}")); - return (126, String::new()); + return Err(126); } } requests.push(None); @@ -3652,7 +3681,7 @@ async fn run_pipeline_inner( let request = CommandRequest::new(program, args, &shell_proc.cwd); if let Err(e) = os.check_command(&request).await { shell_proc.err_msg(&format!("strands-shell: {program}: {e}")); - return (126, String::new()); + return Err(126); } requests.push(Some(CheckedCommand { os: os.clone(), @@ -3660,6 +3689,182 @@ async fn run_pipeline_inner( })); } + Ok(ExpandedPipeline { + args: expanded_args, + env: expanded_env, + redirects: expanded_redirects, + requests, + }) +} + +struct ExpandedPipeline { + args: Vec>, + env: Vec>, + redirects: Vec>, + requests: Vec>, +} + +enum OutputDrain { + Capture(tokio::task::JoinHandle), + Copy(tokio::task::JoinHandle<()>), +} + +struct BuiltinIo { + process: Option, + stdout: OutputDrain, + stderr: OutputDrain, + redirects_stdin: bool, + kept_stdin: Option, +} + +#[inline(never)] +fn prepare_builtin_io<'a>( + os: Arc, + proc: &'a mut Process, + redirects: &'a [(Redirect, String)], + tasks: &'a mut ForegroundTasks, + capture: bool, +) -> Execution<'a, Option>> { + Box::pin(prepare_builtin_io_inner( + os, proc, redirects, tasks, capture, + )) +} + +async fn prepare_builtin_io_inner( + os: Arc, + shell_proc: &mut Process, + redirects: &[(Redirect, String)], + tasks: &mut ForegroundTasks, + capture: bool, +) -> Option> { + let mut io_proc = shell_proc.fork(); + // Inherit stdin from shell_proc if present (e.g. CompoundPipeline). A stdin + // redirect sets the caller's stdin aside until the builtin ends. + shell_proc.transfer_fd(STDIN, &mut io_proc); + let redirects_stdin = replaces_stdin(redirects); + let kept_stdin = redirects_stdin.then(|| take_stdin(&mut io_proc)).flatten(); + let (out_tx, out_rx) = os::pipe(64); + let (err_tx, err_rx) = os::pipe(64); + // A writer the caller installed takes the place of the local pipe. + if shell_proc.inherit_channel_writer(STDOUT, &mut io_proc) { + drop(out_tx); + } else { + io_proc.set_channel_writer(STDOUT, out_tx); + } + let caller_stderr = shell_proc.inherit_channel_writer(STDERR, &mut io_proc); + if !caller_stderr { + io_proc.set_channel_writer(STDERR, err_tx.clone()); + } + shell_proc.set_err_tx(err_tx); + + // Apply redirects to the io_proc so builtins see them + if let Err(e) = apply_redirects(&os, &mut io_proc, redirects).await { + shell_proc.clear_err_tx(); + if caller_stderr && let Some(tx) = shell_proc.stderr_channel() { + shell_proc.set_err_tx(tx); + } + shell_proc.err_msg(&format!("strands-shell: {e}")); + shell_proc.clear_err_tx(); + give_stdin(shell_proc, kept_stdin); + return None; + } + // Builtin diagnostics follow the command's own stderr redirection. + if let Some(tx) = io_proc.stderr_channel() { + shell_proc.set_err_tx(tx); + } + + // Drain stdout/stderr concurrently with the builtin to avoid + // deadlock when the builtin produces more output than the channel + // capacity. + let max_output = shell_proc.max_output; + let stdout_drain = if capture { + OutputDrain::Capture(tokio::task::spawn_local(async move { + let mut reader = FdReader::from_receiver(out_rx); + os::read_to_string_lossy_limited(&mut reader, max_output) + .await + .unwrap_or_default() + })) + } else { + OutputDrain::Copy(tokio::task::spawn_local(async move { + let mut reader = FdReader::from_receiver(out_rx); + #[cfg(not(target_arch = "wasm32"))] + { + let _ = tokio::io::copy(&mut reader, &mut tokio::io::stdout()).await; + } + #[cfg(target_arch = "wasm32")] + { + let mut buf = Vec::new(); + let _ = reader.read_to_end(&mut buf).await; + let _ = std::io::Write::write_all(&mut std::io::stdout(), &buf); + } + })) + }; + let stderr_drain = if capture { + OutputDrain::Capture(tokio::task::spawn_local(async move { + let mut reader = FdReader::from_receiver(err_rx); + os::read_to_string_lossy_limited(&mut reader, max_output) + .await + .unwrap_or_default() + })) + } else { + OutputDrain::Copy(tokio::task::spawn_local(async move { + let mut reader = FdReader::from_receiver(err_rx); + #[cfg(not(target_arch = "wasm32"))] + { + let _ = tokio::io::copy(&mut reader, &mut tokio::io::stderr()).await; + } + #[cfg(target_arch = "wasm32")] + { + let mut buf = Vec::new(); + let _ = reader.read_to_end(&mut buf).await; + let _ = std::io::Write::write_all(&mut std::io::stderr(), &buf); + } + })) + }; + for drain in [&stdout_drain, &stderr_drain] { + match drain { + OutputDrain::Capture(h) => tasks.own(h), + OutputDrain::Copy(h) => tasks.own(h), + } + } + + Some(Box::new(BuiltinIo { + process: Some(io_proc), + stdout: stdout_drain, + stderr: stderr_drain, + redirects_stdin, + kept_stdin, + })) +} + +#[inline(never)] +fn run_expanded_pipeline<'a>( + os: Arc, + proc: &'a mut Process, + pipeline: ExpandedPipeline, + tasks: &'a mut ForegroundTasks, + capture: bool, +) -> Execution<'a, (i32, String)> { + Box::pin(run_expanded_pipeline_inner( + os, proc, pipeline, tasks, capture, + )) +} + +async fn run_expanded_pipeline_inner( + os: Arc, + shell_proc: &mut Process, + pipeline: ExpandedPipeline, + tasks: &mut ForegroundTasks, + capture: bool, +) -> (i32, String) { + let ExpandedPipeline { + args: expanded_args, + env: expanded_env, + redirects: expanded_redirects, + mut requests, + } = pipeline; + let len = expanded_args.len(); + // Single command: check for builtins if len == 1 { if let Some(f) = builtins::lookup(&expanded_args[0][0]) { @@ -3667,111 +3872,32 @@ async fn run_pipeline_inner( let name = expanded_args[0][0].clone(); let args: Vec = expanded_args[0][1..].to_vec(); - let mut io_proc = shell_proc.fork(); - // Inherit stdin from shell_proc if present (e.g. CompoundPipeline). A stdin - // redirect sets the caller's stdin aside until the builtin ends. - shell_proc.transfer_fd(STDIN, &mut io_proc); - let redirects_stdin = replaces_stdin(&expanded_redirects[0]); - let mut kept_stdin = redirects_stdin.then(|| take_stdin(&mut io_proc)).flatten(); - let (out_tx, out_rx) = os::pipe(64); - let (err_tx, err_rx) = os::pipe(64); - // A writer the caller installed takes the place of the local pipe. - if shell_proc.inherit_channel_writer(STDOUT, &mut io_proc) { - drop(out_tx); - } else { - io_proc.set_channel_writer(STDOUT, out_tx); - } - let caller_stderr = shell_proc.inherit_channel_writer(STDERR, &mut io_proc); - if !caller_stderr { - io_proc.set_channel_writer(STDERR, err_tx.clone()); - } - shell_proc.set_err_tx(err_tx); - - // Apply redirects to the io_proc so builtins see them - if let Err(e) = apply_redirects(&os, &mut io_proc, &expanded_redirects[0]).await { - shell_proc.clear_err_tx(); - if caller_stderr && let Some(tx) = shell_proc.stderr_channel() { - shell_proc.set_err_tx(tx); - } - shell_proc.err_msg(&format!("strands-shell: {e}")); - shell_proc.clear_err_tx(); - give_stdin(shell_proc, kept_stdin); + let Some(mut io) = prepare_builtin_io( + os.clone(), + shell_proc, + &expanded_redirects[0], + tasks, + capture, + ) + .await + else { return (1, String::new()); - } - // Builtin diagnostics follow the command's own stderr redirection. - if let Some(tx) = io_proc.stderr_channel() { - shell_proc.set_err_tx(tx); - } - - // Drain stdout/stderr concurrently with the builtin to avoid - // deadlock when the builtin produces more output than the channel - // capacity. - let max_output = shell_proc.max_output; - enum Drain { - Capture(tokio::task::JoinHandle), - Copy(tokio::task::JoinHandle<()>), - } - let stdout_drain = if capture { - Drain::Capture(tokio::task::spawn_local(async move { - let mut reader = FdReader::from_receiver(out_rx); - os::read_to_string_lossy_limited(&mut reader, max_output) - .await - .unwrap_or_default() - })) - } else { - Drain::Copy(tokio::task::spawn_local(async move { - let mut reader = FdReader::from_receiver(out_rx); - #[cfg(not(target_arch = "wasm32"))] - { - let _ = tokio::io::copy(&mut reader, &mut tokio::io::stdout()).await; - } - #[cfg(target_arch = "wasm32")] - { - let mut buf = Vec::new(); - let _ = reader.read_to_end(&mut buf).await; - let _ = std::io::Write::write_all(&mut std::io::stdout(), &buf); - } - })) - }; - let stderr_drain = if capture { - Drain::Capture(tokio::task::spawn_local(async move { - let mut reader = FdReader::from_receiver(err_rx); - os::read_to_string_lossy_limited(&mut reader, max_output) - .await - .unwrap_or_default() - })) - } else { - Drain::Copy(tokio::task::spawn_local(async move { - let mut reader = FdReader::from_receiver(err_rx); - #[cfg(not(target_arch = "wasm32"))] - { - let _ = tokio::io::copy(&mut reader, &mut tokio::io::stderr()).await; - } - #[cfg(target_arch = "wasm32")] - { - let mut buf = Vec::new(); - let _ = reader.read_to_end(&mut buf).await; - let _ = std::io::Write::write_all(&mut std::io::stderr(), &buf); - } - })) }; - for drain in [&stdout_drain, &stderr_drain] { - match drain { - Drain::Capture(h) => tasks.own(h), - Drain::Copy(h) => tasks.own(h), - } - } - let io_cell = RefCell::new(io_proc); - let result = CURRENT_KERNEL - .scope( + let result = box_future(|| { + let io_cell = RefCell::new( + io.process + .take() + .expect("builtin process has not been consumed"), + ); + CURRENT_KERNEL.scope( os.clone(), CURRENT_PROCESS.scope(io_cell, async { let r = f(os.as_ref(), shell_proc, &args).await; // Transfer stdin back so it survives across loop iterations, or give // back the caller's own when a redirect replaced it. - if redirects_stdin { - give_stdin(shell_proc, kept_stdin.take()); + if io.redirects_stdin { + give_stdin(shell_proc, io.kept_stdin.take()); } else { CURRENT_PROCESS .with(|p| p.borrow_mut().transfer_fd(STDIN, shell_proc)); @@ -3779,22 +3905,23 @@ async fn run_pipeline_inner( r }), ) - .await; + }) + .await; if let Err(e) = &result { shell_proc.err_msg(&format!("strands-shell: {name}: {e}")); } shell_proc.clear_err_tx(); - let stdout_str = match stdout_drain { - Drain::Capture(h) => h.await.unwrap_or_default(), - Drain::Copy(h) => { + let stdout_str = match io.stdout { + OutputDrain::Capture(h) => h.await.unwrap_or_default(), + OutputDrain::Copy(h) => { let _ = h.await; String::new() } }; - let stderr_str = match stderr_drain { - Drain::Capture(h) => h.await.unwrap_or_default(), - Drain::Copy(h) => { + let stderr_str = match io.stderr { + OutputDrain::Capture(h) => h.await.unwrap_or_default(), + OutputDrain::Copy(h) => { let _ = h.await; String::new() } @@ -3821,18 +3948,16 @@ async fn run_pipeline_inner( Some(execute_command_line_inner(os.clone(), shell_proc, &func_body).await) } else { let body_os = os.clone(); - box_future(|| { - run_redirected( - os.clone(), - shell_proc, - &expanded_redirects[0], - move |proc| { - Box::pin(async move { - execute_command_line_inner(body_os, proc, &func_body).await - }) - }, - ) - }) + run_redirected( + os.clone(), + shell_proc, + &expanded_redirects[0], + move |proc| { + Box::pin(async move { + execute_command_line_inner(body_os, proc, &func_body).await + }) + }, + ) .await }; // A failed redirect leaves the function's check unreported, so it is dropped. diff --git a/tests/command_check.rs b/tests/command_check.rs index 8ec8d67..e36372e 100644 --- a/tests/command_check.rs +++ b/tests/command_check.rs @@ -187,6 +187,15 @@ fn recursive_execution_leaves_stack_for_command_checks() { assert_eq!(out.stdout, "bottom\n"); assert!(out.stderr.is_empty(), "{}", out.stderr); + let out = shell + .run( + r#"echo input > /tmp/input; f() { if [ "$1" -gt 0 ]; then find /tmp/input -exec f "$(($1 - 1))" \; ; else echo bottom; fi; }; f 31"#, + ) + .await; + assert_eq!(out.status, 0, "{}", out.stderr); + assert_eq!(out.stdout, "bottom\n"); + assert!(out.stderr.is_empty(), "{}", out.stderr); + assert_eq!(shell.run("echo keep > /tmp/keep").await.status, 0); let out = shell .run( From 45f34112ac791eafdb5f78b9e1dfd8d9758894bb Mon Sep 17 00:00:00 2001 From: Gautam Sirdeshmukh <54588697+gautamsirdeshmukh@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:59:03 -0500 Subject: [PATCH 4/4] fix: construct control flow futures outside recursive frames --- src/exec.rs | 12 +++++++----- tests/command_check.rs | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/exec.rs b/src/exec.rs index d2e0a23..d42dc4a 100644 --- a/src/exec.rs +++ b/src/exec.rs @@ -2450,15 +2450,17 @@ async fn execute_item_inner( Item::If { branches, else_body, - } => Box::pin(execute_if(os, proc, branches, else_body.as_ref())).await, + } => box_future(|| execute_if(os, proc, branches, else_body.as_ref())).await, Item::While { condition, body } => { - Box::pin(execute_while(os, proc, condition, body, false)).await + box_future(|| execute_while(os, proc, condition, body, false)).await } Item::Until { condition, body } => { - Box::pin(execute_while(os, proc, condition, body, true)).await + box_future(|| execute_while(os, proc, condition, body, true)).await } - Item::For { var, words, body } => Box::pin(execute_for(os, proc, var, words, body)).await, - Item::Case { word, arms } => Box::pin(execute_case(os, proc, word, arms)).await, + Item::For { var, words, body } => { + box_future(|| execute_for(os, proc, var, words, body)).await + } + Item::Case { word, arms } => box_future(|| execute_case(os, proc, word, arms)).await, Item::Function { name, body } => { proc.set_function(name, body.clone()); Ok(0) diff --git a/tests/command_check.rs b/tests/command_check.rs index e36372e..50d37de 100644 --- a/tests/command_check.rs +++ b/tests/command_check.rs @@ -175,7 +175,7 @@ fn recursive_execution_leaves_stack_for_command_checks() { ) .await; assert_eq!(out.status, 0, "{}", out.stderr); - assert_eq!(out.stdout, "bottom\n"); + assert_eq!(out.stdout, "bottom\n", "{}", out.stderr); assert!(out.stderr.is_empty(), "{}", out.stderr); let out = shell @@ -184,7 +184,7 @@ fn recursive_execution_leaves_stack_for_command_checks() { ) .await; assert_eq!(out.status, 0, "{}", out.stderr); - assert_eq!(out.stdout, "bottom\n"); + assert_eq!(out.stdout, "bottom\n", "{}", out.stderr); assert!(out.stderr.is_empty(), "{}", out.stderr); let out = shell @@ -193,7 +193,7 @@ fn recursive_execution_leaves_stack_for_command_checks() { ) .await; assert_eq!(out.status, 0, "{}", out.stderr); - assert_eq!(out.stdout, "bottom\n"); + assert_eq!(out.stdout, "bottom\n", "{}", out.stderr); assert!(out.stderr.is_empty(), "{}", out.stderr); assert_eq!(shell.run("echo keep > /tmp/keep").await.status, 0);