Skip to content

WIP: fix(traps): no ERR adjudication for an exit unwind (#2482 t133 residual) - #2512

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2482-t133-err-retrigger
Nov 1, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2482-t133-err-retrigger

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2482 — the t133 residual left open by #2506: inside the EXIT
trap action, a command chain terminated by an inner exit fired
the ERR trap a second time (bats printed a duplicate raw
failure callback line; mini2212_t133 stayed red).

Stacked on #2506 (fix/2482-2484-trap-stream @ 8d51e7f) —
this PR's base is that branch; it carries exactly one commit on
top and should merge after / be rebased with #2506.

GNU law (vendored bash-5.3 source)

run_error_trap is called only from execute_cmd.c:773/1000/1166/
2909, always for a command that completed with a status. The
exit builtin never completes: builtins/exit.def exit_or_logout
ends in jump_to_top_level (EXITBLTIN), and run_exit_trap
(trap.c:1027) installs its own setjmp(top_level) to catch it —
so no frame the unwind crosses adjudicates the chain. Ordinary
failures inside trap actions adjudicate exactly like anywhere
else (no running_trap gate at any site), and an exit inside a
subshell / pipeline element is an ordinary failure of that
subshell command in the parent.

Truth table (GNU 5.3.20, script files; ERRFIRE counts):

shape GNU
EXIT action chain h→g→exit 1, -eET 0
same + prior body failure 1 (body only)
EXIT action bare exit 3 0
main-body chain h→g→exit 1 0
bare exit 1 / { exit 1; } 0
eval 'exit 1' / . src(exit) 0
eval 'false' / . src(false) 1
action fn with ( exit 1 ) 1
( exit 1 ) statement 1
true | g in action (g exits) 1
action fn failing ordinarily, -eET 1, action aborts
nested failure chain, no -e 2 (per-frame)

Defect site

execute_materialized_command's errexit tail (command_dispatch.rs)
fired maybe_run_error_trap for any nonzero exit_code under live
errexit — including an in-flight Err(ExitCode):

  1. at the exit builtin's own dispatch frame (attribution probes:
    the fire carries the exit node's own line), and
  2. again at eval/source wrapper frames whose inner command had
    already been adjudicated and converted (eval 'false' fired
    twice, . src(false) fired twice).

Function-call frames never re-fired: the funnel's function fast
path (command_execute.rs execute_function_command_invocation)
bypasses this tail, which is why the extra count was exactly one
per chain.

Fix

One gate: the tail fires only when result.is_ok() — the same
rule the branch's own conversion mark already states ("mark ONLY
the conversion of an Ok completion — an Err already in flight
stays unmarked"). In-flight unwinds propagate byte-identically;
nothing in the session/subshell exit machinery changes.

Verification

…t133)

GNU's run_error_trap call sites (execute_cmd.c:773/1000/1166/2909)
only ever adjudicate a command that completed with a status; the
exit builtin never returns one — builtins/exit.def exit_or_logout
ends in jump_to_top_level (EXITBLTIN), and run_exit_trap
(trap.c:1027) catches that jump with its own setjmp, so no frame
the unwind crosses performs failure adjudication for an
exit-terminated chain.

execute_materialized_command's errexit tail fired
maybe_run_error_trap whenever exit_code was nonzero under live
errexit, including for an in-flight Err(ExitCode): once at the
exit builtin's own dispatch frame (`set -eET; trap .. ERR; exit 1`
fired in rubash, silent in GNU), and again at eval/source wrapper
frames whose inner command had already been adjudicated
(`eval 'false'` fired twice). In the bats cell-133 shape the
extra fire re-ran bats_error_trap inside the EXIT teardown chain,
printing a second raw "failure callback" line.

Gate the tail's fire on result.is_ok() — the same rule the
branch's conversion mark already applies (only an Ok completion
is converted). In-flight unwinds propagate unchanged.

Stacked on PR #2506 (fix/2482-2484-trap-stream), whose face-a/b
trap machinery this adjudication sits in.

Verification (GNU bash 5.3.20, script files): 33 probe shapes
count-compared, core shapes byte-identical (exit chains in the
body / EXIT action / eval / source / subshell / pipeline stage,
plus ordinary-failure controls). New pins
tests/issue2482_t133_err_retrigger.rs 16/16; #2506 pins 8/8 + 8/8;
issue2212 15/15; adjacent trap/errexit targets all green.
mini2212_t133.bats: ok, TAP identical to GNU (was not ok on the
#2506 tip).

Refs #2482
@caomengxuan666
caomengxuan666 force-pushed the fix/2482-t133-err-retrigger branch from 4a4fb3f to 664fd86 Compare November 1, 2026 19:15
@caomengxuan666
caomengxuan666 changed the base branch from fix/2482-2484-trap-stream to master November 1, 2026 19:15
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 1, 2026 19:32
@caomengxuan666
caomengxuan666 merged commit f3815b0 into master Nov 1, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2482-t133-err-retrigger branch November 2, 2026 01:09
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2512 PASS — pin issue2482_t133_err_retrigger 16/16 x2 @--test-threads=4 on Windows. Family neighbors green in the same session on the same tree: issue2482_err_trap_pipeline 8/8, issue2482_trap_unwind_stack 8/8, issue2212_bats_remainder_regressions 14/14 (x2 each; 1 of 15 unix-gated). See the #2506 reply for the one unpinned +0-config DEBUG over-firing found live this session (pre-existing to this PR's scope, applies to the family).

Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 9348b8d (contains this merge). No BLOCKER.

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