Skip to content

The agent that grows with you

fix(cron): degrade on a read-only tick lock, keep the recovery window until a scan saves

kshitijk4poor4ba0218e3a14
diff --git a/cron/jobs.py b/cron/jobs.py
index 24420971d37..8c132fe0e60 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -3346,13 +3346,14 @@ def _get_due_jobs_locked() -> List[Dict[str, Any]]:
"Skipping malformed cron job %r during due scan",
job.get("name") or job.get("id") or "?")
- store_health.end_recovery_window(_current_cron_store().cron_dir)
if scan.needs_save:
try:
save_jobs(raw_jobs, removed_ids=scan.removed or None)
except OSError as exc: # repairs live in memory; the next tick retries the persist
store_health.note_unwritable(exc, "due-scan repairs not persisted", "scan")
return due
+ # Only now is a skipped one-shot claimed or durably due on disk: normal grace applies again.
+ store_health.end_recovery_window(_current_cron_store().cron_dir)
for emit in scan.on_saved:
emit()
return due
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 3fa0365a4a5..4b852e5973f 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -4166,7 +4166,7 @@ def _acquire_tick_lock(lock_file):
"Cron tick could not acquire tick lock: %s — scheduler will "
"attempt fd reclamation and retry with backoff",
exc)
- else:
+ elif exc.errno not in store_health.UNWRITABLE_ERRNOS: # those degrade the store (tick caller)
logger.error("Cron tick could not acquire tick lock: %s", exc)
raise
diff --git a/cron/scheduler_tick.py b/cron/scheduler_tick.py
index 051aa956b35..9b48e12538c 100644
--- a/cron/scheduler_tick.py
+++ b/cron/scheduler_tick.py
@@ -57,7 +57,15 @@ def _tick_admitted(
lock_dir, lock_file = _sched._get_lock_paths()
_sched._ensure_cron_dir(lock_dir)
- lock_fd = _sched._acquire_tick_lock(lock_file)
+ try:
+ lock_fd = _sched._acquire_tick_lock(lock_file)
+ except OSError as exc:
+ if exc.errno not in store_health.UNWRITABLE_ERRNOS:
+ raise # EMFILE/ENFILE etc. stay a FAILED tick (#87644)
+ # Read-only/full/denied store: the lock file cannot be created, so nothing can dispatch.
+ store_health.note_unwritable(exc, "tick lock unavailable", "lock", cron_dir=lock_dir)
+ _sched._sweep_mcp_orphans()
+ return 0
if lock_fd is None:
return 0
diff --git a/cron/store_health.py b/cron/store_health.py
index a5349a388dd..c59dd2f9e67 100644
--- a/cron/store_health.py
+++ b/cron/store_health.py
@@ -25,6 +25,8 @@ from typing import Callable, Iterable, Optional
logger = logging.getLogger("cron.jobs")
PROBE_INTERVAL_SECONDS = 60.0
+# Store-write failures that degrade the store (the tick keeps running) instead of failing the tick.
+UNWRITABLE_ERRNOS = frozenset({errno.EROFS, errno.EACCES, errno.EPERM, errno.ENOSPC, errno.EDQUOT})
# Below this the probe reports ENOSPC: an empty temp file can still be created on a full disk.
_PROBE_MIN_FREE_BYTES = 1 << 20
# `hermes doctor` warns below this, before the store actually starts failing.
@@ -62,7 +64,7 @@ class StoreDegraded:
_DISPATCH_SITES = frozenset({"advance", "claim"})
_degraded: dict[str, StoreDegraded] = {}
-# Each store's just-ended outage, kept until the next due scan (``outage_covers``): the save that
+# Each store's just-ended outage, kept until a due scan's save lands (``outage_covers``): the save that
# ends an outage is often another job's heartbeat, landing before the scan that fires the
# one-shots the outage skipped.
_recovered: dict[str, StoreDegraded] = {}
@@ -104,8 +106,10 @@ def note_unwritable(exc: OSError, consequence: str, site: str, skipped_jobs: Ite
with _lock:
record = _degraded.get(store)
entered = record is None
- if entered:
- record = _degraded[store] = StoreDegraded(store, time.time(), describe_error(exc))
+ if entered: # an ended outage not yet seen by a saved scan carries over: keep its start
+ ended = _recovered.pop(store, None)
+ since = ended.since if ended is not None else time.time()
+ record = _degraded[store] = StoreDegraded(store, since, describe_error(exc))
new_site = site not in record.sites
record.sites.add(site)
# A failed dispatch write throttles the next attempt; once dispatch has been tried, a failed
@@ -179,7 +183,7 @@ def dispatch_blocked(due_jobs: list) -> bool:
def end_recovery_window(cron_dir: Path) -> None:
- """A due scan ran after the outage ended: later scans apply the normal grace window again."""
+ """A due scan's save landed after the outage ended: normal grace applies again."""
if _recovered:
with _lock:
_recovered.pop(str(cron_dir), None)
diff --git a/tests/cron/test_due_scan_save_failure.py b/tests/cron/test_due_scan_save_failure.py
index 1d34d3fe2ea..234bdb9cdbf 100644
--- a/tests/cron/test_due_scan_save_failure.py
+++ b/tests/cron/test_due_scan_save_failure.py
@@ -244,6 +244,10 @@ def test_skipped_oneshot_survives_recovery_by_any_save(cron_store, monkeypatch):
clock["now"] = FIXED_NOW + timedelta(minutes=10)
save_jobs(load_jobs() + [dict(_due_job("other"), next_run_at=(clock["now"] + timedelta(hours=1)).isoformat())])
assert store_health.degraded_record(cron_store / "cron") is None
+ with monkeypatch.context() as m: # fails again before the scan's save lands: still not missed
+ m.setattr(cronjobs, "_stage_jobs_payload", _enospc)
+ assert [d["id"] for d in get_due_jobs()] == ["once"]
+ save_jobs(load_jobs()) # second recovery
assert [d["id"] for d in get_due_jobs()] == ["once"]
stale = dict(once, id="stale") # due during the outage, first scanned after the recovery scan
save_jobs(load_jobs() + [stale])
@@ -301,9 +305,23 @@ def test_unwritable_store_is_shown_in_cron_status_and_announced_once(cron_store,
from gateway.cron_store_notices import install_cron_store_notices
from hermes_cli import cron as cron_cli
+ from cron import scheduler
+
save_jobs([_due_job()])
cron_dir = cron_store / "cron"
monkeypatch.setattr(cron_cli, "_active_cron_provider_name", lambda: "chronos")
+ monkeypatch.setattr(scheduler, "_should_yield_tick_to_fresh_gateway", lambda: None)
+ monkeypatch.setattr(scheduler, "_sweep_mcp_orphans", lambda: None)
+
+ def read_only_lock(_path):
+ raise OSError(errno.EROFS, "Read-only file system")
+
+ with monkeypatch.context() as m: # a read-only mount: .tick.lock cannot be opened for writing
+ m.setattr(scheduler, "_acquire_tick_lock", read_only_lock)
+ m.setattr(scheduler, "_get_lock_paths", lambda: (cron_dir, cron_dir / ".tick.lock"))
+ assert scheduler.tick(verbose=False, sync=True) == 0 # degrade, do not raise
+ assert "lock" in store_health.degraded_record(cron_dir).sites
+ store_health._degraded.clear()
os.chmod(cron_dir, 0o500)
try:
cron_cli.cron_status()