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



On Wed, Nov 11, 2026 at 3:26 AM Eric Postpischil
<ericpostpischil@xxxxxxxxx> wrote:
>
> On Nov 10, 2026, at 21:07, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> > 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.
>
> Thanks, I have switched to a Gmail account.
>
> > I haven't dug in myself but I think this is actually expected
> > behavior?  Return from a function is not a shell exit?  There should
> > be some archive messages related to the handling of ERR_EXIT vs.
> > ERR_RETURN.
>
> 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?
>
> If we execute this:
>
> #!/bin/zsh -ef
> TRAPEXIT() { echo "This should be printed but is not." }
> () { false }
> echo "Completed function."
>
> Then “Completed function” is not printed. That lack indicates the function did not return normally; execution of the script was terminated without continuing after the function execution.

#!/bin/zsh -ef
setopt posixtraps
TRAPEXIT() { echo "This should be printed but is not." }
() { false }
echo "Completed function."

Someone else might be better able to explain the interaction here, but
basically without posixtraps, your TRAPEXIT is not active within the
anonymous function, and by the time the parent context would have seen
the false return and ran it before exiting, we have in fact already
exited the shell, so that never happens.

-- 
Mikael Magnusson




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