@@ -6,9 +6,11 @@ wrapper_pids=()
66
77# Function to count active shell sessions launched by wrapper
88# Expensive, so run sparingly. Only needed to find new shells when they launch.
9- count_shells () {
9+ shells_are_running () {
10+ # As a side effect, update the list of running shells
1011 wrapper_pids=($( list-wrapper-shells) )
11- echo " ${# wrapper_pids[@]} "
12+ # Return true if there are any shells running
13+ [[ " ${# wrapper_pids[@]} " -gt 0 ]]
1214}
1315
1416# Function to check if any registered shell has exited.
@@ -31,25 +33,25 @@ shell_has_exited() {
3133# Function to kill all active shell sessions launched by wrapper.
3234# This is the shutdown procedure, so we do not care about hogging the CPU.
3335kill_shells () {
34- for pid in $( list_wrapper_shells ) ; do
36+ for pid in $( list-wrapper-shells ) ; do
3537 kill -HUP $pid
3638 done
3739
3840 for i in {1..4}; do
39- [ $( count_shells ) -eq 0 ] && return 0
41+ shells_are_running || return 0
4042 sleep 1
4143 done
4244
43- for pid in $( list_wrapper_shells ) ; do
45+ for pid in $( list-wrapper-shells ) ; do
4446 kill -TERM $pid
4547 done
4648
4749 for i in {1..3}; do
48- [ $( count_shells ) -eq 0 ] && return 0
50+ shells_are_running || return 0
4951 sleep 1
5052 done
5153
52- for pid in $( list_wrapper_shells ) ; do
54+ for pid in $( list-wrapper-shells ) ; do
5355 kill -KILL $pid
5456 done
5557
@@ -62,7 +64,7 @@ trap 'kill_shells; exit $?' TERM HUP INT QUIT EXIT
6264# Since we are waiting for something to happen, we can afford burn
6365# up some CPU in order to be more responsive.
6466i=0
65- while [ $( count_shells ) -eq 0 ] ; do
67+ while ! shells_are_running ; do
6668 sleep 0.5
6769 i=$(( i + 1 ))
6870 if [ $i -ge 120 ]; then
@@ -87,7 +89,7 @@ while true; do
8789 while ! shell_has_exited; do
8890 sleep 0.67
8991 done
90- [[ $( count_shells ) -eq 0 ]] && break
92+ shells_are_running || break
9193done
9294
9395# Clean up and exit
0 commit comments