[Top][All Lists]

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

Re: 5.0 regression: Script stuck when waiting in trap

From: Chet Ramey
Subject: Re: 5.0 regression: Script stuck when waiting in trap
Date: Mon, 3 Jun 2019 15:42:22 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.7.0

On 6/2/19 7:55 AM, mwnx wrote:

> Bash Version: 5.0
> Patch Level: 3
> Release Status: release
> Description:
>       Since bash 5.0, a subshell can get stuck (wait forever) in
>       what looks like a pretty specific set of circumstances,
>       namely when combining a group command or a function call
>       with process substitution and attempting to `wait` for said
>       group command or function from within a trap.

> Repeat-By:
>       $ cat <<'EOF' | bash
>       (
>           trap 'wait' EXIT
>           { sleep 2; } > >(cat)
>       ) &
>       sleep 1
>       kill $!
>       wait
>       EOF
>       <ctrl-C after a few seconds of waiting>
>       # The process is actually still alive, even after ctrl-C...
>       $ pstree -p
>       sh(1)-+-bash(6)---pstree(321)
>             `-bash(316)---bash(318)---cat(320)

Here's what happens. The relevant change is that wait without options now
waits for the last process substitution, since that sets $! and is "known"
to the shell.

The sequence of events is approximately:

1. Subshell starts, forks to run process substitution, opens a pipe to the
   process substitution, and runs the group command. The group command
   means the redirection is performed by the subshell, not the `sleep',
   since the redirections persist for the entire group command.

2. Main shell starts, runs sleep, kills the subshell started in step 1.
   This doesn't kill the sleep or the cat. The sleep exits on its own.
   The cat continues to run.

3. The main shell waits for the subshell.

4. The subshell, having received a fatal signal, runs the exit trap and
   waits for the process substitution ($!). It doesn't have a chance to do
   anything with the the file descriptor open to the process substitution
   as the result of the redirection, and the `cat' continues to run because
   it doesn't get the signal. The wait doesn't complete.


``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]