zsh-users
 help / color / mirror / code / Atom feed
* TRAPEXIT doesn't get executed
@ 2000-11-07 18:35 Matt Armstrong
  2000-11-07 19:07 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Matt Armstrong @ 2000-11-07 18:35 UTC (permalink / raw)
  To: zsh-users

I'm trying to find the right idiom to make sure some cleanup code gets
executed no matter how the script exits.

When I run this script:
  
---------------------------------------------------------
#!/usr/bin/zsh
  
set -x
  
function TRAPEXIT {
    print "executed TRAPEXIT"
}
sleep 10
---------------------------------------------------------
  
And hit Ctrl-C while "sleep 10" is executing, the TRAPEXIT isn't
executed.  But when I run this script:
  
---------------------------------------------------------
#!/usr/bin/zsh
  
set -x

function TRAPEXIT {
    print "executed TRAPEXIT"
}
function TRAPINT {
    print "executed TRAPINT"
}
sleep 10
---------------------------------------------------------

And hit Ctrl-C, both TRAPINT and TRAPEXIT get executed.  Is this
explained anywhere?  The behavior is the same under 3.0 and 3.1, so I
imagine this has been covered before.

-- 
matt


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: TRAPEXIT doesn't get executed
  2000-11-07 18:35 TRAPEXIT doesn't get executed Matt Armstrong
@ 2000-11-07 19:07 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2000-11-07 19:07 UTC (permalink / raw)
  To: Matt Armstrong, zsh-users

On Nov 7, 10:35am, Matt Armstrong wrote:
> Subject: TRAPEXIT doesn't get executed
>   
> ---------------------------------------------------------
> #!/usr/bin/zsh
>   
> set -x
>   
> function TRAPEXIT {
>     print "executed TRAPEXIT"
> }
> sleep 10
> ---------------------------------------------------------
>   
> And hit Ctrl-C while "sleep 10" is executing, the TRAPEXIT isn't
> executed.

This is a bit subtle, and I'm not entirely sure zsh is doing the right thing,
but what's happening here is that because zsh is running a script (rather
than interactively), it has not ignored SIGINT.  So the SIGINT is actually
being received by zsh and the c-runtime-library default SIGINT handler is
being called, which kills the process; there's no opportunity for zsh to run
the exit trap.

This is done in part so that shell loops that execute nothing but builtin
commands can nevertheless be interrupted by signals.  However, it might be
that zsh should trap SIGINT anyway and simply respond to it by exiting.

> ---------------------------------------------------------
> #!/usr/bin/zsh
>   
> set -x
> 
> function TRAPEXIT {
>     print "executed TRAPEXIT"
> }
> function TRAPINT {
>     print "executed TRAPINT"
> }
> sleep 10
> ---------------------------------------------------------
> 
> And hit Ctrl-C, both TRAPINT and TRAPEXIT get executed.

In this case, you've explicitly overridden the default SIGINT handler by
declaring a SIGINT trap function, so zsh *doesn't* exit on the SIGINT;
instead it exits by falling off the end after "sleep" is killed (by that
same signal -- the tty driver delivers it to all the processes in the tty
process group).  Put another "print" after the sleep in both scripts and
try again, and you'll see that in the second case the script is still
running after the interrupt, but in the first case it's not.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-11-07 19:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-07 18:35 TRAPEXIT doesn't get executed Matt Armstrong
2000-11-07 19:07 ` Bart Schaefer

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).