The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: guard claim cleanup best-effort + add #86522 regression tests
kshitij70fc5a5ee2d8
diff --git a/cron/scheduler.py b/cron/scheduler.pyindex e4f75c0710c..a457da1a5d9 100644--- a/cron/scheduler.py+++ b/cron/scheduler.py@@ -6853,6 +6853,26 @@ def tick(membership is released in the worker's finally block."""job_id = job["id"]++ def _clear_run_claim_best_effort() -> None:+ """Best-effort claim cleanup on the dispatch-failure paths.++ clear_run_claim does load_jobs/save_jobs file I/O; on the+ interpreter-shutdown path (or with a corrupt store) it can+ itself raise, and these early-exit paths exist precisely to+ skip cleanly — a stale claim expiring at the TTL is a better+ outcome than crashing the tick (#86522).+ """+ try:+ clear_run_claim(job_id)+ except Exception as claim_err:+ logger.warning(+ "Could not clear run_claim for job '%s' after dispatch "+ "failure: %s (claim will expire at TTL)",+ job.get("name", job_id),+ claim_err,+ )+# A tick can race gateway teardown: once the interpreter is# finalizing, ``pool.submit`` raises "cannot schedule new futures# after interpreter shutdown" and crashes the tick. Skip cleanly —@@ -6863,7 +6883,7 @@ def tick("Job '%s' not dispatched — interpreter is shutting down",job.get("name", job_id),)- clear_run_claim(job_id)+ _clear_run_claim_best_effort()return Noneif not try_register_running_job(job_id):logger.info("Job '%s' already running — skipping", job.get("name", job_id))@@ -6881,7 +6901,7 @@ def tick(# audit requirement: every add is paired with guaranteed# cleanup).release_running_job(job_id)- clear_run_claim(job_id)+ _clear_run_claim_best_effort()logger.exception("Job '%s' not dispatched: execution creation failed: %s",job.get("name", job_id),@@ -6899,7 +6919,7 @@ def tick(fut = pool.submit(_run_and_release)except Exception as submit_err:release_running_job(job_id)- clear_run_claim(job_id)+ _clear_run_claim_best_effort()finish_execution(execution["id"],success=False,diff --git a/tests/cron/test_oneshot_dispatch_failure_run_claim.py b/tests/cron/test_oneshot_dispatch_failure_run_claim.pynew file mode 100644index 00000000000..a16e5443233--- /dev/null+++ b/tests/cron/test_oneshot_dispatch_failure_run_claim.py@@ -0,0 +1,131 @@+"""Regression tests for #86522 — one-shot run_claim cleared on dispatch failure.++``get_due_jobs`` stamps a ``run_claim`` on one-shot jobs before returning them+as due (#59229); ``mark_job_run`` clears it on completion. When dispatch+itself fails (interpreter shutdown, execution-creation error, executor submit+error) the job never reaches ``mark_job_run`` and the stale claim blocked+re-dispatch until the TTL expired (default 30 min) — a precisely-timed+one-shot reminder arrived up to 30 minutes late with no error surfaced.++The fix (salvaged from PR #87591 by @RelaxJonh) adds+``cron.jobs.clear_run_claim`` and calls it on all three ``_submit_with_guard``+early-exit paths, wrapped best-effort so a failing store can never crash the+tick these paths exist to protect.+"""+from __future__ import annotations++import sys+from pathlib import Path+from unittest.mock import patch++import pytest++sys.path.insert(0, str(Path(__file__).parent.parent.parent))++import cron.jobs as jobs_mod+from cron.jobs import clear_run_claim+++def cron_store(tmp_path, monkeypatch):+ hermes_home = tmp_path / ".hermes"+ (hermes_home / "cron").mkdir(parents=True)+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))+ monkeypatch.setattr(jobs_mod, "HERMES_DIR", hermes_home)+ monkeypatch.setattr(jobs_mod, "CRON_DIR", hermes_home / "cron")+ monkeypatch.setattr(jobs_mod, "JOBS_FILE", hermes_home / "cron" / "jobs.json")+ monkeypatch.setattr(jobs_mod, "OUTPUT_DIR", hermes_home / "cron" / "output")+ return hermes_home+++def _make_oneshot(claimed: bool = True) -> dict:+ job = jobs_mod.create_job(prompt="remind me", schedule="30m")+ if claimed:+ jobs = jobs_mod.load_jobs()+ for j in jobs:+ if j["id"] == job["id"]:+ j["run_claim"] = {"at": "2026-08-17T10:00:00+00:00", "by": "test:1"}+ jobs_mod.save_jobs(jobs)+ return job+++class TestClearRunClaim:+ def test_clears_claim_on_oneshot(self, cron_store):+ job = _make_oneshot(claimed=True)+ assert clear_run_claim(job["id"]) is True+ reloaded = [j for j in jobs_mod.load_jobs() if j["id"] == job["id"]][0]+ assert reloaded.get("run_claim") is None++ def test_noop_when_already_clear(self, cron_store):+ job = _make_oneshot(claimed=False)+ assert clear_run_claim(job["id"]) is False++ def test_never_touches_recurring_jobs(self, cron_store):+ job = jobs_mod.create_job(prompt="tick", schedule="every 10m")+ jobs = jobs_mod.load_jobs()+ for j in jobs:+ if j["id"] == job["id"]:+ j["run_claim"] = {"at": "2026-08-17T10:00:00+00:00", "by": "test:1"}+ jobs_mod.save_jobs(jobs)+ assert clear_run_claim(job["id"]) is False+ reloaded = [j for j in jobs_mod.load_jobs() if j["id"] == job["id"]][0]+ assert reloaded.get("run_claim") is not None # untouched++ def test_unknown_job_id_returns_false(self, cron_store):+ assert clear_run_claim("no-such-job") is False+++class TestDispatchFailurePathsClearClaim:+ """Each _submit_with_guard early-exit must clear the one-shot claim so the+ next healthy tick re-dispatches instead of waiting out the 30-min TTL."""++ def _tick_one(self, job):+ from cron import scheduler as sched+ with patch.object(sched, "get_due_jobs", return_value=[dict(job)]):+ return sched.tick(verbose=False, sync=True)++ def test_interpreter_shutdown_path_clears_claim(self, cron_store):+ from cron import scheduler as sched+ job = _make_oneshot(claimed=True)+ with patch.object(sched, "_interpreter_shutting_down", return_value=True):+ self._tick_one(job)+ reloaded = [j for j in jobs_mod.load_jobs() if j["id"] == job["id"]][0]+ assert reloaded.get("run_claim") is None, (+ "shutdown-path dispatch failure must clear run_claim (#86522)"+ )++ def test_execution_creation_failure_clears_claim(self, cron_store):+ from cron import scheduler as sched+ job = _make_oneshot(claimed=True)+ with patch.object(sched, "create_execution", side_effect=RuntimeError("db gone")):+ self._tick_one(job)+ reloaded = [j for j in jobs_mod.load_jobs() if j["id"] == job["id"]][0]+ assert reloaded.get("run_claim") is None+ assert job["id"] not in sched.get_running_job_ids()++ def test_submit_failure_clears_claim(self, cron_store):+ from cron import scheduler as sched+ job = _make_oneshot(claimed=True)++ class _ExplodingPool:+ def submit(self, *a, **k):+ raise RuntimeError("cannot schedule new futures")++ pool = _ExplodingPool()+ with patch.object(sched, "_get_parallel_pool", return_value=pool), \+ patch.object(sched, "_get_sequential_pool", return_value=pool):+ self._tick_one(job)+ reloaded = [j for j in jobs_mod.load_jobs() if j["id"] == job["id"]][0]+ assert reloaded.get("run_claim") is None+ assert job["id"] not in sched.get_running_job_ids()++ def test_clear_failure_is_best_effort_not_fatal(self, cron_store):+ """A raising clear_run_claim (corrupt store, teardown I/O error) must+ not crash the tick — these early-exit paths exist to skip cleanly; the+ claim then simply expires at the TTL."""+ from cron import scheduler as sched+ job = _make_oneshot(claimed=True)+ with patch.object(sched, "_interpreter_shutting_down", return_value=True), \+ patch.object(sched, "clear_run_claim", side_effect=OSError(24, "Too many open files")):+ n = self._tick_one(job) # must not raise+ assert n == 0