Skip to content

The agent that grows with you

fix(cron): keep caching bad-expr cron cadences; skip only import-failure None

kshitijk4poor8a1efff13601
diff --git a/cron/jobs.py b/cron/jobs.py
index 0f4be176762..56e83305819 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -40,7 +40,8 @@ from hermes_cli.observability.shared_metrics_gateway import record_cron_missed
from utils import atomic_replace, atomic_write_text, fsync_directory, mkstemp_beside
# croniter is imported lazily (slow import, only needed for cron exprs). HAS_CRONITER stays a
-# module attribute: a monkeypatched value wins because _ensure_croniter only probes while None.
+# module attribute: a monkeypatched value wins because _ensure_croniter only probes while None
+# (and past `_croniter_retry_at`).
croniter = None
HAS_CRONITER: Optional[bool] = None
# Monotonic deadline before the next croniter import probe after an ImportError (not latched).
@@ -2972,7 +2973,7 @@ def _recover_missing_next_run(job: Dict[str, Any], scan: _DueScan) -> Optional[s
"Job '%s' had no next_run_at; recovering %s run at %s",
job.get("name", job.get("id", "?")), recovery_kind, recovered_next)
fields: Dict[str, Any] = {"next_run_at": recovered_next}
- if recovery_kind in {"cron", "interval"} and _is_recoverable_error_job(job):
+ if _is_recoverable_error_job(job):
fields["state"] = "scheduled"
job.update(fields)
scan.persist(job["id"], **fields)
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 8557e267c5a..3deb53e654e 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -841,10 +841,12 @@ def _cron_interval_minutes(expr: str) -> Optional[float]:
if expr in _cron_interval_cache:
return _cron_interval_cache[expr]
result = None
+ ok = False
with contextlib.suppress(Exception):
from cron.jobs import _ensure_croniter
- if _ensure_croniter():
+ ok = _ensure_croniter()
+ if ok:
from cron.jobs import croniter as _croniter
from datetime import datetime
@@ -854,9 +856,10 @@ def _cron_interval_minutes(expr: str) -> Optional[float]:
second = it.get_next(datetime)
gap = (second - first).total_seconds() / 60.0
result = gap if gap > 0 else None
- # Cache only a real cadence: a None from a transient croniter ImportError must not pin the
- # floor allowance for this expr for the process lifetime once the import recovers.
- if result is not None:
+ # Cache a real cadence or a bad-expr None (croniter loaded, expr invalid: stable). Skip the
+ # None from a transient croniter ImportError so it can't pin the floor allowance for the
+ # process lifetime once the import recovers.
+ if result is not None or ok:
_cron_interval_cache[expr] = result
return result
diff --git a/tests/cron/test_jobs.py b/tests/cron/test_jobs.py
index 455538c4154..266915854cf 100644
--- a/tests/cron/test_jobs.py
+++ b/tests/cron/test_jobs.py
@@ -907,10 +907,15 @@ class TestMarkJobRun:
monkeypatch.setattr(jobs_mod, "_croniter_retry_at", 0.0)
assert jobs_mod._ensure_croniter() is False
assert jobs_mod.compute_next_run(job["schedule"]) is None
+ # The in-flight guard's cadence cache must not pin the import-failure None either.
+ import cron.scheduler as sched_mod
+ expr = job["schedule"]["expr"]
+ sched_mod._cron_interval_cache.pop(expr, None)
+ assert sched_mod._cron_interval_minutes(expr) is None
+ assert expr not in sched_mod._cron_interval_cache, "import-failure None was cached"
mark_job_run(job["id"], success=True) # leaves state=error, next_run_at=None
- # Window over (import works again): WITHOUT resetting HAS_CRONITER — the probe's
- # cached outcome must be re-evaluated, not latched.
+ # Window over (import works again); HAS_CRONITER is left untouched.
monkeypatch.setattr(builtins, "__import__", real_import)
# Inside the retry backoff the failed probe is not re-run on every call...
assert jobs_mod._ensure_croniter() is False