Re: `wait` triggers an infinite loop when used in an EXIT trap

Chet Ramey <[email protected]>
Newsgroups gmane.comp.shells.bash.bugs
Organization ITS, Case Western Reserve University
Message-ID <[email protected]>
On 1/5/26 12:42 PM, Félix Hauri via Bug reports for the GNU Bourne Again 
SHell wrote:
> Things seem a little more subtle:

Not really. It's pretty much exactly what I put in my reply.


> Run on Debian Trixie (bash 5.2.15(1)-release), when `trap` is defined
> before `true` commands, I often (but not always) see:

Because the last asynchronous child inherits the exit trap, the list of
background pids, and $!, and does not reset the exit trap before it is
killed by its parent.


> Where `cleanup` was run two times! Seem to be trigger by one of `true`
> command's EXIT signal.

Yes, that's what this paragraph means:

"The child process, which has an active EXIT trap, has inherited the jobs
array, the list of background processes, and the value of $! from its
parent. When it runs its EXIT trap, it will try to wait for all of the
background jobs it inherited, none of which are its children. You should
get at least one error message when this happens."


> When run on 5.3.9(1)-release, I see sometime:
> 
>      declare -a jobs=([0]="254729" [1]="254730" [2]="254731" [3]="254732" [4]="254733")
>      PID: 254727 CMD: declare -p jobs
>      trapChilds.sh: line 7: kill: (254729) - No such process
>      trapChilds.sh: line 7: kill: (254730) - No such process
>      trapChilds.sh: line 7: kill: (254731) - No such process
>      trapChilds.sh: line 7: kill: (254732) - No such process
>      trapChilds.sh: line 7: kill: (254733) - No such process

This is when the child process runs before its parent and clears the EXIT
trap it inherits before it receives SIGTERM.


> But some other time:
> 
>      declare -a jobs=([0]="254707" [1]="254708" [2]="254709" [3]="254710" [4]="254711")
>      PID: 254705 CMD: declare -p jobs
>      trapChilds.sh: line 7: kill: (254707) - No such process
>      trapChilds.sh: line 7: kill: (254710) - No such process
>      PID: 254705 CMD: true
>      PID: 254705 CMD: true
>      trapChilds.sh: line 7: kill: (254707) - No such process
>      trapChilds.sh: line 7: kill: (254707) - No such process
>      trapChilds.sh: line 7: kill: (254710) - No such process
>      trapChilds.sh: line 8: wait: pid 254708 is not a child of this shell
>      trapChilds.sh: line 8: wait: pid 254708 is not a child of this shell
>      trapChilds.sh: line 8: wait: pid 254708 is not a child of^C
> 
> I had to hit Ctrl+C!

That's what this paragraph means:

"The loop happens because it keeps trying to wait for these processes that
are not its children. I'll have to figure out why that's happening --
probably because it's trying to wait for "all background pids" and not
doing the right error checks."

> 
> Note: If the trap is defined AFTER last `true` was ran, everything work fine.

Because the child process doesn't inherit an EXIT trap.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    [email protected]    http://tiswww.cwru.edu/~chet/
OpenPGP_signature.asc (application/pgp-signature, 203 B)
-----BEGIN PGP SIGNATURE-----

wmMEABEIACMWIQR8ATX7CIqvbGbGULm7WGnwZOp0qwUCaVwD9AUDAAAAAAAKCRC7WGnwZOp0q3sG
AJ4gGYPwR3aL3b6sMCQSqfsoQxmfNwCfThLivqX11g3IFOGaeo8rxg/+xM0=
=bIFW
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.