Skip to content

The agent that grows with you

fix(cron): chunk the store probe and forget stores this gateway stops ticking

kshitijk4poorb403581f3698
diff --git a/cron/scheduler_ownership.py b/cron/scheduler_ownership.py
index cf8ff998257..cef9f5ae860 100644
--- a/cron/scheduler_ownership.py
+++ b/cron/scheduler_ownership.py
@@ -48,8 +48,10 @@ def register_ticked_homes(homes) -> None:
if departed:
# Late import: cron.scheduler imports this module.
from cron.scheduler import discard_parallel_pools
+ from cron.store_health import forget_homes
discard_parallel_pools(departed)
+ forget_homes(departed)
def ticked_homes() -> dict:
diff --git a/cron/store_health.py b/cron/store_health.py
index c8a13b0416f..b0d4d4e2e50 100644
--- a/cron/store_health.py
+++ b/cron/store_health.py
@@ -152,6 +152,17 @@ def outage_covers(cron_dir: Path, due_at: float, grace: float) -> bool:
for r in (_degraded.get(str(cron_dir)), _recovered.get(str(cron_dir))))
+def forget_homes(home_keys) -> None:
+ """Drop the state of stores whose profile home (``hermes_home_key``) this process no longer
+ ticks, so a profile that left this gateway cannot keep the host-wide gauges at writable=0."""
+ from hermes_constants import hermes_home_key
+ keys = set(home_keys)
+ with _lock:
+ for records in (_degraded, _recovered):
+ for store in [s for s in records if hermes_home_key(Path(s).parent) in keys]:
+ del records[store]
+
+
def degraded_records() -> list:
with _lock:
return list(_degraded.values())
@@ -259,10 +270,12 @@ def probe_store(cron_dir: Path) -> Optional[OSError]:
if os.path.exists(target) and not os.access(target, os.W_OK): # e.g. a read-only symlink target
return OSError(errno.EACCES, os.strerror(errno.EACCES), target)
tmp = None
+ chunk = b"\0" * 65536
try:
fd, tmp = mkstemp_beside(jobs_file, prefix=".probe_")
with os.fdopen(fd, "wb") as f:
- f.write(b"\0" * size)
+ for offset in range(0, size, len(chunk)): # bounded memory for a large jobs.json
+ f.write(chunk[:size - offset])
f.flush()
os.fsync(f.fileno())
except OSError as exc:
diff --git a/tests/monitoring/test_cron_health_export.py b/tests/monitoring/test_cron_health_export.py
index d0dc9bff34b..9aeb09027af 100644
--- a/tests/monitoring/test_cron_health_export.py
+++ b/tests/monitoring/test_cron_health_export.py
@@ -41,6 +41,9 @@ def test_cron_store_writability_is_exported(monkeypatch, tmp_path):
from cron import store_health
monkeypatch.setattr(store_health, "_degraded", {})
monkeypatch.setattr(store_health, "_listener", None)
+ monkeypatch.setattr(store_health, "_recovered", {})
+ from cron import scheduler_ownership
+ monkeypatch.setattr(scheduler_ownership, "_ticked_homes", {})
store_health.note_unwritable(OSError(errno.ENOSPC, "full"), "x", "advance",
[{"id": "job", "next_run_at": "2026-06-22T12:00:00+00:00"}])
degraded = build_cron_health_snapshot()
@@ -49,9 +52,12 @@ def test_cron_store_writability_is_exported(monkeypatch, tmp_path):
store_health._degraded.clear()
assert _metric(build_cron_health_snapshot(), "hermes.cron.store.writable").value == 1
# A secondary profile's store (not the exporter's scope) must not read as healthy.
+ from cron.scheduler_ownership import register_ticked_homes
+ register_ticked_homes([tmp_path / "other"])
store_health.note_unwritable(OSError(errno.ENOSPC, "full"), "x", "scan", cron_dir=tmp_path / "other" / "cron")
assert _metric(build_cron_health_snapshot(), "hermes.cron.store.writable").value == 0
- store_health._degraded.clear()
+ register_ticked_homes([]) # the profile left this gateway: its store no longer counts
+ assert _metric(build_cron_health_snapshot(), "hermes.cron.store.writable").value == 1
@pytest.mark.parametrize("message", ["oauth refresh failed", "tokenizer crashed", "HTTP 4015"])
def test_error_classification_avoids_auth_substring_false_positives(message):