Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: BUG: Exit trap is not executed when error occurs in a function



Well, the script did terminate, so at some point, the shell did exit, yet the exit handler was not executed. Whatever path is taken with regard to the function, exiting from it with an error or continuing to its end and return, the script eventually does exit, so the exit handler should be called, yes?

That is also my thinking, the exit handler should be executed.

The documentation for `TRAPEXIT()` states

Executed when the shell exits, or when the current function exits if defined inside a function.

The documentation for `trap arg sig` states

If sig is 0 or EXIT and the trap statement is not executed inside the body of a function, then the command arg is executed when the shell terminates; the trap runs before any zshexit hook functions.

In both cases, there is no exception for exits caused by errors when ERR_EXIT is active (or for exits caused by interruptions).

Here is a `test-exit.zsh` script to test what is going on:

TRAPZERR() { echo "Running zerr trap" }
TRAPEXIT() { echo "Running exit trap (main)" }
zshexit () { echo "Running exit hook" }

fun1() {
  TRAPEXIT() { echo "Running exit trap (fun1)" }
  eval "$@"
  echo "Leaving fun1"
}

fun2() {
  TRAPEXIT() { echo "Running exit trap (fun2)" }
  eval "$@"
  echo "Leaving fun2"
}

eval "$@"
echo "Leaving main"

 

Calling it with `fun1 fun2 false` forces it to invoke `fun1`, which then invokes `fun2`, which finally invokes `false`:

% zsh -f test-exit.zsh fun1 fun2 false  
Running zerr trap
Leaving fun2
Running exit trap (fun2)
Leaving fun1
Running exit trap (fun1)
Leaving main
Running exit trap (main)
Running exit hook


This looks fine. The non-zero exit status from `false` triggers the `zerr` trap. Then the execution of `fun2` resumes and triggers its exit trap when it ends. Same for `fun1`. Finally the execution of the main body resumes and triggers the shell's exit trap and hook when it ends.

Running the same with `ERR_EXIT (-e)` enabled produces a much shorter output:

% zsh -ef test-exit.zsh fun1 fun2 false
Running zerr trap
Running exit trap (fun2)

The `zerr` trap is correctly still triggered by the execution of `false`. The execution of `fun2` is then correctly NOT resumed but its exit trap is correctly still triggered. Then, the shell is exited without triggering `fun1`'s exit trap nor the shell's exit trap and hook. That looks wrong to me. Contrast this to what happens if `false` is replaced with an explicit `exit` command:

% zsh -f test-exit.zsh fun1 fun2 exit  
Running exit trap (fun2)
Running exit trap (fun1)
Running exit trap (main)
Running exit hook


In this case, the result is the same with and without `-e`. The `zerr` trap is not triggered since all commands return 0. More importantly, all exit traps and hooks are triggered. In my opinion, that's exactly what is expected here and I think the same should happen when the exit is triggered by the ERR_EXIT option.

The attached experimental patch fixes the issue by reusing the code for explicit exits for ERR_EXIT triggered exits. With it, I get the following output:

% zsh -ef test-exit.zsh fun1 fun2  false
Running zerr trap
Running exit trap (fun2)
Running zerr trap
Running exit trap (fun1)
Running exit trap (main)
Running exit hook


Here, all the exit traps and hooks are triggered as expected. One thing that is a little strange is that the `zerr` trap is now also triggered in `fun1` but not at the top level, which looks rather inconsistent. I think that it should trigger in both, since both `fun2` and `fun1` end with a non-zero exit status.

All tests still pass with the patch but that's certainly not a guarantee that it's fully correct as these exit scenarios are most likely barely tested.

> Strangely enough, just a few days before your original email, someone else reported a similar issue (workers/55221) where TRAPEXIT fails to execute on interruptions.

I replied to that one, it's repeatable, the "was interrupted" state
persists into the exit trap and aborts it.

That is indeed most certainly why TRAPEXIT isn't executed but it doesn't answer whether TRAPEXIT should be executed. In my opinion, the shell's exit trap, as well as its exit hook, should be executed.
 
On my end the DMARC reject gets overridden due to "trusted_forwarder"
(that's what the headers say) and the message ends up in my list inbox.

And on my end, I have a Gmail filter that says "Never send @zsh.org mails to SPAM" but Gmail still warns me when it considers an email as SPAM.

Philippe


On Wed, Nov 11, 2026 at 7:54 AM Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
Bart Schaefer wrote on Wed, 11 Nov 2026 02:07 +00:00:
> On Tue, Nov 10, 2026 at 4:56 PM Philippe Altherr
> <philippe.altherr@xxxxxxxxx> wrote:
>>
>> I'm not a maintainer, just an occasional contributor, but I think that your original mail was the right way to report the issue. However, for some reason, Gmail classified this email as spam and iirc it was the same for your original email. Maybe that explains the lack of response.
>
> I think this is DMARC related, edp.org has a p=reject policy which is
> tripped when zsh-workers forwards the email without changing the From:
> header.

Actually, it's the opposite problem: edp.org's DKIM signature gets
broken by the addition of List-* headers by the list software, since
edp.org's DKIM-Signature header includes List-* headers in the
signed payload, but those headers get added by zsh.org after
edp.org's DKIM-Signature header had been added.

The DKIM failure brings into play edp.org's DMARC "reject" policy,
which explains why the message gets classified as spam (or discarded
outright).

On my end the DMARC reject gets overridden due to "trusted_forwarder"
(that's what the headers say) and the message ends up in my list inbox.

tl;dr for anyone following along at home: Your spam filters are fine;
the message essentially instructed recipients to reject itself.  (And I
expect Eric knows how to change edp.org's configuration to stop this
from recurring.)

Cheers,

Daniel

diff --git a/Src/exec.c b/Src/exec.c
index b0794abab..021696b08 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -1670,12 +1670,33 @@ sublist_done:
 		    !(noerrexit & NOERREXIT_EXIT);
 		if (errexit) {
 		    errflag = 0;
-		    if (sigtrapped[SIGEXIT])
-			dotrap(SIGEXIT);
-		    if (mypid != getpid())
-			_realexit();
-		    else
-			realexit();
+		    // Inlined code from realexit()
+		    int exit_arg = (shell_exiting || exit_pending) ? exit_val : lastval;
+		    // Inlined code from bin_break/case BIN_EXIT
+		    if (locallevel > forklevel && shell_exiting != -1) {
+			/*
+			 * We don't exit directly from functions to allow tidying
+			 * up, in particular EXIT traps.  We still need to perform
+			 * the usual interactive tests to see if we can exit at
+			 * all, however.
+			 *
+			 * If we are forked, we exit the shell at the function depth
+			 * at which we became a subshell, hence the comparison.
+			 *
+			 * If we are already exiting... give this all up as
+			 * a bad job.
+			 */
+			if (stopmsg || (zexit(0, ZEXIT_DEFERRED), !stopmsg)) {
+			    if (trap_state)
+				trap_state = TRAP_STATE_FORCE_RETURN;
+			    retflag = 1;
+			    breaks = loops;
+			    exit_pending = 1;
+			    exit_level = locallevel;
+			    exit_val = exit_arg;
+			}
+		    } else
+			zexit(exit_arg, ZEXIT_NORMAL);
 		}
 		if (errreturn) {
 		    retflag = 1;


Messages sorted by: Reverse Date, Date, Thread, Author