kill the whole image job pool on INT and TERM

Track the pool PIDs and their descendants, TERM then KILL them, and
exit 130/143. Restore the caller's traps afterwards, since soup
installs its own EXIT and ERR handlers.
This commit is contained in:
reyesj2 committed 2026-11-29 17:51:03 -06:00
1 parent eb3ff80154
commit dd4165fa56
1 file changed
+48 -4
+48 -4
View File
@@ -10,6 +10,9 @@
# NOTE: This script depends on so-common
IMAGEREPO=security-onion-solutions
STATUS_CONF='/opt/so/conf/so-status/so-status.conf'
# PIDs of the current launch_pool children, so the INT/TERM trap can kill them.
POOL_PIDS=()
_pool_tree=()
# shellcheck disable=SC2120
container_list() {
@@ -330,6 +333,38 @@ log_downloading() {
echo "Downloading $1" >> "$LOG_FILE" 2>&1
}
# Kill a pool job and everything it spawned: in a non-interactive shell the
# background docker/curl children ignore SIGINT, so killing only the subshell
# would leave them writing to $SIGNPATH. Only PIDs the pool started are walked.
pool_collect_tree() { # pool_collect_tree <pid>; appends to _pool_tree
local pid=$1
local child
_pool_tree+=("$pid")
while IFS= read -r child; do
if [ -n "$child" ]; then
pool_collect_tree "$child"
fi
done < <(pgrep -P "$pid" 2>/dev/null)
}
_pool_abort() { # _pool_abort <exit code>
local code=$1
local pid
trap '' INT TERM
_pool_tree=()
for pid in "${POOL_PIDS[@]}"; do
pool_collect_tree "$pid"
done
for pid in "${_pool_tree[@]}"; do
kill -TERM "$pid" 2>/dev/null || true
done
sleep 2
for pid in "${_pool_tree[@]}"; do
kill -KILL "$pid" 2>/dev/null || true
done
exit "$code"
}
# Only wait on PIDs started here: soup may run its own background jobs in this
# shell (e.g. the grid highstate), and a bare `wait` would block on those.
launch_pool() {
@@ -338,8 +373,14 @@ launch_pool() {
local jobs=$3
local hook=$4
shift 4
local -a active=() pids=() still=()
local -a active=() still=()
local img pid
POOL_PIDS=()
local saved_int saved_term
saved_int=$(trap -p INT)
saved_term=$(trap -p TERM)
trap '_pool_abort 130' INT
trap '_pool_abort 143' TERM
for img in "$@"; do
if [ -n "$hook" ]; then
"$hook" "$img"
@@ -349,7 +390,7 @@ launch_pool() {
echo $? >"$SIGNPATH/$img.$phase.rc"
) &
pid=$!
pids+=("$pid")
POOL_PIDS+=("$pid")
active+=("$pid")
while [ ${#active[@]} -ge "$jobs" ]; do
wait -n "${active[@]}" 2>/dev/null || true
@@ -362,9 +403,12 @@ launch_pool() {
active=("${still[@]}")
done
done
if [ ${#pids[@]} -gt 0 ]; then
wait "${pids[@]}" 2>/dev/null || true
if [ ${#POOL_PIDS[@]} -gt 0 ]; then
wait "${POOL_PIDS[@]}" 2>/dev/null || true
fi
# Restore whatever the caller had; soup installs its own EXIT/ERR handlers.
eval "${saved_int:-trap - INT}"
eval "${saved_term:-trap - TERM}"
}
update_docker_containers() {