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.
Executed when the shell exits, or when the current function exits if defined inside a function.
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