improve handling / killing of hanging background procs

This commit is contained in:
2024-11-02 01:07:11 +01:00
parent ce2daf9749
commit 6ec4c02e40

View File

@@ -566,7 +566,7 @@ declare -A pidtable
first_run=true first_run=true
while :; do while :; do
# Check if main is still alive and exit otherwise. # Check if main is still alive and exit otherwise.
ps -p $main_pid > /dev/null || break ps -p $main_pid >/dev/null || break
[ -v EPOCHSECONDS ] && now=$EPOCHSECONDS || now=$(date +%s) [ -v EPOCHSECONDS ] && now=$EPOCHSECONDS || now=$(date +%s)
for func in "${!DATA_FUNCS[@]}"; do for func in "${!DATA_FUNCS[@]}"; do
@@ -600,16 +600,20 @@ while :; do
fi fi
data=$(timeout 1 cat <&$fd) data=$(timeout 1 cat <&$fd)
rc=$? rc=$?
eval "exec $fd>&-"
fdtable[$func]=-1
pid=${pidtable[$func]} pid=${pidtable[$func]}
if (( rc == 124 )); then if (( rc == 124 )); then
echo "gathering: timeout receiving data from $func (PID $pid, FD $fd), killing process" >&2 echo "gathering: timeout receiving data from $func (PID $pid, FD $fd), killing process" >&2
kill -9 $pid kill -SIGTERM $pid
fi sleep 1
ps -p $pid >/dev/null && kill -SIGKILL $pid
rc=137
else
wait $pid wait $pid
rc=$? rc=$?
echo "gathering: $func exited (rc = $rc)" >&$DEBUGLOG fi
eval "exec $fd>&-" echo "gathering: $func (PID $pid, FD $fd) exited with rc = $rc" >&$DEBUGLOG
fdtable[$func]=-1
if (( rc == 0 )) && [ -n "$data" ]; then if (( rc == 0 )) && [ -n "$data" ]; then
echo "gathering: sending data to main" >&$DEBUGLOG echo "gathering: sending data to main" >&$DEBUGLOG
echo "$data" 1>&$DATAIN echo "$data" 1>&$DATAIN