Skip to content

WIP: fix(traps): skip the function epilogue's trap restores on the fatal -e unwind (DEBUG over-report, Refs #2546) - #2548

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2546-debug-overreport
Nov 2, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2546-debug-overreport

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: fix(traps): skip the function epilogue's trap restores on the fatal -e unwind

Refs #2546 (regression introduced by #2506) — do not merge while draft

Symptom

set -eu + armed DEBUG/EXIT traps + nounset failure inside an untraced function printed one extra DEBUG record (DEBUG cmd=[echo ${u2482bad}]) that GNU bash 5.3.20 does not print — both issue forms (function-valued EXIT action p2482b.sh, string-valued p2482c.sh). pre-#2506 (d529b35) matched GNU byte-for-byte.

Root cause (GNU C citations)

  • execute_cmd.c:5270-5278 execute_function: an untraced function body runs with the DEBUG trap disarmed (restore_default_signal (DEBUG_TRAP) clears SIG_TRAPPED); the restore is registered as the unwind protect uw_maybe_set_debug_trap (trap.c:756), alongside uw_maybe_set_error_trap (:5286) and uw_maybe_set_return_trap (:5301).
  • A fatal ExitCode/LastpipeExit unwind under live errexit longjmps past every execute_function unwind protect (the same skip WIP: fix(traps): #2482 fatal-unwind stack + ERR-in-pipeline (faces a/b); #2483/#2484 analysis #2506 already pinned for uw_restore_funcarray_state frame pops) — so the EXIT action executes in the dying scope, where DEBUG is still disarmed, and run_debug_trap (trap.c:1275, sigmodes[DEBUG_TRAP] & SIG_TRAPPED gate) never fires for the action's commands.
  • rubash's function epilogue (src/executor/function_calls.rs) restored the entry-saved DEBUG (and ERR/RETURN) actions unconditionally, re-arming the trap before the EXIT funnel; WIP: fix(traps): #2482 fatal-unwind stack + ERR-in-pipeline (faces a/b); #2483/#2484 analysis #2506's (correct) re-presentation firing for in-action commands then over-reported by one line per dying untraced frame chain.

The frame-stack half of the protect skip was already gated (exit_unwind); the trap-table half was missed. This PR hoists that same gate above the three restores — one invariant for the whole protect family, no print-surface patch. With -e off the DISCARD unwind runs the protects (sig.c:387 top_level_cleanup), the restores correctly happen, and the action's first command keeps its re-presentation firing.

A/B (WSL GNU bash 5.3.20 oracle, script files, stdout+stderr+exit)

cell GNU pre-fix master this PR
p2482b (fn action, untraced, -eu) 2 DEBUG lines +1 extra byte-identical
p2482c (string action, untraced, -eu) 2 DEBUG lines +1 extra byte-identical
top-level failure (-eu) 3 (re-presentation kept) 3 byte-identical
functrace failure (-eu) 6 (all firings kept) 6 byte-identical
-e off (DISCARD, set -u) 3 (restore ran, firing kept) 3 byte-identical
nested g→f failure 2 (no firing) +1 extra byte-identical
exit builtin in fn no extra, FN leak kept same byte-identical
success baseline 4 (EXIT re-presentation) 4 byte-identical

Verification

  • Counting-law matrix (success/failure × function/top-level × DEBUG/EXIT/RETURN × functrace × -e off × nested × exit-builtin): 10/10 cells byte-identical; both issue probes 9/9 (incl. frame-leak check FN=f).
  • tests/issue2546_debug_overreport.rs: 12/12 — both issue forms + the "+0 counting law" matrix with explicit DEBUG-line counts.
  • WIP: fix(traps): #2482 fatal-unwind stack + ERR-in-pipeline (faces a/b); #2483/#2484 analysis #2506 pins stay green: issue2482_trap_unwind_stack 8/8, issue2482_err_trap_pipeline 8/8, issue2212_bats_remainder_regressions 14/14 (incl. issue2212_13 re-presentation pin).
  • Trap family (37 test files): 190 passed / 6 failed — the 6 failures reproduce identically on pristine master 9348b8d9 (pre-existing: issue85/86/290 trio, issue524 sourced-exit, issue797 preexec transcript, issue_1511). cargo test --lib: 1126/2 — both failures identical on pristine master.

fmt disclosure (the os-206 pitfall)

Whole-crate cargo fmt --check fails on this machine in every checkout (incl. pristine master): cargo-fmt feeds the crate's ~900 test targets to rustfmt in one command line and CreateProcess fails with os error 206 — and independently, master already carries rustfmt drift in files this PR does not touch (src/debugger/wiring_tests.rs, src/executor/assignment_expansion.rs, ...). Per the crew protocol (206 pitfall → per-file + disclosure): both files in this PR were formatted and verified clean with per-file rustfmt --check --edition 2021 (exit 0), and the commit went through the pre-commit hook's own cargo not found degradation branch. No --no-verify was used.

…e unwind

Refs #2546 (regression introduced by #2506)

Root cause: #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 (#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 #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; #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).
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 04:00
@caomengxuan666
caomengxuan666 merged commit dd4ec14 into master Nov 2, 2026
13 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2548 PASS — new pin issue2546_debug_overreport 12/12 ×2 on Windows. No unix-gated faces in this file. No BLOCKER.

Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 2ce27e6 (contains this merge), oracle wsl /usr/local/bin/bash 5.3.20. Logs: D:/repo/win-accept/wave3/logs/issue2546_debug_overreport.r{1,2}.log.

@caomengxuan666
caomengxuan666 deleted the fix/2546-debug-overreport branch November 3, 2026 09:07
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