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 not sure -- I can't find anything relevant in the POSIX spec. But it seems to me like the trap should be executed. All the other shells execute the trap. Emulation modes don't seem to make a difference to this behaviour. $ zsh -c 'trap "echo OK" EXIT; : $((1\\2)); echo OOPS' zsh:1: bad math expression: illegal character: \ (output I would expect: that error message followed by 'OK') - M.
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?
[-- Attachment #1: Type: text/plain, Size: 615 bytes --] On Sun, Dec 11, 2022 at 8:52 PM Bart Schaefer <schaefer@brasslantern.com> wrote: > > 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(). > > The question is whether its OK here to just clear errflag like this, > or if it should be saved and restored? Seems reasonable to errflag on the side of safety (ahem) and do the restore. The new first hunk covers another possible case; immediately outside that diff context we call realexit(), so there is no possibility of errflag being referenced again. [-- Attachment #2: special_exit_trap.txt --] [-- Type: text/plain, Size: 666 bytes --] diff --git a/Src/exec.c b/Src/exec.c index 7001fd615..2b7e0c7c5 100644 --- a/Src/exec.c +++ b/Src/exec.c @@ -1598,6 +1598,7 @@ sublist_done: (isset(ERRRETURN) && !errreturn)) && !(noerrexit & NOERREXIT_EXIT); if (errexit) { + errflag = 0; if (sigtrapped[SIGEXIT]) dotrap(SIGEXIT); if (mypid != getpid()) @@ -1630,9 +1631,12 @@ sublist_done: thisjob = cj; if (exiting && sigtrapped[SIGEXIT]) { + int eflag = errflag; + errflag = 0; /* Clear the context for trap */ dotrap(SIGEXIT); /* Make sure this doesn't get executed again. */ sigtrapped[SIGEXIT] = 0; + errflag = eflag; } unqueue_signals();
[-- Attachment #1.1: Type: text/plain, Size: 802 bytes --] I had a look at the errflag logic and admittedly I don't understand everything/much. However I noticed that other <https://github.com/zsh-users/zsh/blob/6d49734d46a66b572cf064f60dac8d9e0ad309d0/Src/exec.c#L1935> places <https://github.com/zsh-users/zsh/blob/6d49734d46a66b572cf064f60dac8d9e0ad309d0/Src/exec.c#L4598> that restore the errflag preserve the ERRFLAG_INT bit. Maybe the same should be done here, even though it may not be that important as we are on the exit path anyway. I also wondered whether signals.c/dotraps <https://github.com/zsh-users/zsh/blob/6d49734d46a66b572cf064f60dac8d9e0ad309d0/Src/signals.c#L1456> would be a better place for the new logic. It already has special logic for SIGEXIT and this way the new logic would be guaranteed to apply to all runs of SIGEXIT. Philippe [-- Attachment #1.2: Type: text/html, Size: 954 bytes --] [-- Attachment #2: patch-run-exit-trap-also-on-error.txt --] [-- Type: text/plain, Size: 1437 bytes --] diff --git a/Src/signals.c b/Src/signals.c index a61368554..684394520 100644 --- a/Src/signals.c +++ b/Src/signals.c @@ -1306,9 +1306,7 @@ dotrapargs(int sig, int *sigtr, void *sigfn) * function will test for this, but this way we keep status flags * * intact without working too hard. Special cases (e.g. calling * * a trap for SIGINT after the error flag was set) are handled * - * by the calling code. (PWS 1995/06/08). * - * * - * This test is now replicated in dotrap(). */ + * by the calling code. (PWS 1995/06/08). */ if ((*sigtr & ZSIG_IGNORED) || !sigfn || errflag) return; @@ -1471,23 +1469,20 @@ dotrap(int sig) } else funcprog = siglists[sig]; - /* - * Copied from dotrapargs(). - * (In fact, the gain from duplicating this appears to be virtually - * zero. Not sure why it's here.) - */ - if ((sigtrapped[sig] & ZSIG_IGNORED) || !funcprog || errflag) - return; - dont_queue_signals(); - if (sig == SIGEXIT) + int prev_errflag = errflag; + if (sig == SIGEXIT) { ++in_exit_trap; + errflag = 0; + } dotrapargs(sig, sigtrapped+sig, funcprog); - if (sig == SIGEXIT) + if (sig == SIGEXIT) { + errflag = prev_errflag | (errflag & ERRFLAG_INT); --in_exit_trap; + } restore_queue_signals(q); }
On Wed, Dec 14, 2022 at 12:35 AM Philippe Altherr <philippe.altherr@gmail.com> wrote: > > I had a look at the errflag logic and admittedly I don't understand everything/much. However I noticed that other places that restore the errflag preserve the ERRFLAG_INT bit. IMO that's irrelevant ... no trap will run when errflag is set, and in theory at least the exit trap is the last thing we're going to do, so remembering that we got an interrupt during the trap doesn't mean much. zexit() actually goes so far as to clear errflag twice and to clear intrap as well the second time. I'm actually pretty sure saving and restoring errflag is not needed at all. > I also wondered whether signals.c/dotraps would be a better place for the new logic. I considered that, but saw dotraps being re-entered while stepping through things with the debugger, so I thought it better to stay with doing changes in the caller.