Skip to content

fix: let a command substitution read the caller's stdin - #266

Merged
ferdingler merged 3 commits into
strands-agents:mainfrom
ferdingler:fix/subst-subshell-stdin
Nov 13, 2026
Merged

ferdingler merged 3 commits into
strands-agents:mainfrom
ferdingler:fix/subst-subshell-stdin

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Description

A command substitution never sees the stdin of the shell or pipeline around it. So printf hi | { x=$(cat); echo "[$x]"; } prints [] where bash prints [hi], and a script that starts with input=$(cat) gets an empty string even when you pipe JSON into it. That's how Claude Code hook scripts read their event, so they all break. A plain subshell like printf hi | ( cat ) already worked, but a substitution inside one didn't.

Now a substitution reads the same stdin bash would give it, and whatever it leaves unread is still there for the next command. Input is read once and never duplicated: two $(cat) in a row get everything and then nothing, like bash. While fixing this I also stopped a < file redirect on one command, and a compound piped into a pipeline, from throwing away the caller's stdin, because otherwise (cat </dev/null); cat loses the input that bash keeps for the second cat. This also fixes while read loops that stopped after one pass when the body ran a command that doesn't read stdin. It builds on #224, which gave sh script its piped stdin.

Related Issues

Fixes #249
Part of #207

Documentation PR

Type of Change

Bug fix

Testing

I checked every expected output against bash. The new tests cover substitutions, subshells, nested forms, a script run through sh, input fed to the shell from outside, and input read once. There's also a test for the while read loop. They all fail on main and pass here. I also disabled each piece of the fix one at a time and a test caught each one. The full Rust suite passes, and clippy has the same warnings as main. One recursion test runs close to the default thread stack size, so I checked that it has the same headroom as on main. I didn't run the Python or Node suites because the bindings are unchanged. A read after another command already used up stdin still prints an error that bash doesn't. That happens on main too, so I left it out.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

A command substitution ran in a process with no stdin, so `input=$(cat)` read
nothing, while bash gives it the caller's stdin. A script that reads its piped
input this way saw an empty string.

The substitution now borrows the caller's stdin while it runs and gives back
what it did not read. A pipeline's first stage also gives back stdin it did not
read, and a stdin redirect on a command or a compound piped into a pipeline
sets the caller's stdin aside instead of dropping it, so the next reader gets
the rest of the input exactly once.
@ferdingler
ferdingler requested a review from a team as a code owner November 13, 2026 03:07
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent please review

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve. The fix works and also closes #249. There's one 🟡 regression I'd fix before merge (see the inline comment).

Substitutions, subshells, sh script with input=$(cat), and (cat </dev/null); cat all match bash now. The #249 repro (while read f; do ls / >/dev/null; …; done < list) also runs every iteration, where main stops after one pass. Consider adding Fixes #249 and one test for it.

The new problem: a $(…) in a later pipeline stage now reads the caller's stdin, which silently feeds the wrong command.

✅ What I verified · Questions · Appendix (3)

Verified at c577f2f, against main at 46eb349 and GNU bash:

  • cargo test --release --test shell_integration gives 1938 passed, 0 failed. It's the same with the suggested fix applied.
  • I compared PR, main and bash side by side on the PR's claims, #249, and about 40 edge cases (if/until, $(( )), ${x:-$(cat)}, local/export, eval, ., sh -c, redirect targets, { …; } < f, pipefail, set -e, nested $( (cat) ), ~600 KB of input). None hung, and everything outside the items below matches bash.
  • Full log: strands-agents/shell/pr/266/repro-pr-vs-main-vs-bash.txt in the artifacts portal.

Questions (non-blocking)

  • When a substitution drains stdin, later readers now print read: bad fd 0. For example, printf 'a\nb\n' | { while read l; do x=$(cat); done; echo end; } prints end plus read: bad fd 0, while main and bash print only end. You called out the root cause as pre-existing, since an fd that hits EOF ends up missing rather than at EOF. This PR makes it much easier to hit, though, and that stderr noise goes straight to the model. Would handing back an EOF reader instead of None (in run_stage and capture_output) be in scope here, or should it be a follow-up issue?

Appendix (non-blocking)

  • ⚪ An empty first stage still drops stdin. printf 'a\nb\n' | { $x | cat; echo --; cat; } prints --, while bash prints -- a b. The continue at src/exec.rs:3727 drops child without sending through stdin_back. Main behaves the same, so this is a gap in the fix, not a regression.
  • ⚪ printf v | cat <&0 fails with 0: cannot duplicate this fd. This is pre-existing and identical on main. I didn't file it because it's unrelated to this change.
  • ⚪ An EXIT trap can't read the caller's stdin (trap 'read l' EXIT). Pre-existing, and contrived enough that I dropped it.

Reading order: capture_output (1752) → the run_pipeline hand-back (replaces_stdin/run_stage, 3250–3290, then 3663–3975) → the execute_item compound tail (2326–2370).

Comment thread src/exec.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Changes are in aa3559c:

  • Later-stage substitution: fixed, see the inline reply.
  • Empty first stage: fixed too. printf 'a\nb\n' | { $x | cat; echo --; cat; } now prints --, a, b like bash, and there's a case for it in the tests.
  • [BUG] A command in a while read loop body takes the loop's stdin, so the loop stops after one pass #249: added Fixes #249 and a test for the three loop bodies from the issue (ls, echo | cat, sh script).
  • read: bad fd 0 after stdin is used up: I'll do this as a separate follow-up. Main already shows the same message for cat; read, and the real fix is for a drained stdin to look like EOF to every reader, not only after a substitution. That changes more than this PR should.
  • cat <&0 and trap 'read l' EXIT: not related to this change, so I left them.

@strandly-the-agent please review

Merges strands-agents#257 and strands-agents#264. The deadline stop after a compound piped into a
pipeline now runs after the caller's stdin is given back. Stdin set aside
around a substitution or a pipeline stage keeps the outside-input marker, so
Lua still reads it as empty.
@ferdingler

Copy link
Copy Markdown
Collaborator Author

I merged main into the branch in 6131858, because #257 and #264 touched the same code.

  • fix: stop a command when its substitution uses up the deadline #257: in both compound-pipeline paths, the caller's stdin is given back first and then the deadline check runs. So an early stop no longer drops the input.
  • feat: let lua skip stdin an embedder keeps open #264: the stdin that gets set aside around a substitution or a pipeline stage now keeps its outside-input marker. Before this, Lua would wait on an embedder's open stdin after something like x=$(true). There are new Lua tests for it: $(cat) reads marked input, and Lua still sees it as empty inside a substitution, a subshell, and after a pipeline or a redirected command.

@strandly-the-agent please review

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 6131858. All of my earlier findings are resolved, and the merge with #257 and #264 holds up.

  • ✅ Later-stage substitution: fixed. echo foo | echo "[$(cat)]"; cat leaves x y for the trailing cat.
  • ✅ Empty first stage: fixed. $x | cat; echo --; cat prints -- a b, as bash does.
  • ✅ #249: linked with Fixes #249. The tests cover the ls, echo | cat and sh script loop bodies.
  • ⏭️ read: bad fd 0 once stdin is drained: deferred to your follow-up, which makes sense to me.
  • ✅ Merge: with a 200 ms deadline, a stop inside { sleep 1; } | cat or x=$({ sleep 1; } | cat) still hands the caller's input back, and the next cat reads a b. With an embedder stdin left open, lua -e reads [] without hanging after x=$(true), $x | cat, { true; } | cat and read l </dev/null.

I found no new issues.

What I verified
  • Ran cargo test --release --test shell_integration --test lua_integration at 6131858: 1963 + 233 passed, 0 failed.
  • I compared the PR, main and bash on the original repros. The full log is strands-agents/shell/pr/266/repro-6131858.txt in the artifacts portal.
  • The deadline and Lua checks ran through a throwaway Shell::builder() test with a set_channel_reader / set_outside_reader stdin. I didn't commit it.
  • I reviewed the merge resolution in git show --cc 6131858. In run_capturing_status and execute_item, give_stdin now runs before stop_at_deadline. In run_pipeline, the #257 deadline check comes after give_stdin(shell_proc, held_stdin). take_fd/put_fd keep the outside-input marker.

@ferdingler
ferdingler merged commit 8a203dc into strands-agents:main Nov 13, 2026
31 checks passed
@ferdingler
ferdingler deleted the fix/subst-subshell-stdin branch November 13, 2026 04:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants