Clean stale local debug run processes
This commit is contained in:
+66
-11
@@ -5,6 +5,70 @@ ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
||||
# shellcheck source=scripts/local-debug/env.sh
|
||||
source "$ROOT_DIR/scripts/local-debug/env.sh"
|
||||
|
||||
wait_for_pid_exit() {
|
||||
local pid="$1"
|
||||
for _ in $(seq 1 20); do
|
||||
if ! kill -0 "$pid" 2>/dev/null; then
|
||||
return 0
|
||||
fi
|
||||
sleep 0.25
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
descendant_pids() {
|
||||
local pid="$1"
|
||||
local child
|
||||
while IFS= read -r child; do
|
||||
[[ -n "$child" ]] || continue
|
||||
printf '%s\n' "$child"
|
||||
descendant_pids "$child"
|
||||
done < <(pgrep -P "$pid" 2>/dev/null || true)
|
||||
}
|
||||
|
||||
stop_pid_tree() {
|
||||
local name="$1"
|
||||
local root_pid="$2"
|
||||
local pids
|
||||
local pid
|
||||
local forced=0
|
||||
|
||||
[[ "$root_pid" =~ ^[0-9]+$ ]] || return 0
|
||||
pids="$(descendant_pids "$root_pid"; printf '%s\n' "$root_pid")"
|
||||
for pid in $pids; do
|
||||
kill "$pid" 2>/dev/null || true
|
||||
done
|
||||
for pid in $pids; do
|
||||
if ! wait_for_pid_exit "$pid"; then
|
||||
forced=1
|
||||
fi
|
||||
done
|
||||
if [[ "$forced" -eq 1 ]]; then
|
||||
printf '%s process tree rooted at pid %s did not stop after SIGTERM; sending SIGKILL\n' "$name" "$root_pid"
|
||||
for pid in $pids; do
|
||||
kill -9 "$pid" 2>/dev/null || true
|
||||
done
|
||||
for pid in $pids; do
|
||||
wait_for_pid_exit "$pid" || true
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
stop_matching_processes() {
|
||||
local name="$1"
|
||||
local pattern="$2"
|
||||
local pid
|
||||
[[ -n "$pattern" ]] || return 0
|
||||
while IFS= read -r pid; do
|
||||
[[ "$pid" =~ ^[0-9]+$ ]] || continue
|
||||
[[ "$pid" != "$$" ]] || continue
|
||||
if kill -0 "$pid" 2>/dev/null; then
|
||||
printf 'stopping stale %s pid %s\n' "$name" "$pid"
|
||||
stop_pid_tree "$name" "$pid"
|
||||
fi
|
||||
done < <(pgrep -f "$pattern" 2>/dev/null || true)
|
||||
}
|
||||
|
||||
if [[ ! -d "$LOCAL_DEBUG_PID_DIR" ]]; then
|
||||
printf 'no local debug pid directory at %s\n' "$LOCAL_DEBUG_PID_DIR"
|
||||
else
|
||||
@@ -22,7 +86,7 @@ else
|
||||
pid="$(<"$pid_file")"
|
||||
if kill -0 "$pid" 2>/dev/null; then
|
||||
printf 'stopping %s pid %s\n' "$name" "$pid"
|
||||
kill "$pid" 2>/dev/null || true
|
||||
stop_pid_tree "$name" "$pid"
|
||||
else
|
||||
printf '%s pid %s is not running\n' "$name" "$pid"
|
||||
fi
|
||||
@@ -30,16 +94,7 @@ else
|
||||
done
|
||||
fi
|
||||
|
||||
wait_for_pid_exit() {
|
||||
local pid="$1"
|
||||
for _ in $(seq 1 20); do
|
||||
if ! kill -0 "$pid" 2>/dev/null; then
|
||||
return 0
|
||||
fi
|
||||
sleep 0.25
|
||||
done
|
||||
return 1
|
||||
}
|
||||
stop_matching_processes "run bootstrap" "$RUN_BOOTSTRAP_BIN"
|
||||
|
||||
listener_owned_by_local_debug() {
|
||||
local pid="$1"
|
||||
|
||||
Reference in New Issue
Block a user