fix(supervisor): relaunch haproxy directly from bash (~1 s) + restart marker for the watchdog #25

Merged
jknapp merged 2 commits from fix/supervisor-fast-relaunch into main 2026-11-26 02:11:51 +00:00
Owner

Problem

whp02 2026-11-26: a HAProxy worker abort made the master exit (exit-on-failure, same in rc6 and 2026.11.9; the master never re-forks). The in-container supervisor then needed 16 s just to LOG "process not found": it relaunches through ensure_haproxy.py, which imports the whole ~4000-line manager (Flask, Jinja) and was starved by swap I/O. The WHP watchdog docker restart landed on top of it. Hard-down was ~37 s.

Change

scripts/haproxy-supervisor.sh: when pidof haproxy finds nothing it now

  1. writes the marker /tmp/haproxy-restarting (<epoch> <pid>; HAPROXY_SUPERVISOR_MARKER) and logs "haproxy process not found" itself (same text python used), timestamped to the ms;
  2. validates the on-disk config (timeout 20 haproxy -c -q -f /etc/haproxy/haproxy.cfg), the config the dead master loaded minutes ago;
  3. re-checks pidof (never a second master), then launches exactly like spawn_haproxy_master(): haproxy -W -db -S /tmp/haproxy-cli,level,admin -f cfg, stderr appended to the haproxy stderr log (same path/0640//tmp fallback), double-forked so it re-parents to PID 1 (gunicorn reaps it; a zombie child of the loop would keep pidof true);
  4. waits for Loading success. (deadline HAPROXY_START_WAIT, same rule as python), logs each step with +N ms since detection, clears the marker;
  5. falls back to python ensure_haproxy.py only if the config is missing/unreadable/invalid, the binary is missing, or the launch fails or never loads. The existing quick-death/failed-restart backoffs are unchanged and wrap both paths.

Poll interval default 1 s (was 2 s); pidof is a few ms. New knobs (all optional): HAPROXY_SUPERVISOR_CFG, _BIN, _SOCKET, _MARKER. Not done on purpose: master-worker no-exit-on-failure (the master would stay up with no worker and the supervisor would never relaunch).

Tests

  • scripts/test-haproxy-supervisor.py (stubs): direct relaunch with no python, marker present during/cleared after, invalid/missing config and missing binary fall back to python, running haproxy never gets a second master, 1 s default poll. Existing backoff tests unchanged and green (11 tests, ~60 s).
  • NEW scripts/test-haproxy-supervisor-relaunch.py (real haproxy 3.0.11 in a python:3.12-slim container, skips without a binary): real master + the real supervisor, SIGABRT the worker, asserts the master exits, a new master serves < 3 s later, started from bash (python stub never ran), exactly one master, timestamped step log, marker seen then cleared, running master never duplicated, invalid config falls back to python. Measured relaunch (master exit -> serving) 0.76-0.78 s over 4 runs, idle host.
  • Both added to the Dockerfile test RUN chain (image build gate). CLAUDE.md updated.

Not covered: the starved-host case itself (the 16 s was python import under swap; this path imports nothing). No VERSION bump, nothing published. Companion: WHP PR makes the host watchdog honour the marker.

🤖 Generated with Claude Code

https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6

## Problem whp02 2026-11-26: a HAProxy worker abort made the master exit (`exit-on-failure`, same in rc6 and 2026.11.9; the master never re-forks). The in-container supervisor then needed 16 s just to LOG "process not found": it relaunches through `ensure_haproxy.py`, which imports the whole ~4000-line manager (Flask, Jinja) and was starved by swap I/O. The WHP watchdog `docker restart` landed on top of it. Hard-down was ~37 s. ## Change `scripts/haproxy-supervisor.sh`: when `pidof haproxy` finds nothing it now 1. writes the marker `/tmp/haproxy-restarting` (`<epoch> <pid>`; `HAPROXY_SUPERVISOR_MARKER`) and logs "haproxy process not found" itself (same text python used), timestamped to the ms; 2. validates the on-disk config (`timeout 20 haproxy -c -q -f /etc/haproxy/haproxy.cfg`), the config the dead master loaded minutes ago; 3. re-checks `pidof` (never a second master), then launches exactly like `spawn_haproxy_master()`: `haproxy -W -db -S /tmp/haproxy-cli,level,admin -f cfg`, stderr appended to the haproxy stderr log (same path/0640/`/tmp` fallback), double-forked so it re-parents to PID 1 (gunicorn reaps it; a zombie child of the loop would keep `pidof` true); 4. waits for `Loading success.` (deadline `HAPROXY_START_WAIT`, same rule as python), logs each step with `+N ms since detection`, clears the marker; 5. falls back to `python ensure_haproxy.py` only if the config is missing/unreadable/invalid, the binary is missing, or the launch fails or never loads. The existing quick-death/failed-restart backoffs are unchanged and wrap both paths. Poll interval default 1 s (was 2 s); `pidof` is a few ms. New knobs (all optional): `HAPROXY_SUPERVISOR_CFG`, `_BIN`, `_SOCKET`, `_MARKER`. Not done on purpose: `master-worker no-exit-on-failure` (the master would stay up with no worker and the supervisor would never relaunch). ## Tests - `scripts/test-haproxy-supervisor.py` (stubs): direct relaunch with no python, marker present during/cleared after, invalid/missing config and missing binary fall back to python, running haproxy never gets a second master, 1 s default poll. Existing backoff tests unchanged and green (11 tests, ~60 s). - NEW `scripts/test-haproxy-supervisor-relaunch.py` (real haproxy 3.0.11 in a python:3.12-slim container, skips without a binary): real master + the real supervisor, SIGABRT the worker, asserts the master exits, a new master serves < 3 s later, started from bash (python stub never ran), exactly one master, timestamped step log, marker seen then cleared, running master never duplicated, invalid config falls back to python. **Measured relaunch (master exit -> serving) 0.76-0.78 s** over 4 runs, idle host. - Both added to the Dockerfile test RUN chain (image build gate). CLAUDE.md updated. Not covered: the starved-host case itself (the 16 s was python import under swap; this path imports nothing). No VERSION bump, nothing published. Companion: WHP PR makes the host watchdog honour the marker. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
jknapp added 1 commit 2026-11-26 01:56:43 +00:00
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
jknapp added 1 commit 2026-11-26 02:10:08 +00:00
- test-haproxy-supervisor-relaunch.py used `pgrep -P`, which the image does not
  ship (no procps): the image build failed with FileNotFoundError in 3 of the 4
  real-haproxy tests. Children are now read from /proc.
- haproxy-supervisor.sh: clear /tmp/haproxy-restarting at start-up. A
  supervisor SIGKILLed mid-recovery (docker restart) runs no EXIT trap and
  /tmp survives a restart, so the marker would otherwise go stale.
- VERSION 2026.11.10: merging to main publishes :<VERSION>; 2026.11.9 is
  already published and must not be overwritten with different content.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
Author
Owner

Independent review (approve after fixes pushed in 4ced479)

Defect found and fixed

  • scripts/test-haproxy-supervisor-relaunch.py called pgrep -P. The image has no procps, so the image build failed: 3 of the 4 real-haproxy tests raised FileNotFoundError: 'pgrep' (measured with a local docker build). Since this repo has no PR CI, the first place it would have failed is the publish run on main. Fixed: children are now read from /proc.

Other fixes

  • Supervisor start-up now clears /tmp/haproxy-restarting. When docker restart happens mid-recovery, the supervisor is SIGKILLed when PID 1 exits, so no EXIT trap runs, and /tmp survives a restart. Without this the marker goes stale. No reader uses the marker today (WHP reads the log lines), but it should not lie.
  • VERSION is now 2026.11.10. A merge to main publishes :latest, :<VERSION> and :<sha>, and :2026.11.9 is already published, so merging with 2026.11.9 would have overwritten it with different content.

Parity with spawn_haproxy_master() (read, then measured in a running container built from this branch)

  • Command, flags and master socket path are identical (haproxy -W -db -S /tmp/haproxy-cli,level,admin -f /etc/haproxy/haproxy.cfg). stdin is /dev/null. stdout and stderr are appended to the same stderr log, using the same umask 027 / 0640 / /tmp fallback. cwd is /haproxy (WORKDIR) for both.
  • uid is root in both. Env and ulimits are the same container env and defaults: the supervisor and gunicorn both inherit start-up.sh. None of this matters for the config: no template and no code path in haproxy_manager.py renders a ${...} reference, and HAPROXY_NBTHREAD only acts at render time. The pidfile /var/run/haproxy.pid is written by HAProxy itself, and after the relaunch it held the new master's pid.
  • The detach is the same as the python path: the ( ... & ) subshell exits, so the master is reparented to PID 1. Measured: master ppid=1, and gunicorn reaped the old master ("Worker (pid:44) exited with code 134"). That arbiter logs and waitpid()s unknown children too. 0 zombies afterwards.
  • The python side tracks nothing by pid (is_process_running matches on the process name; reload uses socat to /tmp/haproxy-cli). After the bash relaunch, echo reload | socat stdio /tmp/haproxy-cli returned Success=1 and show proc showed reloads=1 with a new worker. ensure_haproxy.py run afterwards was a no-op (rc 0, still one master), so python does not start a second master.
  • E2E in the built image: SIGABRT on the worker. The master was gone in 0.29 s, a new master was serving 0.91 s later, and the marker was seen during the restart and gone after. Supervisor timings: detection to config valid +24 ms, launched +28 ms, "up in place" +131 ms.

Known residuals (not blockers, already there before this PR)

  • Supervisor and /api/reload can race. Both check-then-spawn and nothing locks between them, but the window is now ms instead of seconds.
  • pidof haproxy also matches a concurrent haproxy -c started by the API. Worst case: a spurious "already running" then a quick-death retry about 3 s later. Never a second master.
  • An [ALERT] seen at the deadline sends a live process to the python fallback, which then no-ops. This is the same as spawn_haproxy_master().

Runs: shellcheck clean. Full local docker build: whole test chain OK, supervisor stub suite 11/11, real relaunch 4/4. The relaunch test failed before the fix.

## Independent review (approve after fixes pushed in 4ced479) **Defect found and fixed** - `scripts/test-haproxy-supervisor-relaunch.py` called `pgrep -P`. The image has no procps, so the image build failed: 3 of the 4 real-haproxy tests raised `FileNotFoundError: 'pgrep'` (measured with a local `docker build`). Since this repo has no PR CI, the first place it would have failed is the publish run on main. Fixed: children are now read from `/proc`. **Other fixes** - Supervisor start-up now clears `/tmp/haproxy-restarting`. When `docker restart` happens mid-recovery, the supervisor is SIGKILLed when PID 1 exits, so no EXIT trap runs, and `/tmp` survives a restart. Without this the marker goes stale. No reader uses the marker today (WHP reads the log lines), but it should not lie. - `VERSION` is now 2026.11.10. A merge to main publishes `:latest`, `:<VERSION>` and `:<sha>`, and `:2026.11.9` is already published, so merging with 2026.11.9 would have overwritten it with different content. **Parity with `spawn_haproxy_master()`** (read, then measured in a running container built from this branch) - Command, flags and master socket path are identical (`haproxy -W -db -S /tmp/haproxy-cli,level,admin -f /etc/haproxy/haproxy.cfg`). stdin is `/dev/null`. stdout and stderr are appended to the same stderr log, using the same umask 027 / 0640 / `/tmp` fallback. cwd is `/haproxy` (WORKDIR) for both. - uid is root in both. Env and ulimits are the same container env and defaults: the supervisor and gunicorn both inherit start-up.sh. None of this matters for the config: no template and no code path in `haproxy_manager.py` renders a `${...}` reference, and `HAPROXY_NBTHREAD` only acts at render time. The pidfile `/var/run/haproxy.pid` is written by HAProxy itself, and after the relaunch it held the new master's pid. - The detach is the same as the python path: the `( ... & )` subshell exits, so the master is reparented to PID 1. Measured: master ppid=1, and gunicorn reaped the old master ("Worker (pid:44) exited with code 134"). That arbiter logs and waitpid()s unknown children too. 0 zombies afterwards. - The python side tracks nothing by pid (`is_process_running` matches on the process name; reload uses socat to `/tmp/haproxy-cli`). After the bash relaunch, `echo reload | socat stdio /tmp/haproxy-cli` returned `Success=1` and `show proc` showed reloads=1 with a new worker. `ensure_haproxy.py` run afterwards was a no-op (rc 0, still one master), so python does not start a second master. - E2E in the built image: SIGABRT on the worker. The master was gone in 0.29 s, a new master was serving 0.91 s later, and the marker was seen during the restart and gone after. Supervisor timings: detection to config valid +24 ms, launched +28 ms, "up in place" +131 ms. **Known residuals (not blockers, already there before this PR)** - Supervisor and `/api/reload` can race. Both check-then-spawn and nothing locks between them, but the window is now ms instead of seconds. - `pidof haproxy` also matches a concurrent `haproxy -c` started by the API. Worst case: a spurious "already running" then a quick-death retry about 3 s later. Never a second master. - An `[ALERT]` seen at the deadline sends a live process to the python fallback, which then no-ops. This is the same as `spawn_haproxy_master()`. **Runs:** shellcheck clean. Full local `docker build`: whole test chain OK, supervisor stub suite 11/11, real relaunch 4/4. The relaunch test failed before the fix.
jknapp merged commit 6a2ac2f1db into main 2026-11-26 02:11:51 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: cloud-hosting-platform/haproxy-manager-base#25