fix(supervisor): relaunch haproxy directly from bash in ~1 s; restart marker for the host watchdog

whp02 2026-11-26: after a worker abort the supervisor took 16 s just to log
'haproxy process not found' (ensure_haproxy.py imports the whole manager,
slow on a swapping host), and the WHP watchdog docker-restarted on top.

- When haproxy is gone, validate the on-disk config (haproxy -c) and start
  haproxy -W -db -S ... exactly like spawn_haproxy_master(), detached to PID 1,
  waiting for 'Loading success.'; fall back to ensure_haproxy.py only if the
  config is missing/invalid or the launch fails. pidof re-checked right before
  launch (never a second master).
- Poll interval default 1 s (was 2 s). Each step logged with a UTC ms timestamp.
- /tmp/haproxy-restarting ('<epoch> <pid>') exists while a recovery runs
  (HAPROXY_SUPERVISOR_MARKER) so the host watchdog can back off.
- Tests: stubbed fast/fallback/idempotency cases in test-haproxy-supervisor.py;
  new test-haproxy-supervisor-relaunch.py with a real haproxy (SIGABRT the
  worker, relaunch < 3 s, measured 0.77 s idle). Both run in the image build.
No VERSION bump.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-25 17:56:28 -08:00
1 parent ff535fae1f
commit 6df8d4b332
5 files changed
+440 -8

No files matched your search

+133 -6
View File
@@ -10,9 +10,26 @@
#
# The liveness probe is `pidof` (a few ms), NOT python: ensure_haproxy.py imports
# the 4000-line manager (Flask, Jinja, ...) which costs ~0.5 s of CPU per run and
# was affordable at 15 s but not at 2 s. python only runs when haproxy is gone.
# 2026-11 evidence: every watchdog abort took the whole edge down for 15-52 s,
# most of it detection latency + start; the default is now 2 s (was 15).
# was affordable at 15 s but not at 2 s. 2026-11 evidence: every watchdog abort
# took the whole edge down for 15-52 s, most of it detection latency + start.
#
# FAST PATH (2026-11-26, whp02 abort: python took 16 s just to LOG "process not
# found" on an I/O-starved host). When haproxy is gone the loop relaunches it
# DIRECTLY FROM BASH against the config on disk (the one the dead master loaded
# successfully minutes ago): `haproxy -c -f cfg` first, then the same command,
# flags, user and detach trick as haproxy_manager.spawn_haproxy_master()
# (`haproxy -W -db -S /tmp/haproxy-cli,level,admin -f cfg`, stderr appended to the
# stderr log, re-parented to PID 1 so gunicorn reaps it). Only if that fails
# (config missing/invalid, haproxy exits or never logs "Loading success.") does
# it fall back to `python ensure_haproxy.py` (regenerates + validates + starts).
# Never starts a second master: pidof is re-checked after validation, right
# before the launch. Every step is logged with a UTC millisecond timestamp.
#
# While a recovery is in progress the loop keeps HAPROXY_SUPERVISOR_MARKER
# (default /tmp/haproxy-restarting) containing "<epoch-seconds> <supervisor pid>"
# so the host watchdog (WHP panel/internal/watchdog/haproxy.go) can see the
# in-place restart is underway and not `docker restart` on top of it. It is
# removed when the recovery finishes (success or failure).
#
# Two backoffs:
# * a restart attempt that FAILS (config HAProxy rejects): the loop doubles its
@@ -36,10 +53,15 @@ _posint() { # _posint VALUE DEFAULT
esac
}
INTERVAL="$(_posint "${HAPROXY_SUPERVISOR_INTERVAL:-}" 2)"
INTERVAL="$(_posint "${HAPROXY_SUPERVISOR_INTERVAL:-}" 1)"
MAX_BACKOFF="$(_posint "${HAPROXY_SUPERVISOR_MAX_BACKOFF:-}" 30)"
QUICK_DEATH="$(_posint "${HAPROXY_SUPERVISOR_QUICK_DEATH:-}" 10)"
STABLE="$(_posint "${HAPROXY_SUPERVISOR_STABLE:-}" 60)"
START_WAIT="$(_posint "${HAPROXY_START_WAIT:-}" 10)"
CFG="${HAPROXY_SUPERVISOR_CFG:-/etc/haproxy/haproxy.cfg}"
BIN="${HAPROXY_SUPERVISOR_BIN:-haproxy}"
MASTER_SOCK="${HAPROXY_SUPERVISOR_SOCKET:-/tmp/haproxy-cli}"
MARKER="${HAPROXY_SUPERVISOR_MARKER:-/tmp/haproxy-restarting}"
STEPS=""
for s in ${HAPROXY_SUPERVISOR_BACKOFF_STEPS:-2 5 10 30}; do
v="$(_posint "$s" 0)"
@@ -58,6 +80,111 @@ _step() {
echo "${v}"
}
# --- logging / timing helpers -------------------------------------------------
_now_ms() {
if [ -n "${EPOCHREALTIME:-}" ]; then
local t="${EPOCHREALTIME/./}" # microseconds
echo $(( t / 1000 ))
else
echo $(( $(date +%s) * 1000 ))
fi
}
_log() { # _log LEVEL MESSAGE... (stderr = container stderr, like the old echo)
local lvl="$1"; shift
printf '%s [haproxy-supervisor] %s: %s\n' "$(date -u +%Y-%m-%dT%H:%M:%S.%3NZ)" "${lvl}" "$*" >&2
}
_stderr_log() { # same fallback as haproxy_manager._stderr_log_path()
local p="${HAPROXY_STDERR_LOG:-/var/log/haproxy/haproxy-stderr.log}"
if mkdir -p "$(dirname "${p}")" 2>/dev/null && (umask 027; : >>"${p}") 2>/dev/null; then
echo "${p}"
else
echo /tmp/haproxy-stderr.log
fi
}
_marker_set() { printf '%s %s\n' "$(date +%s)" "$$" >"${MARKER}" 2>/dev/null || true; }
_marker_clear() { rm -f "${MARKER}" 2>/dev/null || true; }
trap _marker_clear EXIT
# Direct bash relaunch. Return 0 = haproxy is up (started here or already
# running), 1 = caller must fall back to ensure_haproxy.py.
fast_relaunch() {
local t0="$1" t cfg_ms log_path offset text i
if [ ! -r "${CFG}" ]; then
_log WARN "fast path skipped: config ${CFG} missing/unreadable"
return 1
fi
if ! command -v "${BIN}" >/dev/null 2>&1; then
_log WARN "fast path skipped: ${BIN} not found on PATH"
return 1
fi
if ! timeout 20 "${BIN}" -c -q -f "${CFG}" >/dev/null 2>&1; then
_log WARN "fast path skipped: '${BIN} -c -f ${CFG}' rejected the on-disk config"
return 1
fi
t="$(_now_ms)"; cfg_ms=$(( t - t0 ))
_log INFO "on-disk config valid (+${cfg_ms} ms)"
# Idempotency: never a second master. Re-check right before launching (the
# validation above took time; the API's start_haproxy may have won).
if pidof -q haproxy; then
_log INFO "haproxy is already running; nothing to start"
return 0
fi
log_path="$(_stderr_log)"
offset="$(stat -c %s "${log_path}" 2>/dev/null || echo 0)"
# Same detach as spawn_haproxy_master(): the double fork re-parents haproxy to
# PID 1 (gunicorn reaps it; a zombie child of this loop would keep pidof true).
( chmod 0640 "${log_path}" 2>/dev/null
"${BIN}" -W -db -S "${MASTER_SOCK},level,admin" -f "${CFG}" \
</dev/null >>"${log_path}" 2>&1 & )
t="$(_now_ms)"
_log INFO "launched '${BIN} -W -db -S ${MASTER_SOCK},level,admin -f ${CFG}' (+$(( t - t0 )) ms)"
# Wait for "Loading success." (deadline HAPROXY_START_WAIT; alive + no [ALERT] at
# the deadline also counts), exactly like spawn_haproxy_master().
for i in $(seq 1 $(( START_WAIT * 10 ))); do
text="$(tail -c +$(( offset + 1 )) "${log_path}" 2>/dev/null || true)"
case "${text}" in
*"Loading success."*)
t="$(_now_ms)"
_log INFO "haproxy up in place: config loaded (+$(( t - t0 )) ms since detection)"
return 0 ;;
esac
# exited (or never came up) after the 1 s grace: stop waiting
if [ "${i}" -gt 10 ] && ! pidof -q haproxy; then break; fi
sleep 0.1
done
if pidof -q haproxy; then
case "${text}" in *"[ALERT]"*) ;; *)
_log INFO "haproxy up in place: alive, slow to log (+$(( $(_now_ms) - t0 )) ms since detection)"
return 0 ;;
esac
fi
_log ERROR "direct relaunch failed: $(printf '%s' "${text}" | tail -n 3 | tr '\n' '|')"
return 1
}
# Recover a missing haproxy: fast path, then the python fallback.
recover() {
local t0
t0="$(_now_ms)"
_marker_set
_log WARN "haproxy process not found — attempting in-place restart (direct relaunch first)"
if fast_relaunch "${t0}"; then
_marker_clear
return 0
fi
_log WARN "falling back to ensure_haproxy.py (+$(( $(_now_ms) - t0 )) ms)"
if python /haproxy/scripts/ensure_haproxy.py 2>&1; then
_log INFO "ensure_haproxy.py succeeded (+$(( $(_now_ms) - t0 )) ms since detection)"
_marker_clear
return 0
fi
_log ERROR "ensure_haproxy.py failed (+$(( $(_now_ms) - t0 )) ms)"
_marker_clear
return 1
}
delay="${INTERVAL}"
last_start=-1 # SECONDS at our last successful (re)start; -1 = none yet
quick=0 # consecutive deaths within QUICK_DEATH of a start
@@ -75,14 +202,14 @@ while true; do
if [ "${up}" -lt "${QUICK_DEATH}" ]; then
quick=$((quick + 1))
wait_s="$(_step "${quick}")"
echo "haproxy-supervisor: haproxy died ${up}s after start (quick death #${quick}); waiting ${wait_s}s before restart" >&2
_log WARN "haproxy died ${up}s after start (quick death #${quick}); waiting ${wait_s}s before restart"
sleep "${wait_s}"
pidof -q haproxy && continue
elif [ "${up}" -ge "${STABLE}" ]; then
quick=0
fi
fi
if python /haproxy/scripts/ensure_haproxy.py 2>&1; then
if recover; then
delay="${INTERVAL}"
last_start="${SECONDS}"
else