Repository navigation
fix(traps): EXIT in whole-comsubst/pipeline calls, global RETURN, no-hang return - #1155
Merged
Merged
Conversation
… 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.