Skip to content

[BUG] With a stdout writer installed, a nested group that redirects only stderr writes ahead of earlier output ({ echo A; { echo B; } 2>&1; echo C; } 2>/dev/null gives B A C) #390

Open

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell (built from main at dbb6189)
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Binding

Rust

Strands Shell Version

0.0.0 (built from source, main dbb6189)

Language Runtime Version

Rust 1.100.0

Operating System

Debian 12 (aarch64)

Installation Method

built from source

Steps to Reproduce

Install a stdout writer on the shell's process, run a group that redirects only stderr and contains another such group, then read what the writer received:

use strands_shell::Shell;
use strands_shell::os::{STDOUT, pipe};

#[test]
fn writer_keeps_order_around_a_nested_stderr_group() {
    let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
    let local = tokio::task::LocalSet::new();
    rt.block_on(local.run_until(async {
        let mut shell = Shell::builder().build().unwrap();
        let (tx, mut rx) = pipe(1024);
        shell.proc.set_channel_writer(STDOUT, tx);
        shell.run("{ echo A; { echo B; } 2>&1; echo C; } 2>/dev/null").await;
        let mut got = String::new();
        while let Ok(chunk) = rx.try_recv() {
            got.push_str(&String::from_utf8_lossy(&chunk));
        }
        assert_eq!(got, "A\nB\nC\n");
    }));
}

Expected Behavior

The writer receives A, B, C in that order, as bash prints them.

Actual Behavior

The writer receives B\nA\nC\n. Without a writer, Shell::run returns A\nB\nC\n.

Additional Context

Found while reviewing #384; it is not caused by that PR. On main the output of a compound pipeline in the same position happens to keep its order ({ { echo a; } | cat; { echo n; } 2>/dev/null; } 2>/dev/null gives a, n). With #384 at f6a79a4 that tail is captured like the rest of the group, so it joins this bug and the writer gets n, a.

In src/exec.rs at dbb6189, the outer group buffers its output and hands it to the writer when it ends. The arm of run_capturing_status that runs a group with only non-stdin redirects (lines 2269-2273) turns proc.capture on but leaves fd 1 in place. Process::write_stdout (src/os.rs:877) prefers the fd 1 channel over captured_output, so the inner group writes straight to the writer, ahead of the buffered A.

I did not test the Python, Node.js or WASM bindings; I found no place where they install a channel writer.

Possible Solution

Set stdout aside in that arm, as capture_pipeline_checked does. With this change on top of #384 (f6a79a4), the script above gives A, B, C, the two compound-pipeline scripts give a, n, and cargo test --workspace --all-targets gives 2736 passed, 0 failed. I did not run it on main alone.

                         let saved_capture = std::mem::replace(&mut proc.capture, true);
                         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_item(os.clone(), proc, &cr)).await;
                         proc.capture = saved_capture;
+                        proc.close(STDOUT);
+                        saved_stdout.transfer_fd(STDOUT, proc);
                         let out = std::mem::replace(&mut proc.captured_output, saved_output);

Related Issues

#384, #379 (a redirect ignored when a writer is installed; a different path), #245

Activity

  1. added
    bug
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions