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

+80 -1
View File
@@ -25,7 +25,7 @@ exit ${ENSURE_RC:-0}
"""
class SupervisorTests(unittest.TestCase):
class SupervisorBase:
def setUp(self):
self.d = tempfile.mkdtemp(prefix='hap-sup-')
self.state = os.path.join(self.d, 'state')
@@ -39,6 +39,10 @@ class SupervisorTests(unittest.TestCase):
def run_sup(self, seconds, **env):
e = dict(os.environ, PATH=self.d + ':' + os.environ['PATH'], STATE=self.state)
e.setdefault('LIFE', '0')
# keep the fast path OFF unless a test opts in (no config -> python fallback)
e.setdefault('HAPROXY_SUPERVISOR_CFG', os.path.join(self.d, 'no-such.cfg'))
e.setdefault('HAPROXY_SUPERVISOR_MARKER', os.path.join(self.state, 'marker'))
e.setdefault('HAPROXY_STDERR_LOG', os.path.join(self.d, 'stderr.log'))
e.update(env)
try:
subprocess.run(['bash', SCRIPT], env=e, timeout=seconds, stdout=subprocess.PIPE,
@@ -54,6 +58,8 @@ class SupervisorTests(unittest.TestCase):
except OSError:
return []
class SupervisorTests(SupervisorBase, unittest.TestCase):
def test_non_integer_interval_does_not_kill_the_supervisor(self):
self.assertEqual(self.run_sup(8, HAPROXY_SUPERVISOR_INTERVAL='0.5', ENSURE_RC='1'), 'running')
self.assertGreaterEqual(len(self.starts()), 2) # fell back to the default interval and kept trying
@@ -93,5 +99,78 @@ class SupervisorTests(unittest.TestCase):
self.assertEqual(self.starts(), [])
# stand-in `haproxy`: -c validates (rc from $CHECK_RC); a launch stamps the start,
# logs the line the supervisor waits for, and records whether the marker existed
HAPROXY = """#!/bin/sh
if [ "$1" = "-c" ]; then exit ${CHECK_RC:-0}; fi
[ -f "$MARKER" ] && echo yes >> "$STATE/marker-seen"
date +%s.%N > "$STATE/started"
date +%s.%N >> "$STATE/direct"
echo "[NOTICE] Loading success." >&2
exit ${LAUNCH_RC:-0}
"""
class FastPathTests(SupervisorBase, unittest.TestCase):
def setUp(self):
super().setUp()
p = os.path.join(self.d, 'haproxy')
with open(p, 'w') as fh:
fh.write(HAPROXY)
os.chmod(p, os.stat(p).st_mode | stat.S_IXUSR)
self.cfg = os.path.join(self.d, 'haproxy.cfg')
with open(self.cfg, 'w') as fh:
fh.write('# stub\n')
def fast_env(self, **kw):
e = dict(HAPROXY_SUPERVISOR_CFG=self.cfg, MARKER=os.path.join(self.state, 'marker'),
HAPROXY_SUPERVISOR_INTERVAL='1', LIFE='1000')
e.update(kw)
return e
def direct(self):
try:
with open(os.path.join(self.state, 'direct')) as fh:
return fh.read().split()
except OSError:
return []
def test_dead_haproxy_is_relaunched_directly_without_python(self):
self.run_sup(4, **self.fast_env(LIFE='0')) # LIFE=0: haproxy is always "gone" at the first poll
self.assertGreaterEqual(len(self.direct()), 1)
self.assertEqual(self.starts(), []) # ensure_haproxy.py / python never ran
with open(os.path.join(self.state, 'marker-seen')) as fh:
self.assertEqual(fh.read().split(), ['yes'] * len(self.direct()))
self.assertFalse(os.path.exists(os.path.join(self.state, 'marker'))) # cleared after the recovery
def test_invalid_config_falls_back_to_python(self):
self.run_sup(4, **self.fast_env(LIFE='0', CHECK_RC='1'))
self.assertEqual(self.direct(), [])
self.assertGreaterEqual(len(self.starts()), 1)
self.assertFalse(os.path.exists(os.path.join(self.state, 'marker')))
def test_failed_direct_launch_falls_back_to_python(self):
# haproxy binary not on PATH: the direct path cannot start it
self.run_sup(4, **self.fast_env(LIFE='0', HAPROXY_SUPERVISOR_BIN='no-such-haproxy-bin'))
self.assertEqual(self.direct(), [])
self.assertGreaterEqual(len(self.starts()), 1)
def test_missing_config_falls_back_to_python(self):
self.run_sup(4, **self.fast_env(LIFE='0', HAPROXY_SUPERVISOR_CFG=os.path.join(self.d, 'gone.cfg')))
self.assertEqual(self.direct(), [])
self.assertGreaterEqual(len(self.starts()), 1)
def test_running_haproxy_never_gets_a_second_master(self):
with open(os.path.join(self.state, 'started'), 'w') as fh:
fh.write(str(time.time()))
self.assertEqual(self.run_sup(3, **self.fast_env()), 'running')
self.assertEqual(self.direct(), [])
self.assertEqual(self.starts(), [])
def test_poll_interval_defaults_to_one_second(self):
self.run_sup(3, **{k: v for k, v in self.fast_env(LIFE='0').items() if k != 'HAPROXY_SUPERVISOR_INTERVAL'})
self.assertGreaterEqual(len(self.direct()), 1) # first relaunch inside the 3 s budget (2 s poll + start would not fit twice)
if __name__ == '__main__':
unittest.main()