Skip to content

fix(executor): until-break errexit attribution and -E subshell ERR inheritance (#702) - #719

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/702-errexit-until-break
Oct 17, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/702-errexit-until-break

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes the two deterministic errexit / trap-inheritance divergences from GNU bash reported in #702.

R4.1 — set -e misfires in an until loop whose body starts with break/continue

An until loop evaluates its condition with errexit suppressed and leaves the condition's failing status in the shell status. The body's first command then runs; break/continue unwound without establishing their own status, so the dispatcher's post-command errexit / ERR-trap gate attributed the condition's status to that body command:

before:  $ set -e; until false; do break; done; echo REACHED   ->  (exits 1, no output)
after:                                                          ->  REACHED  (rc 0)

before:  $ set -e; until (exit 7); do break; done               ->  exits 7
after:                                                          ->  rc 0

before:  $ set -e; trap 'echo ERR:$?' ERR; until false; do break; done  ->  ERR:1
after:                                                          ->  (silent, rc 0)

GNU break.def/continue.def return EXECUTION_SUCCESS for an in-range count; the fix establishes that status in execute_loop_control before unwinding, so the attribution is correct for every loop form and every position (directly or as the first command of a group). while true; do break; done, for i in 1; do break; done and until false; do :; break; done are unchanged.

R4.2 — set -E not inherited by ( ) subshells

GNU reset_or_restore_signal_handlers keeps the ERR trap in a forked subshell child when error_trace_mode (set -E) is set, exactly as it keeps DEBUG/RETURN under functrace (set -T). rubash's reset_for_subshell unconditionally recorded ERR as reset, so the inherited trap never fired inside a ( ) body even with -E:

before:  $ set -E; trap 'echo ERR' ERR; ( false ); echo done  ->  ERR / done        (1 ERR)
after:                                                          ->  ERR / ERR / done  (2 ERR)

before:  $ set -E; trap 'echo ERR' ERR; f(){ false; }; ( f ); echo done  ->  ERR / done        (1 ERR)
after:                                                                    ->  ERR / ERR / ERR / done  (3 ERR)

before:  $ o=$(set -E; trap 'echo E' ERR; (false)); echo "[$o]"  ->  [E]
after:                                                           ->  [E\nE]

A subshell-child variant of the reset honors errtrace and is used from the two true subshell entry points (( ) and f() ( ) bodies). The command-substitution, pipeline-element and read copy paths keep the unconditional reset they model separately. Without -E the behavior is unchanged (trap 'echo ERR' ERR; ( false ) still fires once), and a subshell still lists the inherited trap (( trap -p ERR )).

Tests

New tests/issue702_errexit_until_break_regressions.rs pins all of the above against byte-verified GNU bash 5.3.0 output, including the unaffected while/for forms, the leading-: control, condition-status non-leak, ERR-trap silence, and the no-errtrace control.

Focused suites run green: issue702_*, issue531, issue520, issue343_345, issue546, issue524, issue439, issue395, issue507, issue70, issue_s6, issue661, issue485, issue302_304, issue505, issue543, issue589, issue294, issue345; lib filters trap/loop; cargo fmt --check clean.

Known adjacent quirks (left alone, pre-existing and out of #702's scope)

  • ( (false) ) with -E: GNU 2 ERR fires, this branch 3 (a nested subshell as the first body command over-fires; GNU suppresses the middle-level fire).
  • f() ( false ); f with -E: GNU 3, this branch 2 (the function-with-subshell-body call is missing one of GNU's two level-0 fires).
  • cat <(false) with -E (process substitution) still differs; the process-substitution copy path was not touched.

…break/continue (#702)

An until loop runs its condition with errexit suppressed, leaving the
condition's failing status in self.exit_code. The body's first command
then runs; break/continue unwound without establishing their own status,
so the dispatcher's post-command errexit / ERR-trap gate attributed the
condition's failure to that body command. GNU break.def/continue.def
return EXECUTION_SUCCESS, so establish that status before unwinding:
`until false; do break; done` now reaches the following command and
leaves $? = 0, and `until (exit 7); do break; done` no longer leaks 7.
GNU reset_or_restore_signal_handlers keeps the ERR trap in a forked
subshell child when error_trace_mode (-E) is set, exactly as it keeps
DEBUG/RETURN under functrace (-T). reset_for_subshell unconditionally
recorded ERR as reset, so the inherited trap never fired inside a
`( )` body even with -E. Add a subshell-child variant that honors
errtrace and use it from the two true subshell entry points; the
command-substitution, pipeline-element and read copy paths keep the
unconditional reset they model separately.
GNU bash 5.3.0 byte-verified expectations for the until/break
attribution (while/for unaffected, leading no-op unaffected, condition
status non-leaking, ERR trap silent) and for -E propagation into
`( )` subshells (plain subshell, function-in-subshell, comsub-wrapped
subshell, and the no-errtrace control).
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.

1 participant