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

Re: EXIT trap not executed on error



Had this sitting on my TODO list for four years.  Mentioned it in a
recent thread about ERR_EXIT handling.

On Sat, Dec 8, 2018 at 12:23 PM Martijn Dekker <martijn@xxxxxxxx> wrote:
>
> When zsh exits due to an error in a special builtin (e.g. 'set -o
> bad@option') or in an arithmetic expression in $((...)), the EXIT trap
> is not executed.
>
> Is this a bug?

I'm going to have to say "yes."

Because this is "zsh -c", we come through this bit of execlist():

   1632     if (exiting && sigtrapped[SIGEXIT]) {
   1633         dotrap(SIGEXIT);
   1634         /* Make sure this doesn't get executed again. */
   1635         sigtrapped[SIGEXIT] = 0;
   1636     }

That brings us to this bit of signals.c with errflag == ERRFLAG_ERROR:

1302     /* if signal is being ignored or the trap function                *
1303      * is NULL, then return                                           *
1304      *                                                                *
1305      * Also return if errflag is set.  In fact, the code in the       *
1306      * function will test for this, but this way we keep status flags *
1307      * intact without working too hard.  Special cases (e.g. calling  *
1308      * a trap for SIGINT after the error flag was set) are handled    *
1309      * by the calling code.  (PWS 1995/06/08).                        *
1310      *                                                                *
1311      * This test is now replicated in dotrap().                       */
1312     if ((*sigtr & ZSIG_IGNORED) || !sigfn || errflag)
1313         return;

I don't recall why this suppresses traps on errflag, if I ever knew.
However, the comment suggests the calling code should be clearing
errflag around the dotrap().

Indeed, this seems to fix at least this case:

diff --git a/Src/exec.c b/Src/exec.c
index a1059af5e..57a1eaa1d 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -1630,6 +1630,7 @@ sublist_done:
        thisjob = cj;

     if (exiting && sigtrapped[SIGEXIT]) {
+       errflag = 0;
        dotrap(SIGEXIT);
        /* Make sure this doesn't get executed again. */
        sigtrapped[SIGEXIT] = 0;

The question is whether its OK here to just clear errflag like this,
or if it should be saved and restored?




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