Skip to content

WIP: fix(traps): #2482 fatal-unwind stack + ERR-in-pipeline (faces a/b); #2483/#2484 analysis - #2506

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/2482-2484-trap-stream
Nov 1, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/2482-2484-trap-stream

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Cluster C (trap timing + stream ordering family) — per-issue status:

Refs #2482 — face a FIXED; face b pipeline half FIXED, mini still red on a pre-existing residual (corrected 2026-11-02).

  • Face a (cells 55/57/59/64): the [ecosuite] bats-core own suite: setup/teardown + file-evaluation machinery divergences (7 files, named tests) #2212 F4 umbrella silencing DEBUG
    inside the EXIT action was wrong — GNU execute_cmd.c:4506 calls
    run_debug_trap unconditionally; :4498 gates only the
    BASH_COMMAND refresh. Removed the umbrella (dedicated
    exit_trap_running flag for the freeze), and mirrored GNU's
    fatal-unwind stack semantics: while set -e is live at the
    failure point (flags.c:263), an ExitCode unwind skips the frame
    pops (execute_cmd.c:5329/5418), so the EXIT action sees the
    dying stack; the epilogue leaks the frames and the EXIT-trap
    funnel drops them after the action. With -e off, the DISCARD
    unwind runs the protects (sig.c:387) and the stack is clean.
    Minis t55/t57/t59/t64 green.
  • Face b (cell 133), pipeline half: pipeline elements are
    subshells — ERR now follows errtrace into elements
    (reset_for_subshell_child at both stage builders), and the
    element's own completion takes no wait-side adjudication
    (stage-root depth marker; nested ( ) commands keep theirs).
    All minimal pipeline shapes byte-identical to GNU; pins below.
  • Face b, residual (why the t133 mini is still red): rubash fires
    the ERR trap a second time INSIDE the EXIT trap action, for the
    action's command chain terminated by an inner
    exit "$bats_exec_suite_status" — the fixture's on_failure
    callback prints one extra raw line (6 vs GNU's 5). This is
    PRE-EXISTING on the frozen tree (minimal probes: GNU 1 fire /
    frozen 2; GNU 0 / frozen 1) and is the double-trigger the issue
    text names for this cell; the exit unwind itself is clean. The
    adjudication site is shared with the session/subshell exit
    machinery and was not pinned to a safe gate tonight — see
    REPORT.md face-b correction. An earlier "t133 green" note was
    based on a contaminated delivery and is withdrawn.
  • Face c (cell 106): not reproduced in isolation (probes
    identical to GNU); tonight's direct runs were polluted by VM
    load ≈ 35 starving the disowned bg job — re-run on a quiet
    machine before treating as a defect (REPORT.md).
  • Pins: issue2482_trap_unwind_stack 8/8, issue2482_err_trap_pipeline
    8/8 (scrubbed, GNU 5.3.20 expectations, local artifacts);
    issue2212 stays 15/15.

Refs #2483 — analysis only (in REPORT.md). Cells 115/116:
pipeline stage output is capture-then-emit, not streamed — a
producer's bytes sit in the stage machinery until the stage
completes, so a later direct stderr write from the suite process
overtakes the TAP stream (minimal repro in REPORT). This is the
#2244 drain-model family; the fix is the write-through streaming
rework, not safe on release night.

Refs #2484 — analysis only (in REPORT.md). Cell 118: inside a
pipeline-stage function, exec 3<&1 binds fd 3 to the stage's
stdout-capture (drained to the output file) instead of the
inter-stage pipe, so bats_tee's >(cat >&3) copy lands in the
file twice and the next stage gets nothing (fd-3 repro in
REPORT); the procsub delivery-timing half is the same drain
family as #2483.

Full report: cluster workdir REPORT.md
(~/workspace/rubash-wave-20261102/c-traps/REPORT.md). One commit
for #2482 (amended once to correct the mini tally); no code
change for #2483/#2484 by design of the split delivery.

@caomengxuan666
caomengxuan666 force-pushed the fix/2482-2484-trap-stream branch from eabd6cf to 3890916 Compare November 1, 2026 18:23
… pipeline elements

Refs #2482

Face a (bats cells 55/57/59/64): GNU execute_cmd.c:4506 calls
run_debug_trap unconditionally for simple commands; the
running_trap gate at execute_cmd.c:4498 covers only the
the_printed_command_except_trap refresh. The #2212 F4 umbrella
that silenced DEBUG inside the EXIT action was therefore wrong:
the EXIT window now sets a dedicated exit_trap_running flag whose
only job is the BASH_COMMAND freeze, and commands inside the
action take their re-presentation firing with the frozen command.

A fatal ExitCode/LastpipeExit unwind while `set -e` is live
(flags.c:263 exit_immediately_on_error = errexit_flag) skips
execute_function's frame pops (the uw_restore_funcarray_state
unwind protect, execute_cmd.c:5329, run at :5418 only on the
normal return), so the EXIT action executes with the dying call
stack installed (eval.c:102-106). The function epilogue now
leaks those frames — counted in ShellState's
exit_unwind_leaked_frames — and the EXIT-trap funnel drops them
after the action runs; FUNCNAME's att_invisible gates
(variables.c:1812, cleared on the skipped epilogue path at
execute_cmd.c:5167) honor the leak. The gate is the live -e
flag at the failure point, not the suppression state: a DISCARD
under a || suppression leaks every frame, while with -e off the
DISCARD unwind runs the protects first (subst.c
exp_jump_to_top_level -> sig.c:387 top_level_cleanup) and the
EXIT trap sees a clean stack. Both directions are pinned.
issue2212_13 is corrected to the re-presentation behavior.

Face b (cell 133): a pipeline element is a subshell in GNU, so
its ERR trap follows errtrace (trap.c:1480) exactly like a ( )
child — both pipeline stage builders now reset traps with
reset_for_subshell_child instead of the unconditional variant.
The element's own completion takes no wait-side adjudication
(execute_pipeline has no wait frame for an element): the stage
builder records the stage root's subshell_depth and
execute_command's subshell tail skips the fire (and the paired
debugger park) at exactly that depth; nested ( ) commands inside
the body sit deeper and keep their wait-side fire.

Regression pins (GNU bash 5.3.20 expectations, scrubbed runs):
tests/issue2482_trap_unwind_stack.rs 8/8,
tests/issue2482_err_trap_pipeline.rs 8/8,
tests/issue2212_bats_remainder_regressions.rs stays 15/15.
h-bats minis: t55/t57/t59/t64 green; the t133 pipeline face is fixed (the mini stays red on a pre-existing exit-in-EXIT-action ERR adjudication residual - see the cluster REPORT).
@caomengxuan666
caomengxuan666 force-pushed the fix/2482-2484-trap-stream branch from 3890916 to 8d51e7f Compare November 1, 2026 18:24
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 1, 2026 18:58
@caomengxuan666
caomengxuan666 merged commit ab591d4 into master Nov 1, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 1, 2026
…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 deleted the fix/2482-2484-trap-stream branch November 2, 2026 01:09
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2506 PASS on all pinned faces; one new divergence found on an unpinned shape, flagged (not self-fixed) — Windows-runnable pins all green x2 @--test-threads=4: issue2482_err_trap_pipeline 8/8, issue2482_trap_unwind_stack 8/8, issue2212_bats_remainder_regressions 14/14 (1 of 15 unix-gated). Live probe set -eET + ERR trap + false | true byte-identical to WSL GNU 5.3.20.

NEW FINDING (A/B-verified, introduced by this family): with DEBUG + EXIT armed and a nounset DISCARD failure (set -eu; trap dbg DEBUG; trap xt/string/ EXIT; f(){ echo ${u2482bad}; }; f), master fires an extra DEBUG cmd=[echo ${u2482bad}] for the failing command before the EXIT action — WSL GNU Bash 5.3.20 fires none (verified in BOTH the function-action and string-action EXIT forms; unbound-variable diagnostic, rc, and remaining DEBUG lines otherwise byte-identical). Pre-#2506 master (d529b35, built and probed in this session) matches GNU exactly — so the face-A repricing added here over-fires in these +0 configs. Repro: D:/repo/win-accept/wave2/probes/p2482b.sh, p2482c.sh (cmp.sh driver). Recommend a counting-law pin for the +0 configs (EXIT action armed, no ERR) on the trap lane.

Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 9348b8d (contains this merge), oracle wsl /usr/local/bin/bash 5.3.20. No BLOCKER; the over-firing is a divergence to home, not a crash/hang.

SpookyWaste pushed a commit to SpookyWaste/rubash that referenced this pull request Nov 12, 2026
…e unwind

Refs unixwin#2546 (regression introduced by unixwin#2506)

Root cause: unixwin#2506 taught the EXIT-trap window that commands inside the
action take DEBUG re-presentation firings (correct for an ARMED trap:
GNU execute_cmd.c:4506 calls run_debug_trap unconditionally for simple
commands, and the running_trap gate at :4498 covers only the
the_printed_command_except_trap refresh). But the funnel then consulted
a trap table rubash had already re-armed: the function epilogue
restored the entry-saved DEBUG action unconditionally, while GNU
restores it via the uw_maybe_set_debug_trap unwind protect
(execute_cmd.c:5276, registered next to uw_maybe_set_error_trap at
:5286 and uw_maybe_set_return_trap at :5301) — and the fatal
ExitCode/LastpipeExit unwind under live errexit longjmps past every
one of those protects. The EXIT action therefore executes in the dying
scope, where an untraced function had disarmed the DEBUG trap at entry
(execute_cmd.c:5278 restore_default_signal), so run_debug_trap
(trap.c:1275, `sigmodes[DEBUG_TRAP] & SIG_TRAPPED`) never fires — GNU
prints no DEBUG record for the failing command. The frame-stack half
of the same protect skip was already modeled (unixwin#2506's leaked frames,
gated on exit_unwind); the trap-table half was missed, so master
printed one extra `DEBUG cmd=[echo ${u2482bad}]`.

Fix: hoist the epilogue's existing exit_unwind condition (Err(ExitCode
| LastpipeExit) && errexit_enabled, the unixwin#2506 frame-leak gate) above
the restores and skip the DEBUG/ERR/RETURN restores on that path — one
invariant for the whole protect family, not a print-surface patch.
With `-e` off the DISCARD unwind still runs the protects
(sig.c:387 top_level_cleanup -> run_unwind_protects) and the restore
rightly happens, so the action's first command keeps its
re-presentation firing.

Verification (WSL GNU bash 5.3.20 oracle, script files):
- Both issue forms (p2482b function action, p2482c string action):
  stdout+stderr+exit byte-identical, extra DEBUG line gone.
- Counting-law matrix 10/10 byte-identical: success/failure x
  function/top-level x DEBUG/EXIT/RETURN x functrace x -e off x
  nested x exit-builtin; the armed cells (top-level failure, functrace,
  -e off, success baseline) keep every re-presentation firing.
- tests/issue2546_debug_overreport.rs 12/12; unixwin#2506 pins stay green
  (issue2482_trap_unwind_stack 8/8, issue2482_err_trap_pipeline 8/8,
  issue2212_bats_remainder_regressions 14/14); trap family
  (37 files) and cargo test --lib match pristine master exactly
  (the 6 trap-family and 2 lib failures reproduce on clean master).
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