Status server: /health answers while a recording holds its lock

/health is what liveness probes call, and it was blocking on
_recordings_lock: connection_report() lists the recordings, while
start_recording/stop_recording hold that lock across the whole per-channel
attach (3.7 s for 10 400 channels, measured) and /recording/capture holds it
across the stop.

On a beamline server that was fatal. A liveness healthcheck probing /health
every 50 s timed out during exactly those windows, concluded the server was
dead and SIGKILLed it - seven times in one night, each kill 3-16 s after a
POST /status/capture or /recording/capture, and each costing a 114 s
namespace rebuild. The symptom reported was "the status server is always
reinitializing after a request from a client", and nothing in the server's
own logs connected the two: systemd only recorded status=9/KILL, there was
no OOM (oom_kill 0 at every cgroup level, peak 1.28 GB of 63 GB) and no
login session to blame.

/health now waits HEALTH_LOCK_TIMEOUT (0.5 s) for the lock and otherwise
reports "recordings": "busy" instead of not answering at all. Long enough
that an uncontended read always wins, short enough that no probe times out
because a recording is being attached or stopped. list_recordings() grew an
optional timeout; every other caller keeps the blocking behaviour, since
they want the real list.
This commit is contained in:
Henrik Lemke committed 2026-11-19 07:47:27 +01:00
1 parent bf911d67f7
commit e6a2b3cbcd
2 files changed
+75 -3

No files matched your search

+33 -3
View File
@@ -145,6 +145,12 @@ def _ca_thread(target, *args, name=None, **kwargs):
# sys.modules cache hit. Extend the tuple when another such module turns up;
# a name that is not installed is skipped, since this is a convenience and
# must not be able to stop the server from starting.
# How long /health waits for the recordings lock before answering without
# the recording list. Long enough that a normal, uncontended read wins;
# short enough that a liveness probe never times out because a recording is
# being attached or stopped.
HEALTH_LOCK_TIMEOUT = 0.5
MAIN_THREAD_IMPORTS = ("escape.swissfel",)
@@ -688,7 +694,14 @@ class NamespaceMonitorStore:
"n_monitored": len(self._monitors),
"n_direct_read": len(self._direct),
"n_monitorable": len(self._monitorable),
"recordings": self.list_recordings(),
# Bounded, never blocking: /health is what liveness probes
# call, and start_recording/stop_recording hold this lock across
# the whole per-channel attach (measured: 3.7 s for 10 400
# channels) and the stop. A probe that blocks there concludes the
# server is dead and kills it - which is exactly what happened,
# repeatedly, a few seconds after each capture. Reporting
# "recordings_busy" is strictly better than not answering.
"recordings": self._recordings_or_busy(),
"n_target_names": len(self._target_names),
"n_initialized": n_init,
"n_failed": n_failed,
@@ -904,9 +917,26 @@ class NamespaceMonitorStore:
rep["size_per_channel"] = session.size_report(top_n=size_top_n)
return rep
def list_recordings(self):
with self._recordings_lock:
def list_recordings(self, timeout=None):
"""Report every recording. `timeout` bounds the wait for the lock and
raises TimeoutError instead of blocking - see connection_report().
"""
if timeout is None:
with self._recordings_lock:
return [s.report() for s in self._recordings.values()]
if not self._recordings_lock.acquire(timeout=timeout):
raise TimeoutError("recordings lock busy")
try:
return [s.report() for s in self._recordings.values()]
finally:
self._recordings_lock.release()
def _recordings_or_busy(self):
"""list_recordings() for /health: a short wait, then give up."""
try:
return self.list_recordings(timeout=HEALTH_LOCK_TIMEOUT)
except TimeoutError:
return "busy"
def drop_recording(self, recording_id):
"""Forget a finished recording, freeing its buffers."""
+42
View File
@@ -2646,3 +2646,45 @@ def test_health_distinguishes_running_code_from_the_checkout_on_disk(app_and_sto
assert moved["commit"] == at_start # what is RUNNING
assert moved["commit_on_disk"] != at_start # what is on disk
assert "restart" in moved["hint"]
def test_health_answers_while_a_recording_holds_its_lock(app_and_store):
"""/health is what liveness probes call, so it must never block on a
work lock.
start_recording/stop_recording hold _recordings_lock across the whole
per-channel attach - 3.7 s for 10 400 channels, measured - and
/recording/capture holds it across the stop. A probe that blocks there
concludes the server is dead; on a real beamline server that killed it
seven times in one night, each kill a few seconds after a capture and
each costing a 114 s namespace rebuild.
"""
app, store, _ = app_and_store
client = app.test_client()
holder_in = threading.Event()
release = threading.Event()
def hold():
with store._recordings_lock:
holder_in.set()
release.wait(10)
t = threading.Thread(target=hold, daemon=True)
t.start()
assert holder_in.wait(5)
try:
t0 = time.time()
body = client.get("/health").get_json()
elapsed = time.time() - t0
finally:
release.set()
t.join(5)
# Answered, and promptly - not after whatever the holder was doing.
assert body["state"] == "ready"
assert elapsed < 5
assert body["recordings"] == "busy"
# Uncontended, the real list comes back as before.
assert isinstance(client.get("/health").get_json()["recordings"], list)