Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Supervise managed dev runtime restarts

rcourtmandd818164d6fc
diff --git a/docs/release-control/v6/internal/subsystems/deployment-installability.md b/docs/release-control/v6/internal/subsystems/deployment-installability.md
index bdf86d730a..b3b0431026 100644
--- a/docs/release-control/v6/internal/subsystems/deployment-installability.md
+++ b/docs/release-control/v6/internal/subsystems/deployment-installability.md
@@ -233,6 +233,12 @@ That runtime-guidance rule also applies to successful launcher startup output:
`hot-dev-bg` must identify `http://127.0.0.1:5173` as the browser entrypoint
and present `7655` as the managed backend dependency, rather than advertising
frontend and backend URLs as if they were equal browser targets.
+The managed repo-root `npm run dev` path must also be self-healing at the
+launcher layer: `hot-dev-bg` may not treat a detached `hot-dev.sh` child as
+sufficient management. The default managed runtime must supervise that child
+and restart it when the owner process exits unexpectedly, so a killed or wedged
+foreground owner does not leave both `5173` and `7655` down until a human
+notices.
The same wrapper-first rule applies to launcher help text: `hot-dev-bg` usage
output must present the repo-root npm entrypoints first and reserve raw
subcommands as secondary script-local controls for direct troubleshooting.
diff --git a/scripts/hot-dev-bg.sh b/scripts/hot-dev-bg.sh
index 8540a4f641..2242077e8a 100755
--- a/scripts/hot-dev-bg.sh
+++ b/scripts/hot-dev-bg.sh
@@ -425,6 +425,110 @@ require_python() {
command -v python3 >/dev/null 2>&1 || fail "python3 is required"
}
+start_hot_dev_child() {
+ FRONTEND_DEV_HOST="${FRONTEND_DEV_HOST}" \
+ FRONTEND_DEV_PORT="${FRONTEND_DEV_PORT}" \
+ PULSE_DEV_API_HOST="${PULSE_DEV_API_HOST}" \
+ PULSE_DEV_API_PORT="${PULSE_DEV_API_PORT}" \
+ PULSE_DEV_API_URL="${PULSE_DEV_API_URL}" \
+ PULSE_DEV_WS_URL="${PULSE_DEV_WS_URL}" \
+ ROOT_DIR="${ROOT_DIR}" \
+ "${ROOT_DIR}/scripts/hot-dev.sh" &
+ HOT_DEV_CHILD_PID="$!"
+}
+
+stop_hot_dev_child() {
+ local child_pid="${1:-}"
+ local retries=30
+
+ [[ -n "${child_pid}" ]] || return 0
+ kill -TERM "${child_pid}" 2>/dev/null || true
+
+ while (( retries > 0 )); do
+ if ! kill -0 "${child_pid}" 2>/dev/null; then
+ return 0
+ fi
+ sleep 0.5
+ retries=$((retries - 1))
+ done
+
+ kill -KILL "${child_pid}" 2>/dev/null || true
+}
+
+run_supervised_session() {
+ local restart_delay="${HOT_DEV_BG_SUPERVISOR_RESTART_DELAY:-1}"
+ local child_pid=""
+ local child_exit=0
+ local shutdown_requested="false"
+
+ supervisor_stop() {
+ shutdown_requested="true"
+ if [[ -n "${child_pid}" ]] && kill -0 "${child_pid}" 2>/dev/null; then
+ stop_hot_dev_child "${child_pid}"
+ wait "${child_pid}" 2>/dev/null || true
+ fi
+ exit 0
+ }
+
+ trap supervisor_stop TERM INT HUP
+
+ while true; do
+ HOT_DEV_CHILD_PID=""
+ start_hot_dev_child
+ child_pid="${HOT_DEV_CHILD_PID:-}"
+ [[ -n "${child_pid}" ]] || fail "Managed runtime supervisor failed to start hot-dev"
+ log "Supervisor started hot-dev child (pid: ${child_pid})"
+
+ if wait "${child_pid}"; then
+ child_exit=0
+ else
+ child_exit=$?
+ fi
+
+ if [[ "${shutdown_requested}" == "true" ]]; then
+ exit 0
+ fi
+
+ log "Managed runtime child exited unexpectedly (pid: ${child_pid}, exit: ${child_exit}). Restarting..."
+ sleep "${restart_delay}"
+ done
+}
+
+spawn_detached_supervisor() {
+ FRONTEND_DEV_HOST="${FRONTEND_DEV_HOST}" \
+ FRONTEND_DEV_PORT="${FRONTEND_DEV_PORT}" \
+ PULSE_DEV_API_HOST="${PULSE_DEV_API_HOST}" \
+ PULSE_DEV_API_PORT="${PULSE_DEV_API_PORT}" \
+ PULSE_DEV_API_URL="${PULSE_DEV_API_URL}" \
+ PULSE_DEV_WS_URL="${PULSE_DEV_WS_URL}" \
+ ROOT_DIR="${ROOT_DIR}" \
+ LOG_FILE="${LOG_FILE}" \
+ HOT_DEV_BG_SCRIPT="${ROOT_DIR}/scripts/hot-dev-bg.sh" \
+ python3 - <<'PY'
+import os
+import subprocess
+
+root_dir = os.environ["ROOT_DIR"]
+log_file = os.environ["LOG_FILE"]
+cmd = [os.environ["HOT_DEV_BG_SCRIPT"], "supervise"]
+
+env = os.environ.copy()
+
+with open(log_file, "ab", buffering=0) as log:
+ proc = subprocess.Popen(
+ cmd,
+ cwd=root_dir,
+ stdin=subprocess.DEVNULL,
+ stdout=log,
+ stderr=subprocess.STDOUT,
+ start_new_session=True,
+ env=env,
+ )
+
+print(proc.pid)
+PY
+}
+
start_bg() {
local takeover="${1:-false}"
require_python
@@ -466,49 +570,17 @@ start_bg() {
fi
local pid
- pid="$(
- FRONTEND_DEV_HOST="${FRONTEND_DEV_HOST}" \
- FRONTEND_DEV_PORT="${FRONTEND_DEV_PORT}" \
- PULSE_DEV_API_HOST="${PULSE_DEV_API_HOST}" \
- PULSE_DEV_API_PORT="${PULSE_DEV_API_PORT}" \
- PULSE_DEV_API_URL="${PULSE_DEV_API_URL}" \
- PULSE_DEV_WS_URL="${PULSE_DEV_WS_URL}" \
- ROOT_DIR="${ROOT_DIR}" \
- LOG_FILE="${LOG_FILE}" \
- python3 - <<'PY'
-import os
-import subprocess
-
-root_dir = os.environ["ROOT_DIR"]
-log_file = os.environ["LOG_FILE"]
-cmd = [os.path.join(root_dir, "scripts", "hot-dev.sh")]
-
-env = os.environ.copy()
-
-with open(log_file, "ab", buffering=0) as log:
- proc = subprocess.Popen(
- cmd,
- cwd=root_dir,
- stdin=subprocess.DEVNULL,
- stdout=log,
- stderr=subprocess.STDOUT,
- start_new_session=True,
- env=env,
- )
-
-print(proc.pid)
-PY
- )"
+ pid="$(spawn_detached_supervisor)"
- [[ -n "${pid}" ]] || fail "Failed to start hot-dev"
+ [[ -n "${pid}" ]] || fail "Failed to start managed runtime supervisor"
printf "%s\n" "${pid}" > "${PID_FILE}"
- log "Started hot-dev (pid: ${pid})"
+ log "Started managed runtime supervisor (pid: ${pid})"
log "Waiting for managed backend listener on port ${PULSE_DEV_API_PORT}..."
if ! wait_for_managed_listener "${PULSE_DEV_API_PORT}" "${pid}" 90; then
log "Backend did not start. Showing recent logs:"
tail -n 80 "${LOG_FILE}" || true
- fail "hot-dev startup failed"
+ fail "managed runtime startup failed"
fi
if ! wait_for_managed_listener "${FRONTEND_DEV_PORT}" "${pid}" 90; then
@@ -841,7 +913,7 @@ parse_takeover_flag() {
usage
return 1
;;
- stop|backend-restart|status|logs)
+ stop|backend-restart|status|logs|supervise)
if [[ -n "${flag}" ]]; then
usage
return 1
@@ -862,6 +934,7 @@ main() {
takeover="$(parse_takeover_flag "$@")" || exit 1
case "${command}" in
+ supervise) run_supervised_session ;;
start)
start_bg "${takeover}"
;;
diff --git a/scripts/tests/test-hot-dev-bg.sh b/scripts/tests/test-hot-dev-bg.sh
index d39acbb277..7ca999dd07 100755
--- a/scripts/tests/test-hot-dev-bg.sh
+++ b/scripts/tests/test-hot-dev-bg.sh
@@ -264,21 +264,12 @@ test_launchd_session_supervises_managed_runtime() {
}
test_start_bg_reports_browser_entrypoint() {
- local test_dir fake_bin output
+ local test_dir output
test_dir="$(mktemp -d)"
temp_dirs+=("${test_dir}")
- fake_bin="${test_dir}/bin"
- mkdir -p "${fake_bin}"
-
- cat > "${fake_bin}/python3" <<'EOF'
-#!/usr/bin/env bash
-printf '4242\n'
-EOF
- chmod +x "${fake_bin}/python3"
output="$(
HOT_DEV_BG_PATH="${HOT_DEV_BG}" \
- PATH="${fake_bin}:$PATH" \
bash -lc '
source "${HOT_DEV_BG_PATH}"
PID_FILE="'"${test_dir}"'/hot-dev-bg.pid"
@@ -288,15 +279,56 @@ EOF
has_unmanaged_listeners(){ return 1; }
is_running(){ return 1; }
wait_for_managed_listener(){ return 0; }
+ spawn_detached_supervisor(){ printf "4242\n"; }
start_bg false
'
)"
+ assert_contains "start reports managed runtime supervisor pid" "${output}" "Started managed runtime supervisor (pid: 4242)"
assert_contains "start reports browser entrypoint" "${output}" "Browser entrypoint: http://127.0.0.1:5173"
assert_contains "start reports managed backend" "${output}" "Managed backend: http://127.0.0.1:7655"
assert_not_contains "start no longer reports generic frontend url" "${output}" "Frontend: http://127.0.0.1:5173"
}
+test_supervisor_restarts_unexpected_child_exit() {
+ local output
+ output="$(
+ HOT_DEV_BG_PATH="${HOT_DEV_BG}" \
+ bash -lc '
+ source "${HOT_DEV_BG_PATH}"
+ set +e
+ test_dir="$(mktemp -d)"
+ trap "rm -rf \"$test_dir\"" EXIT
+ count_file="${test_dir}/starts"
+ printf "0\n" > "${count_file}"
+ start_hot_dev_child() {
+ count="$(cat "${count_file}")"
+ count=$((count + 1))
+ printf "%s\n" "${count}" > "${count_file}"
+ if [[ "${count}" -eq 1 ]]; then
+ (exit 7) &
+ else
+ (sleep 30) &
+ fi
+ HOT_DEV_CHILD_PID="$!"
+ }
+ stop_hot_dev_child() {
+ kill -TERM "${1}" 2>/dev/null || true
+ }
+ HOT_DEV_BG_SUPERVISOR_RESTART_DELAY=0.1
+ run_supervised_session &
+ supervisor_pid=$!
+ sleep 0.5
+ kill -TERM "${supervisor_pid}"
+ wait "${supervisor_pid}"
+ printf "starts=%s\n" "$(cat "${count_file}")"
+ '
+ )"
+
+ assert_contains "supervisor reports unexpected child exit" "${output}" "Managed runtime child exited unexpectedly"
+ assert_contains "supervisor restarts hot-dev child after exit" "${output}" "starts=2"
+}
+
test_launchd_wrapper_uses_managed_supervisor() {
local output
output="$(sed -n '1,80p' "${DEV_LAUNCHD_WRAPPER}")"
@@ -446,7 +478,7 @@ test_hot_dev_script_advertises_foreground_escape_hatch() {
test_hot_dev_bg_script_advertises_managed_entrypoint() {
local output
- output="$(sed -n '1,780p' "${ROOT_DIR}/scripts/hot-dev-bg.sh")"
+ output="$(cat "${ROOT_DIR}/scripts/hot-dev-bg.sh")"
assert_contains "hot-dev-bg usage points to managed runtime first" "${output}" "npm run dev # Canonical managed dev runtime"
assert_contains "hot-dev-bg still documents direct launcher start" "${output}" "./scripts/hot-dev-bg.sh start [--takeover]"
@@ -703,6 +735,7 @@ main() {
test_takeover_avoids_killing_current_shell_lineage
test_launchd_session_supervises_managed_runtime
test_start_bg_reports_browser_entrypoint
+ test_supervisor_restarts_unexpected_child_exit
test_launchd_wrapper_uses_managed_supervisor
test_launchd_setup_advertises_managed_runtime_controls
test_root_package_exposes_managed_runtime_entrypoints