The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): back off croniter re-probes for 60s after an ImportError
kshitijk4poordfc289bc2844
diff --git a/cron/jobs.py b/cron/jobs.pyindex f7aa9c64fb5..0f4be176762 100644--- a/cron/jobs.py+++ b/cron/jobs.py@@ -43,6 +43,9 @@ from utils import atomic_replace, atomic_write_text, fsync_directory, mkstemp_be# module attribute: a monkeypatched value wins because _ensure_croniter only probes while None.croniter = NoneHAS_CRONITER: Optional[bool] = None+# Monotonic deadline before the next croniter import probe after an ImportError (not latched).+_CRONITER_RETRY_SECONDS = 60.0+_croniter_retry_at: float = 0.0def _ensure_croniter() -> bool:@@ -51,16 +54,17 @@ def _ensure_croniter() -> bool:An ImportError is NOT latched: caching False would pin a single transient failure(wrong interpreter, shadowed path) for the whole process lifetime, leaving everyrecurring job's next_run_at None until a gateway restart (#127182). Stay None and- re-probe on the next call — the finder fails fast when the package is genuinely- absent, and due-scan recovery re-arms the schedules as soon as the import succeeds."""- global croniter, HAS_CRONITER- if HAS_CRONITER is None:+ re-probe once ``_CRONITER_RETRY_SECONDS`` have passed (so a genuinely absent package does+ not cost a finder walk per call on every due-scan tick), and due-scan recovery re-arms the+ schedules as soon as the import succeeds. An explicit True/False override always wins."""+ global croniter, HAS_CRONITER, _croniter_retry_at+ if HAS_CRONITER is None and time.monotonic() >= _croniter_retry_at:try:from croniter import croniter as _cronitercroniter = _croniterHAS_CRONITER = Trueexcept ImportError:- pass+ _croniter_retry_at = time.monotonic() + _CRONITER_RETRY_SECONDSreturn bool(HAS_CRONITER)@@ -2964,12 +2968,11 @@ def _recover_missing_next_run(job: Dict[str, Any], scan: _DueScan) -> Optional[srecovery_kind = kindif not recovered_next:return None- job["next_run_at"] = recovered_nextlogger.info("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 job.get("state") == "error":+ if recovery_kind in {"cron", "interval"} and _is_recoverable_error_job(job):fields["state"] = "scheduled"job.update(fields)scan.persist(job["id"], **fields)diff --git a/tests/cron/test_jobs.py b/tests/cron/test_jobs.pyindex 10380cc8ef6..455538c4154 100644--- a/tests/cron/test_jobs.py+++ b/tests/cron/test_jobs.py@@ -893,7 +893,7 @@ class TestMarkJobRun:job = create_job(prompt="Recurring", schedule="0 7,15,23 * * *")assert job["schedule"]["kind"] == "cron"- # Simulate the transient failure window: the probe import raises once.+ # Simulate the transient failure window: the croniter import raises while patched in.real_import = builtins.__import__def failing_import(name, *args, **kwargs):@@ -904,6 +904,7 @@ class TestMarkJobRun:monkeypatch.setattr(builtins, "__import__", failing_import)monkeypatch.setattr(jobs_mod, "croniter", None)monkeypatch.setattr(jobs_mod, "HAS_CRONITER", None)+ monkeypatch.setattr(jobs_mod, "_croniter_retry_at", 0.0)assert jobs_mod._ensure_croniter() is Falseassert jobs_mod.compute_next_run(job["schedule"]) is Nonemark_job_run(job["id"], success=True) # leaves state=error, next_run_at=None@@ -911,6 +912,10 @@ class TestMarkJobRun:# Window over (import works again): WITHOUT resetting HAS_CRONITER — the probe's# cached outcome must be re-evaluated, not latched.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+ # ...but once the backoff elapses it is re-evaluated, not latched.+ monkeypatch.setattr(jobs_mod, "_croniter_retry_at", 0.0)assert jobs_mod._ensure_croniter() is True, ("a transient ImportError was latched: every recurring job would stay ""next_run_at=None until a gateway restart"