Skip to content

The agent that grows with you

fix(cron): keep ticking when load_jobs() cannot persist a structural repair

kshitijk4poore8b3960a9e3c
diff --git a/cron/jobs.py b/cron/jobs.py
index abae105bcdb..be9b49cebac 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -1457,8 +1457,11 @@ def load_jobs() -> List[Dict[str, Any]]:
logger.warning("%s", note)
# Keep the shrink-merge (a degraded-lock sibling's create may have landed since the read)
# unless disk is STILL a shape the merge would refuse: a sibling may have rewritten it.
- save_jobs(jobs, replace=unmergeable and _peek_jobs_unlocked() is None)
- logger.warning("Auto-repaired jobs.json (%s)", repair)
+ try:
+ save_jobs(jobs, replace=unmergeable and _peek_jobs_unlocked() is None)
+ logger.warning("Auto-repaired jobs.json (%s)", repair)
+ except OSError as exc: # repaired in memory; the next load retries the persist
+ warn_store_unwritable(exc, f"jobs.json repair ({repair}) not persisted", "load")
_record_load_stamp(pre_read_stamp)
return jobs
diff --git a/tests/cron/test_due_scan_save_failure.py b/tests/cron/test_due_scan_save_failure.py
index 85babe9af7c..9f2a87bff26 100644
--- a/tests/cron/test_due_scan_save_failure.py
+++ b/tests/cron/test_due_scan_save_failure.py
@@ -14,6 +14,7 @@ dispatch (no durable advance) without raising or mutating the store.
"""
import errno
+import json
import logging
import pytest
@@ -89,8 +90,9 @@ def test_due_jobs_are_returned_when_the_store_cannot_be_saved(cron_store, full_d
assert str(cron_store / "cron") in warnings[0] and str(cron_store / "other-profile") in warnings[1]
[email protected]("with_once", [True, False], ids=["one-shot-due", "recurring-only"])
-def test_tick_on_unwritable_store_returns_cleanly_without_dispatch(cron_store, monkeypatch, caplog, with_once):
[email protected]("with_once,junk", [(True, False), (False, False), (False, True)],
+ ids=["one-shot-due", "recurring-only", "load-repair"])
+def test_tick_on_unwritable_store_returns_cleanly_without_dispatch(cron_store, monkeypatch, caplog, with_once, junk):
"""Real tick(): the advance cannot be persisted, so the recurring job is NOT run (at-most-once);
a due one-shot still reaches its fire claim, which fails closed with a ``failed`` execution row.
The tick neither raises nor alters the store, still reaps MCP orphans (also when every due job
@@ -101,6 +103,9 @@ def test_tick_on_unwritable_store_returns_cleanly_without_dispatch(cron_store, m
repeat={"times": 1, "completed": 0})
save_jobs([_due_job(), _half_paused_job()] + ([once] if with_once else []))
before = load_jobs()
+ if junk: # load_jobs() drops a non-object entry and persists that repair: the save must not abort the tick
+ raw = json.loads(cronjobs.JOBS_FILE.read_text())
+ cronjobs.JOBS_FILE.write_text(json.dumps(dict(raw, jobs=raw["jobs"] + [42])))
ran, sweeps = [], []
monkeypatch.setattr(executions, "EXECUTIONS_FILE", cron_store / "cron" / "executions.db")
monkeypatch.setattr(scheduler, "run_one_job", lambda job, **k: ran.append(job["id"]) or True)