Hello,
It's possible to have the following script never terminate: #!/bin/bash subfun() { trap "echo received term for $*;exit 1" SIGTERM echo my pid $BASHPID i=0 while true; do echo $BASHPID subfun $* $i i=$((i+1)) sleep 5 done } trap 'echo main; exit 1' 0 1 2 3 15 subfun xxx & funpid=$! subfun yyy & twopid=$! echo funpid=$funpid twopid=$twopid mypid=$$ echo killing $funpid $twopid kill $funpid $twopid echo waiting on everything wait echo done The output is: my pid 12186 12186 subfun xxx 0 funpid=12186 twopid=12187 mypid=12185 killing 12186 12187 waiting on everything my pid 12187 12187 subfun yyy 0 received term for xxx 12187 subfun yyy 1 12187 subfun yyy 2 12187 subfun yyy 3 and 12187 keeps printing. It would seem there $! can return a pid of the subshell before it's in a state that it can be killed. This has to do with setting of the trap handler, because if the trap handler of the main shell is changed to : trap 'echo main; exit 1' EXIT - Then it never hangs. I.e the more trap handlers that subshells have to reset to their defaults the higher the chances this race condition is hit. Also if I change the sleep in the subshell function to anything below 5 I also seem to be unable to hit it. Also sometimes when it doesn't hang I get the following output: my pid 12283 12283 subfun xxx 0 funpid=12283 twopid=12284 mypid=12282 killing 12283 12284 waiting on everything received term for xxx done main It misses a "Received term for yyy" line. I reproduced this behavior both with version 4.4.20(1)-release (that comes with ubuntu 18.04) as well as 5.0.18(1)-release built from source. Any ideas what might be causing it?