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.

Recover managed mock-mode restart handoffs

rcourtmana13218a39ad8
diff --git a/docs/release-control/v6/internal/subsystems/deployment-installability.md b/docs/release-control/v6/internal/subsystems/deployment-installability.md
index fbbea64df5..e636b1062a 100644
--- a/docs/release-control/v6/internal/subsystems/deployment-installability.md
+++ b/docs/release-control/v6/internal/subsystems/deployment-installability.md
@@ -871,6 +871,17 @@ deployments each. Bumping either side requires bumping the other (and
the matching `scripts/tests/test-toggle-mock.sh` fixtures) so toggle
CLIs, managed runtime restarts, and the in-binary default never drift
apart.
+Mock toggles are runtime transitions, not just environment-file edits. A
+successful `scripts/toggle-mock.sh on|off` run must leave the managed browser
+entrypoint serving the requested `/api/system/mock-mode` state through the
+frontend proxy before the command reports success. When macOS launchd or
+another managed wrapper hands ownership to a replacement `hot-dev-bg.sh supervise`
+process during the restart, `scripts/hot-dev-bg.sh` must adopt the healthy
+supervisor, refresh the managed pid file, and continue instead of surfacing a
+false startup failure. `scripts/toggle-mock.sh` may only continue after a
+non-clean managed restart when that browser-entrypoint proof confirms the
+requested mock state; otherwise it must fail explicitly or use an intentional
+fallback.
That same hosted runtime rollout boundary also owns public routing identity for
managed tenants. `internal/cloudcp/docker/labels.go`,
`internal/cloudcp/docker/manager.go`, and
diff --git a/scripts/hot-dev-bg.sh b/scripts/hot-dev-bg.sh
index 4cb189cb0f..3aeb47f015 100755
--- a/scripts/hot-dev-bg.sh
+++ b/scripts/hot-dev-bg.sh
@@ -121,7 +121,7 @@ process_inspection_restricted() {
process_probe_denied "${output}"
}
-discover_managed_supervisor_pid() {
+managed_supervisor_pids() {
local pid command
while IFS= read -r pid; do
@@ -131,10 +131,19 @@ discover_managed_supervisor_pid() {
case "${command}" in
"bash ${ROOT_DIR}/scripts/hot-dev-bg.sh supervise"|"/bin/bash ${ROOT_DIR}/scripts/hot-dev-bg.sh supervise"|"${ROOT_DIR}/scripts/hot-dev-bg.sh supervise")
printf "%s\n" "${pid}"
- return 0
;;
esac
done < <(pgrep -f "${ROOT_DIR}/scripts/hot-dev-bg.sh supervise" 2>/dev/null | sort -n)
+}
+
+discover_managed_supervisor_pid() {
+ local pid
+
+ while IFS= read -r pid; do
+ [[ -n "${pid}" ]] || continue
+ printf "%s\n" "${pid}"
+ return 0
+ done < <(managed_supervisor_pids)
return 1
}
@@ -302,6 +311,46 @@ wait_for_managed_listener() {
return 1
}
+adopt_managed_runtime_with_listener() {
+ local port="$1"
+ local candidate_pid
+
+ [[ "${PID_FILE}" == "${DEFAULT_HOT_DEV_BG_PID_FILE}" ]] || return 1
+
+ while IFS= read -r candidate_pid; do
+ [[ -n "${candidate_pid}" ]] || continue
+ pid_may_be_running "${candidate_pid}" || continue
+ if port_has_managed_listener "${port}" "${candidate_pid}"; then
+ mkdir -p "$(dirname "${PID_FILE}")"
+ printf "%s\n" "${candidate_pid}" > "${PID_FILE}"
+ log "Adopted managed runtime supervisor after handoff (pid: ${candidate_pid})"
+ return 0
+ fi
+ done < <(managed_supervisor_pids)
+
+ return 1
+}
+
+wait_for_managed_runtime_listener() {
+ local port="$1"
+ local session_pid="$2"
+ local timeout_seconds="${3:-60}"
+ local checks=$((timeout_seconds * 2))
+
+ while (( checks > 0 )); do
+ if [[ -n "${session_pid}" ]] && port_has_managed_listener "${port}" "${session_pid}"; then
+ return 0
+ fi
+ if adopt_managed_runtime_with_listener "${port}"; then
+ return 0
+ fi
+ sleep 0.5
+ checks=$((checks - 1))
+ done
+
+ return 1
+}
+
is_running() {
if [[ ! -f "${PID_FILE}" ]]; then
recover_managed_pid_file
@@ -676,13 +725,15 @@ start_bg() {
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
+ if ! wait_for_managed_runtime_listener "${PULSE_DEV_API_PORT}" "${pid}" 90; then
log "Backend did not start. Showing recent logs:"
tail -n 80 "${LOG_FILE}" || true
fail "managed runtime startup failed"
fi
+ pid="$(managed_session_pid || true)"
+ [[ -n "${pid}" ]] || pid="$(cat "${PID_FILE}" 2>/dev/null || true)"
- if ! wait_for_managed_listener "${FRONTEND_DEV_PORT}" "${pid}" 90; then
+ if ! wait_for_managed_runtime_listener "${FRONTEND_DEV_PORT}" "${pid}" 90; then
log "Backend is up, but a managed frontend listener on port ${FRONTEND_DEV_PORT} is not ready yet."
log "Check logs with: npm run dev:logs"
fi
diff --git a/scripts/tests/test-hot-dev-bg.sh b/scripts/tests/test-hot-dev-bg.sh
index 526522f97a..247aa3f910 100755
--- a/scripts/tests/test-hot-dev-bg.sh
+++ b/scripts/tests/test-hot-dev-bg.sh
@@ -389,7 +389,7 @@ test_start_bg_reports_browser_entrypoint() {
PULSE_DEV_API_PORT=7655
has_unmanaged_listeners(){ return 1; }
is_running(){ return 1; }
- wait_for_managed_listener(){ return 0; }
+ wait_for_managed_runtime_listener(){ return 0; }
spawn_detached_supervisor(){ printf "4242\n"; }
start_bg false
'
@@ -402,6 +402,34 @@ test_start_bg_reports_browser_entrypoint() {
assert_not_contains "start no longer reports generic frontend url" "${output}" "Frontend: http://127.0.0.1:5173"
}
+test_wait_for_managed_runtime_listener_adopts_supervisor_handoff() {
+ local test_dir output
+ test_dir="$(mktemp -d)"
+ temp_dirs+=("${test_dir}")
+
+ output="$(
+ HOT_DEV_BG_PATH="${HOT_DEV_BG}" \
+ bash -lc '
+ source "${HOT_DEV_BG_PATH}"
+ PID_FILE="'"${test_dir}"'/hot-dev-bg.pid"
+ DEFAULT_HOT_DEV_BG_PID_FILE="${PID_FILE}"
+ pid_may_be_running(){ return 0; }
+ managed_supervisor_pids(){ printf "1111\n2222\n"; }
+ port_has_managed_listener() {
+ [[ "${1:-}" == "7655" && "${2:-}" == "2222" ]]
+ }
+
+ wait_for_managed_runtime_listener 7655 1111 1
+ printf "status=%s\n" "$?"
+ printf "pid_file=%s\n" "$(cat "${PID_FILE}")"
+ '
+ )"
+
+ assert_contains "startup wait adopts the recovered supervisor" "${output}" "Adopted managed runtime supervisor after handoff (pid: 2222)"
+ assert_contains "startup wait returns success after adoption" "${output}" "status=0"
+ assert_contains "startup wait rewrites the managed pid file" "${output}" "pid_file=2222"
+}
+
test_supervisor_restarts_unexpected_child_exit() {
local output
output="$(
@@ -1519,6 +1547,7 @@ main() {
test_takeover_avoids_killing_current_shell_lineage
test_launchd_session_supervises_managed_runtime
test_start_bg_reports_browser_entrypoint
+ test_wait_for_managed_runtime_listener_adopts_supervisor_handoff
test_supervisor_restarts_unexpected_child_exit
test_stop_hot_dev_child_targets_child_process_group
test_launchd_wrapper_uses_managed_supervisor
diff --git a/scripts/tests/test-toggle-mock.sh b/scripts/tests/test-toggle-mock.sh
index 93267e77cd..1d175c2946 100755
--- a/scripts/tests/test-toggle-mock.sh
+++ b/scripts/tests/test-toggle-mock.sh
@@ -75,6 +75,10 @@ case "${COMMAND}" in
fi
;;
start)
+ if [[ "${HOT_DEV_BG_STUB_START_FAIL:-false}" == "true" ]]; then
+ echo "[hot-dev-bg] ERROR: managed runtime startup failed" >&2
+ exit 1
+ fi
echo "[hot-dev-bg] Started"
;;
stop)
@@ -153,6 +157,35 @@ test_managed_hot_dev_stop_uses_control_plane() {
assert_contains "stop_hot_dev_runtime uses managed stop command" "${calls}" "stop "
}
+test_apply_mode_accepts_recovered_managed_mock_state_after_start_failure() {
+ local stub state output calls
+ state="$(mktemp -d)"
+ temp_dirs+=("${state}")
+ stub="$(make_stub_hot_dev_bg)"
+
+ output="$(
+ HOT_DEV_BG_PATH="${stub}" \
+ HOT_DEV_BG_STUB_LOG="${state}/calls.log" \
+ HOT_DEV_BG_STUB_RUNNING=true \
+ HOT_DEV_BG_STUB_START_FAIL=true \
+ TOGGLE_MOCK_PATH="${TOGGLE_MOCK}" \
+ bash -lc '
+ source "${TOGGLE_MOCK_PATH}"
+ detect_runtime_mode(){ printf "managed-hot-dev\n"; }
+ set_mock_mode(){ printf "set_mode=%s\n" "$1"; }
+ stop_hot_dev_runtime(){ printf "stop=%s\n" "$1"; }
+ verify_mock_state_via_frontend_proxy(){ printf "verified=%s\n" "$1"; return 0; }
+ apply_mode true
+ '
+ )"
+ calls="$(cat "${state}/calls.log")"
+
+ assert_contains "apply_mode still uses managed takeover start" "${calls}" "start --takeover"
+ assert_contains "apply_mode warns on a non-clean managed restart" "${output}" "Managed hot-dev start command did not complete cleanly"
+ assert_contains "apply_mode accepts a recovered requested mock state" "${output}" "browser entrypoint is serving the requested mock state"
+ assert_contains "apply_mode completes the toggle after recovery proof" "${output}" "Mock mode is now enabled"
+}
+
test_ensure_mock_env_file_seeds_canonical_demo_defaults() {
local state output env_contents
state="$(mktemp -d)"
@@ -260,6 +293,7 @@ main() {
test_detects_managed_hot_dev_runtime
test_managed_hot_dev_start_uses_takeover
test_managed_hot_dev_stop_uses_control_plane
+ test_apply_mode_accepts_recovered_managed_mock_state_after_start_failure
test_ensure_mock_env_file_seeds_canonical_demo_defaults
test_ensure_mock_env_file_migrates_legacy_sidecar
test_set_mock_mode_does_not_recreate_legacy_sidecars
diff --git a/scripts/toggle-mock.sh b/scripts/toggle-mock.sh
index 2a0ecb48f4..690d7f0be7 100755
--- a/scripts/toggle-mock.sh
+++ b/scripts/toggle-mock.sh
@@ -199,6 +199,13 @@ verify_mock_state_via_frontend_proxy() {
return 1
}
+managed_hot_dev_reports_mock_state() {
+ local target_mode="$1"
+
+ hot_dev_bg_is_running || return 1
+ verify_mock_state_via_frontend_proxy "$target_mode"
+}
+
sed_inplace() {
local expr="$1"
local file="$2"
@@ -568,7 +575,7 @@ sync_production_config() {
start_hot_dev_runtime() {
log_info "Starting managed hot-dev runtime..."
if ! run_hot_dev_bg start --takeover; then
- log_error "Managed hot-dev failed to start"
+ log_warn "Managed hot-dev start command did not complete cleanly"
return 1
fi
log_success "Managed hot-dev runtime is running"
@@ -702,7 +709,9 @@ apply_mode() {
;;
managed-hot-dev|hot-dev)
if ! start_hot_dev_runtime; then
- if [[ "${allow_fallback}" == "true" ]]; then
+ if managed_hot_dev_reports_mock_state "$target_mode"; then
+ log_warn "Managed hot-dev reported a restart problem, but the browser entrypoint is serving the requested mock state; continuing"
+ elif [[ "${allow_fallback}" == "true" ]]; then
log_warn "hot-dev restart failed or is unhealthy; falling back to standalone backend on port ${DEFAULT_PORT}"
stop_hot_dev_runtime "$start_mode" || true
start_standalone_runtime "$target_mode"