zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Martijn Dekker <martijn@inlv.org>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: EXIT trap not executed on error
Date: Sun, 11 Dec 2022 20:52:07 -0800	[thread overview]
Message-ID: <CAH+w=7ZBiyGz20aY1_v0Yy9Cvcb1rrdZC6OjQ1u39iG+TECArw@mail.gmail.com> (raw)
In-Reply-To: <1939278e-89c4-812d-5dea-e836568dc100@inlv.org>

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@inlv.org> 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?


  reply	other threads:[~2022-12-12  4:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-08 20:08 Martijn Dekker
2022-12-12  4:52 ` Bart Schaefer [this message]
2022-12-14  4:47   ` Bart Schaefer
2022-12-14  8:35     ` Philippe Altherr
2022-12-15  6:40       ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7ZBiyGz20aY1_v0Yy9Cvcb1rrdZC6OjQ1u39iG+TECArw@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=martijn@inlv.org \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).