Skip to content

fix(exec): skip EXIT trap on exec replacement - #1031

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/1012-exec-trap
Oct 22, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/1012-exec-trap

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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.

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).
@caomengxuan666
caomengxuan666 merged commit d93ef48 into master Oct 22, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/1012-exec-trap 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

Development

Successfully merging this pull request may close these issues.

[bug] EXIT trap fires on exec — GNU bash does not run it

1 participant