Skip to content

fix(traps): EXIT in whole-comsubst/pipeline calls, global RETURN, no-hang return - #1155

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/trap-trio
Oct 23, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/trap-trio

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #1104 (P1), closes #1105, closes #1106. (1) Function calls that ARE the whole comsubst body/pipeline stage now run the EXIT trap into the capture/pipe (GNU subst.c:7340-7341, execute_cmd.c:1761-1763); bare-trap inline fast path only yields for EXIT installs. (2) RETURN traps are last-set-wins global: drop the function-scoped gate, remove inherited trap on untraced entry (functrace/DEBUG semantics kept), unconditional fire, restore-if-still-untrapped. (3) return N inside a RETURN trap is absorbed once as function status (deliberate divergence: GNU 5.3 recurses forever/hangs; we exit rc=N with no hang). Tests: lib 693 + 8 new regression + trap/pipe/comsub/function suites green, executor_tests delta-free vs clean tree; GNU parity byte-identical incl. rc.

… no-hang RETURN+return

Root causes (GNU C source is the spec, third_party/bash @9c465866):

#1104 (P1): an EXIT trap installed inside a function was silently
dropped when the call was the entire comsubst body or a pipeline
segment. GNU ends the comsub child with `rc = run_exit_trap ();
exit (rc)` (subst.c:7340-7341, captured into the substitution) and
runs the subshell EXIT trap at every pipeline element exit
(execute_cmd.c:1761-1763). The `$(f)` word-level fast path
(run_function_command_substitution) restored the parent trap table
without ever firing, and the function/builtin pipeline stages never
ran the exit trap (unlike the compound stage). Fix: reset_for_subshell
at entry + run_exit_trap_for_status before the boundary restore, with
output merged into the capture/pipe. The bare-`trap` pipeline inline
arm (throwaway env clone) now declines EXIT installs to the subshell
stage via trap_stage_installs_exit; queries/resets/non-EXIT installs
stay inline (no observable in-stage effect).

#1105: nested RETURN traps are global last-set-wins
(execute_cmd.c:5291-5303 + trap.c trap_if_untrapped), but niu gated
the fire on a function-scoped cell (each fired its own). Fix: remove
__RUBASH_RETURN_TRAP_FUNCTION/note_return_trap_scope; at function
entry remove the inherited RETURN trap for untraced functions (kept
for functrace/`declare -ft`, never under an active DEBUG trap),
fire unconditionally, then restore the saved action only if removed
and still untrapped. Outer return now runs the inner install; a
body `trap - RETURN` still restores the saved action.

#1106 (P3, pathological): `return N` in a RETURN action recurses
forever in GNU (the from_return_trap setter is #if 0'd out, so
execute_cmd.c:5377 refires; bash 5.3.20 hangs on the probe). NiU
absorbs it once: run_function_return_trap returns the absorbed status
and the dispatcher lets it win over the body status. No hang, no
stray `return 3` diagnostic (rc=3, empty stderr).

Probes (bash 5.3.20 vs fixed binary, script files, stdout+stderr+rc):
p1104a/p1104b/v5 captured=[body+EXIT-TRAP] / pipe body+EXIT-TRAP;
p1105/v1 B,B; v4 inner-removal restores A; v2/v3/v6 unchanged;
p1106/p1106b run once with rc=3 (GNU hangs: deliberate divergence).
New tests/issue1104_1105_1106_trap_trio_regressions.rs (8 tests).
cargo fmt clean; lib (693) + trap/pipe/comsub/function suites green.
executor_tests has 8 pre-existing Android-env failures (e.g. hardcoded
64) SIGRTMAX / bash-5.3.0 strings), verified identical on pristine
origin/master.
@caomengxuan666
caomengxuan666 merged commit b01fe7a into master Oct 23, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/trap-trio branch October 23, 2026 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant