Skip to content

The agent that grows with you

fix(cron): reap MCP orphans on the all-skipped unwritable-store tick

kshitijk4poorcb0f315d9670
diff --git a/cron/scheduler_tick.py b/cron/scheduler_tick.py
index f3b086c2e64..531b8337774 100644
--- a/cron/scheduler_tick.py
+++ b/cron/scheduler_tick.py
@@ -111,10 +111,11 @@ def _tick_admitted(
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"skipped {len(recurring)} recurring job(s)")
- due_jobs = [j for j in due_jobs if j not in recurring]
+ n = len(due_jobs)
+ due_jobs = [j for j in due_jobs if j.get("schedule", {}).get("kind") not in {"cron", "interval"}]
+ warn_store_unwritable(exc, f"skipped {n - len(due_jobs)} recurring job(s)")
if not due_jobs:
+ _sched._sweep_mcp_orphans()
return 0
_max_workers = _sched._resolve_max_parallel_workers()
diff --git a/tests/cron/test_due_scan_save_failure.py b/tests/cron/test_due_scan_save_failure.py
index e4d603f5856..36a39f92ef0 100644
--- a/tests/cron/test_due_scan_save_failure.py
+++ b/tests/cron/test_due_scan_save_failure.py
@@ -90,20 +90,23 @@ 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]
-def test_tick_on_unwritable_store_returns_cleanly_without_dispatch(cron_store, monkeypatch, caplog):
[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):
"""Real tick(): the advance cannot be persisted, so the recurring job is NOT run (at-most-once);
- the one-shot still reaches its fire claim, which fails closed with a ``failed`` execution row.
- The tick neither raises nor alters the store; a WARNING names the unwritable store."""
+ 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
+ was skipped), and a WARNING names the unwritable store."""
from cron import executions, scheduler
once = dict(_due_job("once"), schedule={"kind": "once", "run_at": FIXED_NOW.isoformat(), "display": "once"},
repeat={"times": 1, "completed": 0})
- save_jobs([_due_job(), _half_paused_job(), once])
+ save_jobs([_due_job(), _half_paused_job()] + ([once] if with_once else []))
before = load_jobs()
- ran = []
+ 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)
monkeypatch.setattr(scheduler, "_should_yield_tick_to_fresh_gateway", lambda: None)
+ monkeypatch.setattr(scheduler, "_sweep_mcp_orphans", lambda: sweeps.append(1))
monkeypatch.setattr(cronjobs, "_last_store_warning", {})
def _enospc(*_args, **_kwargs):
@@ -113,8 +116,11 @@ def test_tick_on_unwritable_store_returns_cleanly_without_dispatch(cron_store, m
with caplog.at_level(logging.WARNING, logger="cron.scheduler"):
assert scheduler.tick(verbose=False, sync=True) == 0
- assert ran == []
+ assert ran == [] and sweeps == [1]
assert f"Cron store {cron_store / 'cron'} is unwritable" in caplog.text
assert load_jobs() == before
row = executions.latest_execution("once")
- assert row["status"] == "failed" and "Cron store unwritable" in row["error"]
+ if with_once:
+ assert row["status"] == "failed" and "Cron store unwritable" in row["error"]
+ else:
+ assert row is None