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/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to