help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] & affect the order of EXIT trap executed in subshells


From: Chet Ramey
Subject: Re: [Help-bash] & affect the order of EXIT trap executed in subshells
Date: Fri, 21 Dec 2018 14:27:30 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Thunderbird/60.3.3

On 12/18/18 7:07 PM, Peng Yu wrote:
> Hi,
> 
> But just adding '&' and `wait`, the order of execution of EXIT traps
> are changed.

It's no mystery; it's a straightforward application of basic rules.

> I am not sure that this is a good design as it is more intuitive if
> the behavior is preserved as much as possible with such a minor change
> (as the main purpose of & is to run processes in background it should
> not touch how signals are handle as much as possible).
> 
> Given that normal background processes won't get the INT signal, it
> seems to be special treatment for subshell for which to get the signal
> later.

This doesn't make any sense. Think about it in terms of the effect the
SIGINT has on the EXIT2 process.

Asynchronous processes ignore SIGINT and SIGQUIT. That's basic shell
behavior dating back to the Bourne shell, and POSIX requires it. I'm not
sure you can say that using `&' "should not touch how signals are handled
as much as possible."

So your first example is run synchronously, gets the SIGINT, and exits due
to it, running its exit trap along the way. Its parent waits for it to
exit, notes that it died due to SIGINT, resends the SIGINT to itself, and
exits due to it, running its exit trap along the way.

In the second case, the background process doesn't get SIGINT, and exits
when the sleep completes, running the exit trap on the way out. Its parent
does get the SIGINT, which interrupts the wait and causes the shell to
exit, running its own exit trap. Depending on when you send SIGINT, it
probably happens well before the sleep completes.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://tiswww.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]