Repository navigation
fix: exit the shell when exit runs inside a function - #222
Conversation
2b1d53d to
21002d6
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. exit inside a function now ends the shell, subshell, $( ) or script the same way bash does. I found no blocking issues. One 🟡 inline: keep the new pending_exit flag pub(crate) so it doesn't become public API (if it stays pub, this needs needs-api-review).
✅ What I verified
- Tests:
cargo test --workspace --all-targetsat21002d6passed 2,226 tests with 0 failures (stable 1.100, debuginfo off). - Bash comparison: I ran 87 scripts under bash, a
mainbinary and a PR binary. They coverif/while/until,&&/\|\|/!, groups,case,for,eval, nested functions,(…),$( ), pipelines in both directions,&withwait, redirects including failing ones,x=VAL f,command/builtin, EXIT traps,${u?},set -u, arrays, and a function exit followed byexit N. - No regressions: in every case that works on
main, the PR either matches bash or behaves asmaindoes. The headline casef(){ exit 6; }; f; echo nonow gives no output and status 6, the same as bash (mainprintednoand returned 0). - Leftover state: a pending exit can't leak into the next call.
executeandexecute_with_readerreset both flags (src/exec.rs:194-195,:237-238). Every path throughrun_pipelineeither goes throughexecute_pipeline_checked, which consumes the flag, or clears it itself (pipeline tails and theexec/commandpaths). - Removed early return: removing it in the function path (
src/exec.rs:3469) also fixes$(f)output, whichmainthrew away when the function exited (f(){ echo a; exit 3; }; X=$(f; echo after)now gives[a]).
Passes: the correctness and adversarial subagents both timed out at 900s without returning anything. Disk ran out during the first test build, which slowed them down. I read both role files and ran both passes inline instead, so this is a single-reviewer pass.
Appendix: non-blocking (3)
- ⚪ Stale
refused_arrayafter a background job in$( ). Atsrc/exec.rs:1845the newBackgroundarm swallows theExitwithout clearingrefused_array. The next function'sexitin the same substitution is then ignored. Repro:g(){ exit 4; }; x=$(a=(1) & g; echo in); echo "x=$x"givesx=inon the PR andx=in both bash andmain. Withtrue &in place of the array the PR is correct. This needs an array inside a backgrounded command inside$( ), so it's unlikely in practice. Possible fix: addproc.refused_array = false;in that arm. - ⚪ Pre-existing, not filed (outside this PR's scope):
trap 'exit 6' EXITends with status 0 instead of 6, andexit $(exit 6)gives 1 instead of 6. The second is in the same family as #232. - ⚪ Differences that come from syntax the shell doesn't support yet (
<<<,time,select,[[ ]],<( ),declare) were the same onmain. #207 already tracks them.
Evidence: s3://strandly-prod-artifacts-319426459652/strands-agents/shell/pr/222/ contains cargo-test.log, bash-parity.txt, repro-refused-array.sh and repro-refused-array.out.
|
@strandly-the-agent Please review this PR Both findings are fixed in b8e912d. Full workspace tests, fmt, clippy, doc tests, and warning-denied docs pass. The original 23 Bash comparisons still pass, plus five stdout/status checks for the new edge cases and normal |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve at b8e912d. Both earlier findings are fixed, and I found nothing new.
- 🟡
pending_exitis nowpub(crate)(src/os.rs:151). Fixed. - ⚪ The captured background arm now clears
refused_array(src/exec.rs:1848). Fixed. My reprox=$(a=(1) & g; echo in)now givesx=, the same as bash.
✅ What I verified
- Tests:
cargo test --workspace --all-targetsatb8e912dpassed 2,229 tests with 0 failures. - New tests: I deleted the one-line fix and re-ran the 3 new
background_array_refusal_*tests. All 3 fail without it and pass with it restored. - Bash comparison: my 87 scripts from the last review give exactly the same output on
b8e912das on21002d6, so nothing regressed. - Probing the fix: I tried scripts that might lose an exit after the flag is cleared:
x=$(a=(1) & true; g; echo in), the same pattern inside a function (f(){ a=(1) & g; echo in-f; }), andreturnafter a background job inside$( ). All three match bash. The flag only matters until the function returns, and this arm is where the exit gets swallowed, so clearing it there is the right place.
Evidence: s3://strandly-prod-artifacts-319426459652/strands-agents/shell/pr/222/cargo-test-b8e912d.log
b8e912d to
3585172
Compare
|
@strandly-the-agent Please review this PR Rebased onto main through 7000b66 in 3585172. The conflict with merged #217 is resolved: the compound pipeline keeps its producer status for pipefail and clears the function-exit flag at the pipeline boundary. The earlier visibility and array-refusal fixes remain intact. Full workspace tests, fmt, clippy, doc tests, and warning-denied docs pass. All 38 Bash stdout/status comparisons pass, including ten new combined pipefail/function-exit cases covering both compound pipeline directions, command substitution, return, and errexit; 33 also compare stderr presence. GitHub now reports this PR mergeable. The final fetch included only the unrelated #218/#219 merges, and the combined merge tree was clean. |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve at 3585172. The rebase over #217 is correct. Both compound pipeline paths keep the producer's status for pipefail and clear pending_exit after the tail, and the earlier fixes are still in place (src/os.rs:153, src/exec.rs:1848). I compared 107 scripts against bash and none got worse than on main. 2,353 tests pass.
One non-blocking item, from the same cause as the background-job fix: refused_array also goes stale when a captured compound pipeline's first stage refuses an array. Details below.
⚪ Stale refused_array in a captured compound pipeline
At src/exec.rs:2010-2011, a $( ) compound pipeline runs its first stage on the caller's proc, and captured_status(result, proc.last_exit) swallows the refusal's Exit without clearing the flag. The next function's exit in the same substitution is then ignored:
g(){ exit 4; }; x=$({ a=(1); } | cat; g; echo in); echo "x=$x"givesx=in. bash givesx=.- With
{ true; }in place of the array, the PR givesx=correctly. The flag is the cause.
This needs an array inside a compound pipeline inside $( ), so it's unlikely in practice. Possible fix: add proc.refused_array = false; after the captured_status call at line 2011.
✅ What I verified
- Tests:
cargo test --workspace --all-targetsat3585172passed 2,353 tests with 0 failures. - Rebase:
git range-diffagainstb8e912dshows commit 3 unchanged. Commits 1 and 2 differ only where #217's pipefail andopt_pipefailchanges land. - Conflict resolution: in both compound pipeline paths,
pending_exit = falsefollows the tail (src/exec.rs:2024,:2282). The producer status comes fromcaptured_status, so a function exit there becomes its status for pipefail. - Bash comparison: I ran 107 scripts and compared stdout and exit status across bash,
mainat7000b66and the PR. Zero scripts that matched bash onmainstop matching on the PR. The 20 new ones cover pipefail with a function exit on either side of a compound pipeline, in$( ), with!,return,set -e/-eo pipefail,whileproducers and three-stage pipelines, and all match bash except the item above.{ f; } | { f; }fails to parse on bothmainand the PR (#207). - Passes: the rebase delta is a small conflict resolution plus a re-check of my earlier findings, so I reviewed it myself rather than spawning subagents.
Evidence: s3://strandly-prod-artifacts-319426459652/strands-agents/shell/pr/222/ contains cargo-test-3585172.log and bash-parity-3585172.txt.
|
@strandly-the-agent Please review this PR Fixed the captured compound-pipeline refusal in 9c24ef2. Once the producer's control flow becomes its stage status, I clear All four new regression tests failed before the fix and pass now, including pipefail. I also audited the other status/control-flow consumption sites: forked processes are discarded, while redirected groups, eval, and source propagate the result. A 72-case CLI matrix covering nine refusal boundaries, four subsequent function outcomes, and pipefail on/off matches Bash stdout and exit status. It reproduced 12 failures before this fix, all at this compound boundary. Array diagnostics remain specific to this shell's unsupported-array behavior. The full Rust test suite, fmt, clippy, doc tests, warning-denied docs, and the existing 38 Bash comparison probes pass. Please re-review the current head. |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve at 9c24ef2. The compound-pipeline refused_array item from my last review is fixed, and I found nothing new.
- ⚪
$( )compound pipeline first stage (src/exec.rs:2013): fixed.g(){ exit 4; }; x=$({ a=(1); } | cat; g; echo in)now givesx=, the same as bash.returnstill returns only from the function (x=in 4, as in bash).
✅ What I verified
- Tests:
cargo test --workspace --all-targetsat9c24ef2passed 2,357 tests with 0 failures. - New tests: I deleted the fix line at
src/exec.rs:2013and ran the 4compound_array_refusal_*tests. All 4 fail without it and pass again once it's restored. - Fix placement:
proc.refused_array = false;comes right aftercaptured_status(result, proc.last_exit)turns the stage's control flow into a status, and before the tail runs. So a refusal in the tail still reaches the tail's own boundary. - Bash comparison: I ran 120 scripts and compared stdout and exit status across bash,
mainat7000b66and the PR. These are the 107 from my last review plus 13 new ones at this boundary: the refusal in the tail, three-stage pipelines, nested compound pipelines, the substitution inside a function, a redirected group,eval, andreturn. Zero scripts that matched bash onmainstop matching on the PR. - Remaining differences: all expected. They come from syntax the shell doesn't support yet or the separate trap/substitution-status gaps, and the two new ones exist only because arrays are refused (bash runs them).
- Passes: this delta is a two-line fix plus tests for my own finding, so I reviewed it myself rather than spawning subagents.
Evidence: s3://strandly-prod-artifacts-319426459652/strands-agents/shell/pr/222/ contains cargo-test-9c24ef2.log and bash-parity-9c24ef2.txt.
Description
exitinside a shell function only returned from the function, so the script kept going:with status 0. In bash that prints nothing and exits with 6.
Now
exitin a function ends the shell, or only the subshell, command substitution or script it runs in, like bash. A function that exits as a pipeline stage or in a background job still ends only that stage or job, andreturnworks as before. Other errors that end the shell when raised inside a function (${x?msg},set -uunbound variables) now end the shell too, as in bash, instead of only returning from the function.I compared about 30 cases against bash (loops,
if,&&,!, groups,eval, nested functions, subshells,$( ), pipelines, background jobs, scripts, EXIT traps) and added tests for them. I also added tests so a function's exit can't carry over into the nextShell::runor MCP call. Assignment-only commands such asx=$(...)still set$?to 0; #232 covers that separately.Related Issues
Part of #207
Documentation PR
N/A
Type of Change
Bug fix
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
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.