Repository navigation
fix: let a command substitution read the caller's stdin - #266
Conversation
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.
|
@strandly-the-agent please review |
strandly-the-agent
left a comment
There was a problem hiding this comment.
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_integrationgives 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.txtin 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; }printsendplusread: bad fd 0, while main and bash print onlyend. 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 ofNone(inrun_stageandcapture_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. Thecontinueatsrc/exec.rs:3727dropschildwithout sending throughstdin_back. Main behaves the same, so this is a gap in the fix, not a regression. - ⚪
printf v | cat <&0fails with0: 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
EXITtrap 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).
… empty first stage
|
Thanks for the review. Changes are in aa3559c:
@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.
|
I merged main into the branch in 6131858, because #257 and #264 touched the same code.
@strandly-the-agent please review |
strandly-the-agent
left a comment
There was a problem hiding this comment.
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)]"; catleavesx yfor the trailingcat. - ✅ Empty first stage: fixed.
$x | cat; echo --; catprints-- a b, as bash does. - ✅ #249: linked with
Fixes #249. The tests cover thels,echo | catandsh scriptloop bodies. - ⏭️
read: bad fd 0once stdin is drained: deferred to your follow-up, which makes sense to me. - ✅ Merge: with a 200 ms deadline, a stop inside
{ sleep 1; } | catorx=$({ sleep 1; } | cat)still hands the caller's input back, and the nextcatreadsa b. With an embedder stdin left open,lua -ereads[]without hanging afterx=$(true),$x | cat,{ true; } | catandread l </dev/null.
I found no new issues.
What I verified
- Ran
cargo test --release --test shell_integration --test lua_integrationat6131858: 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.txtin the artifacts portal. - The deadline and Lua checks ran through a throwaway
Shell::builder()test with aset_channel_reader/set_outside_readerstdin. I didn't commit it. - I reviewed the merge resolution in
git show --cc 6131858. Inrun_capturing_statusandexecute_item,give_stdinnow runs beforestop_at_deadline. Inrun_pipeline, the #257 deadline check comes aftergive_stdin(shell_proc, held_stdin).take_fd/put_fdkeep the outside-input marker.
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 withinput=$(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 likeprintf 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< fileredirect on one command, and a compound piped into a pipeline, from throwing away the caller's stdin, because otherwise(cat </dev/null); catloses the input that bash keeps for the secondcat. This also fixeswhile readloops that stopped after one pass when the body ran a command that doesn't read stdin. It builds on #224, which gavesh scriptits 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 thewhile readloop. 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. Areadafter another command already used up stdin still prints an error that bash doesn't. That happens on main too, so I left it out.cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.