Skip to content

[BUG] A command in a while read loop body takes the loop's stdin, so the loop stops after one pass #249

Closed

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell (tested on main @ a064834)
  • I have checked the documentation and this is not expected behavior
  • I have searched issues and there are no duplicates (Agent-usability gaps: missing commands, flags, and shell syntax #207 lists the separate cmd | while … parse failure; it notes while read …; done < file works, which only holds when the body uses builtins/functions)

Binding

Rust

Strands Shell Version

main @ a064834 (also reproduces on PR #224 head 1775f8f)

Language Runtime Version

Rust 1.99.1

Operating System

Linux 6.1 (aarch64)

Installation Method

built from source

Steps to Reproduce

printf 'a\nb\nc\n' > /tmp/list
while read f; do ls / >/dev/null; echo "l:$f"; done < /tmp/list

Same result with echo "$f" | cat, sh script.sh "$f" (a script that does not read stdin), and with the input piped in (printf … | while read f; do …; done).

Expected Behavior

bash runs every iteration:

l:a
l:b
l:c

Actual Behavior

l:a
strands-shell: read: bad fd 0

The loop runs once, then read has no stdin. Bodies made only of single builtins or function calls work (e(){ echo "f:$1"; }; while read f; do e "$f"; done < /tmp/list runs all three).

Additional Context

while read f; do <cmd> "$f"; done < list is a very common agent idiom, and it fails here without an error from the body itself.

Possible Solution

The likely cause is src/exec.rs:3483 in run_pipeline: shell_proc.transfer_fd(STDIN, &mut procs[0]) moves the enclosing compound's stdin into stage 0, and the stage drops it when it finishes, whether or not it read anything. Giving stage 0 a shared handle (or handing the fd back after the pipeline) would keep unread input for the next loop iteration, like a real inherited fd.

Related Issues

#207, #224 (scripts now read their stage's stdin; the same fd lifetime applies)

Activity

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

    No labels
    No labels

    Type

    No type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions