Skip to content

The agent that grows with you

fix: the wedged-cron drain check parses jobs.json once per run (review follow-up)

teknium127144dd59a1b
diff --git a/cron/scheduler.py b/cron/scheduler.py
index c51d24de848..fd4351cbdfa 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -527,6 +527,8 @@ _running_lock = threading.Lock()
# Per in-flight id: time.time() claim instant + the future owning its release (``_FUTURE_PENDING``
# until pool.submit returns). Past-allowance with no live future = leak; the sweep force-releases.
_running_since: dict = {}
+# job_id -> stale-inflight allowance (s), resolved once per run by get_wedged_job_ids.
+_running_allowance_s: dict = {}
_running_futures: dict = {}
# Installed in ``_running_futures`` at claim time so a sweep landing before ``pool.submit`` returns
@@ -612,20 +614,28 @@ def get_wedged_job_ids() -> "frozenset[str]":
now = time.time()
with _running_lock:
ages = {jid: now - started for jid, started in _running_since.items() if jid in _running_job_ids}
+ allowances = {jid: _running_allowance_s[jid] for jid in ages if jid in _running_allowance_s}
if not ages:
return frozenset()
floor_seconds = _inflight_min_allowance_minutes() * 60.0
- wedged = set()
- for job_id, age in ages.items():
- allowance = floor_seconds
+ unresolved = [jid for jid in ages if jid not in allowances]
+ if unresolved:
+ # One jobs.json parse per run, not per tick per job: the restart drain polls this every
+ # 0.1 s on the event loop for the whole wait, and get_job() re-reads the file each call.
+ by_id: dict = {}
with contextlib.suppress(Exception):
- from cron.jobs import get_job
- interval_minutes = _job_interval_minutes(get_job(job_id) or {})
- if interval_minutes:
- allowance = max(allowance, 2.0 * interval_minutes * 60.0)
- if age >= allowance:
- wedged.add(job_id)
- return frozenset(wedged)
+ from cron.jobs import load_jobs
+ by_id = {j.get("id"): j for j in load_jobs()}
+ with _running_lock:
+ for job_id in unresolved:
+ allowance = floor_seconds
+ interval_minutes = _job_interval_minutes(by_id.get(job_id) or {})
+ if interval_minutes:
+ allowance = max(allowance, 2.0 * interval_minutes * 60.0)
+ allowances[job_id] = allowance
+ if job_id in _running_job_ids: # released meanwhile -> don't resurrect the entry
+ _running_allowance_s[job_id] = allowance
+ return frozenset(jid for jid, age in ages.items() if age >= max(allowances[jid], floor_seconds))
def try_register_running_job(job_id: str) -> bool:
@@ -658,6 +668,7 @@ def release_running_job(job_id: str) -> None:
with _running_lock:
_running_job_ids.discard(job_id)
_running_since.pop(job_id, None)
+ _running_allowance_s.pop(job_id, None)
_running_futures.pop(job_id, None)
_running_worker_pids.pop(job_id, None)
@@ -889,6 +900,7 @@ def sweep_stale_inflight(due_jobs: Optional[list] = None) -> list:
continue
_running_job_ids.discard(job_id)
_running_since.pop(job_id, None)
+ _running_allowance_s.pop(job_id, None)
_running_futures.pop(job_id, None)
_forced_release_count += 1
stale.append((job_id, age, allowance, fut, reason))
diff --git a/tests/gateway/test_restart_drain.py b/tests/gateway/test_restart_drain.py
index e1fc0ae9136..a524b8763d8 100644
--- a/tests/gateway/test_restart_drain.py
+++ b/tests/gateway/test_restart_drain.py
@@ -528,7 +528,7 @@ async def test_request_restart_skips_wait_for_cron_run_past_inflight_allowance(m
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
monkeypatch.delenv("HERMES_AGENT_TIMEOUT", raising=False)
- monkeypatch.setattr("cron.jobs.get_job", lambda job_id: None)
+ monkeypatch.setattr("cron.jobs.load_jobs", lambda: [])
runner, _adapter = make_restart_runner()
runner.stop = AsyncMock()
runner._restart_after_turn_timeout = 300.0 # would hang the test without the wedge bypass
@@ -552,7 +552,7 @@ def test_wedged_cron_allowance_honours_young_runs_and_job_interval(monkeypatch,
import cron.scheduler as sched
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
- monkeypatch.setattr("cron.jobs.get_job", lambda job_id: {"id": job_id, "schedule": {"kind": "interval", "minutes": 360}})
+ monkeypatch.setattr("cron.jobs.load_jobs", lambda: [{"id": "six-hourly-job", "schedule": {"kind": "interval", "minutes": 360}}])
runner, _adapter = make_restart_runner()
assert sched.try_register_running_job("six-hourly-job")
try:
@@ -565,3 +565,28 @@ def test_wedged_cron_allowance_honours_young_runs_and_job_interval(monkeypatch,
assert runner._wedged_agent_count() == 1 and runner._awaitable_work_count() == 0
finally:
sched.release_running_job("six-hourly-job")
+
+
+def test_wedged_cron_check_parses_jobs_once_per_run(monkeypatch, tmp_path):
+ """The restart drain polls the wedged count every 0.1 s on the event loop; the job interval
+ must be resolved once per in-flight run, not by a full jobs.json parse per job per tick."""
+ import cron.scheduler as sched
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ loads = []
+ monkeypatch.setattr("cron.jobs.load_jobs", lambda: loads.append(1) or [
+ {"id": jid, "schedule": {"kind": "interval", "minutes": 360}} for jid in ("job-a", "job-b", "job-c")])
+ for jid in ("job-a", "job-b", "job-c"):
+ assert sched.try_register_running_job(jid)
+ try:
+ for _ in range(50):
+ assert sched.get_wedged_job_ids() == frozenset()
+ assert len(loads) == 1
+ with sched._running_lock:
+ sched._running_since["job-b"] = time.time() - 13 * 3600
+ assert sched.get_wedged_job_ids() == frozenset({"job-b"})
+ assert len(loads) == 1
+ finally:
+ for jid in ("job-a", "job-b", "job-c"):
+ sched.release_running_job(jid)
+ assert not sched._running_allowance_s