Skip to content

The agent that grows with you

fix(cron): skip dispatch, not crash, when the store cannot persist the advance

kshitijk4poor75b612ddb855
diff --git a/cron/scheduler.py b/cron/scheduler.py
index db29cde4415..6d96c5bba52 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -4281,7 +4281,14 @@ def _sweep_mcp_orphans() -> None:
def _process_due_job(job: dict, adapters, loop, verbose: bool) -> bool:
"""Run one due job via the shared ``run_one_job`` body."""
# Claim only when the worker actually starts, so a queued lease can't expire first.
- claimed = claim_job_for_fire(job["id"], return_job=True)
+ try:
+ claimed = claim_job_for_fire(job["id"], return_job=True)
+ except OSError as exc:
+ from cron.scheduler_tick import warn_store_unwritable
+ warn_store_unwritable(exc, f"job '{job.get('name') or job['id']}'")
+ finish_execution(
+ job["execution_id"], success=False, error=f"Cron store unwritable; not started: {exc}")
+ return False
if not claimed:
finish_execution(
job["execution_id"], success=False, error="Fire claim lost; execution was not started.")
diff --git a/cron/scheduler_tick.py b/cron/scheduler_tick.py
index 3afd054d671..ced12adc967 100644
--- a/cron/scheduler_tick.py
+++ b/cron/scheduler_tick.py
@@ -2,6 +2,27 @@
import concurrent.futures
import contextlib
+import logging
+import time
+
+logger = logging.getLogger("cron.scheduler")
+
+# One WARNING per errno per window: an unwritable store fails every 60s tick until fixed.
+_STORE_WARN_INTERVAL_SECONDS = 900.0
+_last_store_warning: dict = {}
+
+
+def warn_store_unwritable(exc: OSError, skipped: str) -> None:
+ """Rate-limited WARNING for a cron store write that failed (ENOSPC/EROFS/EACCES). The caller
+ skips the dispatch that needed the write: no job runs without a durable advance/fire claim."""
+ now = time.monotonic()
+ last = _last_store_warning.get(exc.errno)
+ if last is not None and now - last < _STORE_WARN_INTERVAL_SECONDS:
+ return
+ _last_store_warning[exc.errno] = now
+ logger.warning(
+ "Cron store is unwritable (%s); skipped %s. Jobs stay due and fire once the store "
+ "accepts writes again.", exc, skipped)
def tick(verbose=True, adapters=None, loop=None, sync=True, *, can_dispatch=None):
@@ -80,7 +101,16 @@ def _tick_admitted(
# Advance next_run_at for recurring jobs FIRST, under the lock, before any execution
# (at-most-once). Re-advancing running jobs keeps the grace window alive; mark_job_run
# overwrites it on completion. Composes with the claim-time advance in claim_job_for_fire.
- _sched.advance_next_runs([job["id"] for job in due_jobs])
+ try:
+ _sched.advance_next_runs([job["id"] for job in due_jobs])
+ except OSError as exc:
+ # No durable advance -> a crash mid-run would re-fire recurring jobs; skipping is the
+ # at-most-once side. One-shots still go through their own fire claim.
+ recurring = [j for j in due_jobs if j.get("schedule", {}).get("kind") in {"cron", "interval"}]
+ warn_store_unwritable(exc, f"{len(recurring)} recurring job(s)")
+ due_jobs = [j for j in due_jobs if j not in recurring]
+ if not due_jobs:
+ return 0
_max_workers = _sched._resolve_max_parallel_workers()
if verbose: