Repository navigation
fix(exec): skip EXIT trap on exec replacement - #1031
Merged
Merged
Conversation
GNU builtins/exec.def exec_builtin hands the resolved command to execute_cmd.c:6126 shell_execve, which replaces the shell image on success — the exit path (shell.c:985 exit_shell -> run_exit_trap) is never taken, so `trap ... EXIT; exec echo REPLACED` prints only REPLACED. trap.c:1440 reset_or_restore_signal_handlers additionally clears SIG_TRAPPED for the exit trap (keeping the string) before shell_execve, so a post-reset execve failure (slash operand, e.g. ENOENT/EACCES/EISDIR) also exits without the trap; only the pre-reset search failure (command == 0 -> sh_notfound -> failed_exec) keeps it. Rubash cannot execve: it spawns the replacement and unwinds with its status, and that unwind ran the EXIT trap unconditionally. Model the GNU rule with an exec_replaced latch on the Executor, set on the exec-replacement unwind only when the operand reached the post-reset world (slash bypasses search_for_command, else a successful find_user_command), checked at the single exit-trap choke point (run_exit_trap_for_status_with_output_redirects). Fork clones start clear; the in-place flat-subshell frame (execute_cmd.c:1576) saves and restores it so a subshell exec cannot disarm the parent's trap. Probes (bash 5.3.20 script-file oracle, all byte-identical incl. rc): exec-echo, exec-false, search-failure (trap kept), slash-failure (no trap), normal exit (trap kept), option error, subshell, comsub, nested subshell, function exec. Tests: tests/issue1012_exec_exit_trap_regressions.rs (8); lib 693 passed; related exec/trap suites 72 passed. executor_tests shows the same 13 pre-existing environment failures with and without this change (verified via stash).
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 #1012. GNU exec_builtin -> shell_execve (execute_cmd.c:6126) replaces the image with no exit path (never reaches exit_shell/run_exit_trap); trap.c:1440 also clears SIG_TRAPPED before shell_execve, so slash-path exec failures don't trap either — only pre-reset search failures (sh_notfound -> failed_exec) keep the trap, verified on GNU 5.3.20. Rubash emulates exec via spawn+unwind, which unconditionally ran the trap. Fix: exec_replaced latch on Executor (fork-clone false), set on the post-reset replacement unwind (slash bypasses search; otherwise only successful PATH search counts), single EXIT-trap choke point honors it; flat-subshell save/restore so body exec can't disarm the parent. Tests: lib 693 + 8 new regression + 72 exec/trap integration green; 11-shape GNU parity (exit/search-fail/slash-fail/subshell/comsub/function) byte-identical.