Clean stale local debug run processes

This commit is contained in:
npc0-hue
2026-08-06 21:46:02 +08:00
parent 35694613e5
commit d28e998411
2 changed files with 121 additions and 11 deletions
+55
View File
@@ -60,6 +60,60 @@ wait_for_pid_exit() {
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_orphaned_run_bootstrap() {
local pid_file="$LOCAL_DEBUG_PID_DIR/run.pid"
local pid
if managed_pid_running "$pid_file"; then
return 0
fi
while IFS= read -r pid; do
[[ "$pid" =~ ^[0-9]+$ ]] || continue
[[ "$pid" != "$$" ]] || continue
if kill -0 "$pid" 2>/dev/null; then
printf 'stopping stale run bootstrap pid %s before start\n' "$pid"
stop_pid_tree "run bootstrap" "$pid"
fi
done < <(pgrep -f "$RUN_BOOTSTRAP_BIN" 2>/dev/null || true)
}
ensure_port_available() {
local name="$1"
local port="$2"
@@ -228,6 +282,7 @@ start_service platform_web "$ROOT_DIR" env \
wait_for_url platform_web "$(local_debug_web_url)"
stop_orphaned_run_bootstrap
local_debug_build_bootstrap_run
start_service run "$(dirname "$RUN_BOOTSTRAP_BIN")" env \
GOCACHE="$GOCACHE" \