On 1/2/26 2:23 PM, Aleksey Covacevice wrote:

Bash Version: 5.3
Patch Level: 8
Release Status: release

Description:
     `wait` triggers an infinite "pid is not a child of this shell" loop when
     used in an EXIT trap.

Repeat-By:
     Run the following:

         jobs=()

         cleanup() {
             kill -- "${jobs[@]}"
             wait
         }

         trap cleanup EXIT

         true & jobs+=($!)
         true & jobs+=($!)
         true & jobs+=($!)
         true & jobs+=($!)
         true & jobs+=($!)

     Ocasionally bash loops at `wait` claiming some "pid is not a child of
     this shell". (It doesn't happen if `kill` is not used.)

Consider this sequence of events. The parent shell forks the last
asynchronous process and adds it to the jobs array (setting the
value of $! and adding it to the list of background jobs the shell
maintains). It then exits and runs its exit trap before the child process
runs and resets its EXIT trap handler. The parent shell, as part of its
EXIT trap, kills the last asynchronous child, which still has an active
EXIT trap, with a SIGTERM. Since the shell is not interactive, SIGTERM is
a fatal signal. That asynchronous child shell exits, running its EXIT trap.

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.

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.

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