Skip to content

The agent that grows with you

fix(cron): refuse to overwrite a corrupt jobs store on save

kshitijk4poor553388b320cf
diff --git a/cron/jobs.py b/cron/jobs.py
index 1562ec44725..2a525292b16 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -36,7 +36,7 @@ logger = logging.getLogger(__name__)
from hermes_time import now as _hermes_now
from hermes_time import get_timezone
-from utils import atomic_replace, atomic_write_text
+from utils import atomic_replace, atomic_write_text, fsync_directory
# 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.
@@ -1412,7 +1412,7 @@ def load_jobs() -> List[Dict[str, Any]]:
return load_jobs()
for note in notes:
logger.warning("%s", note)
- save_jobs(jobs)
+ save_jobs(jobs, replace=True) # locked full-store read: the repaired list is authoritative
logger.warning("Auto-repaired jobs.json (%s)", repair)
_record_load_stamp(pre_read_stamp)
return jobs
@@ -1534,6 +1534,9 @@ def _save_jobs_unlocked(
recovery)."""
jobs_file = _current_cron_store().jobs_file
ensure_dirs()
+ # Fail closed: merging against an unreadable store would silently overwrite every job in it.
+ if not replace and jobs_file.exists() and _peek_jobs_unlocked() is None:
+ raise RuntimeError(f"Cron database corrupted; refusing to overwrite {jobs_file}")
# Owner snapshot BEFORE replace so a root writer can hand the file back to the gateway user.
_stat_before = None
for probe in (jobs_file, jobs_file.parent):
@@ -1559,6 +1562,7 @@ def _save_jobs_unlocked(
continue
atomic_replace(tmp_path, jobs_file)
tmp_path = None
+ fsync_directory(jobs_file.parent)
_secure_file(jobs_file)
_preserve_file_ownership(jobs_file, _stat_before)
# Invalidate (never refresh) the stamp: a refresh would let a nested save certify disk
diff --git a/tests/cron/test_jobs_shrink_merge.py b/tests/cron/test_jobs_shrink_merge.py
index 948aeace5b6..e7e94bc9b94 100644
--- a/tests/cron/test_jobs_shrink_merge.py
+++ b/tests/cron/test_jobs_shrink_merge.py
@@ -167,16 +167,22 @@ def test_merge_does_not_mutate_caller_list(hermes_env):
assert my_payload == [], "caller's list was mutated in place by the merge"
-def test_corrupt_disk_file_does_not_break_save(hermes_env):
- """A corrupt jobs.json under a save must not recurse or crash: the
- non-repairing peek returns None and the save overwrites cleanly."""
+def test_save_over_corrupt_store_fails_closed(hermes_env):
+ """A merging save over an unreadable jobs.json must refuse (the jobs in it
+ are unknown, so overwriting would drop them) and leave the bytes intact;
+ ``replace=True`` stays available as the explicit recovery rewrite."""
import cron.jobs as jobs
from cron.jobs import load_jobs, save_jobs
jobs.ensure_dirs()
jobs_file = jobs._current_cron_store().jobs_file
- jobs_file.write_text('{"jobs": [{"id": "ccc', encoding="utf-8")
- save_jobs([{"id": "aaaaaaaaaaaa", "name": "a"}])
+ corrupt = b'{"jobs": [{"id": "ccc'
+ jobs_file.write_bytes(corrupt)
+ with pytest.raises(RuntimeError):
+ save_jobs([{"id": "aaaaaaaaaaaa", "name": "a"}])
+ assert jobs_file.read_bytes() == corrupt
+
+ save_jobs([{"id": "aaaaaaaaaaaa", "name": "a"}], replace=True)
assert [j["id"] for j in load_jobs()] == ["aaaaaaaaaaaa"]