Skip to content

The agent that grows with you

perf(cron): skip run_claim clear for recurring jobs on dispatch failure

kshitijbaa1cfb89de1
diff --git a/cron/scheduler.py b/cron/scheduler.py
index a457da1a5d9..efc96e67eaa 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -6857,12 +6857,20 @@ def tick(
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).
+ Only one-shot jobs carry a ``run_claim`` (stamped by
+ get_due_jobs, #59229), so recurring jobs skip the call
+ entirely — clear_run_claim acquires _jobs_lock (blocking
+ cross-process flock) and does a full load_jobs read, and the
+ dispatch-failure paths fire exactly when the process can
+ least afford N pointless lock/read round-trips (interpreter
+ shutdown, EMFILE). clear_run_claim itself does
+ load_jobs/save_jobs file I/O; on those degraded paths it can
+ raise, and these early-exits exist precisely to skip cleanly
+ — a stale claim expiring at the TTL is a better outcome than
+ crashing the tick (#86522).
"""
+ if (job.get("schedule") or {}).get("kind") != "once":
+ return
try:
clear_run_claim(job_id)
except Exception as claim_err:
diff --git a/tests/cron/test_oneshot_dispatch_failure_run_claim.py b/tests/cron/test_oneshot_dispatch_failure_run_claim.py
index a16e5443233..62aa35dea08 100644
--- a/tests/cron/test_oneshot_dispatch_failure_run_claim.py
+++ b/tests/cron/test_oneshot_dispatch_failure_run_claim.py
@@ -129,3 +129,15 @@ class TestDispatchFailurePathsClearClaim:
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
+
+ def test_recurring_dispatch_failure_skips_claim_io(self, cron_store):
+ """Recurring jobs carry no run_claim, so the dispatch-failure paths
+ must not pay clear_run_claim's lock acquisition + full jobs-file read
+ for a guaranteed no-op — the failure paths fire exactly when the
+ process can least afford pointless I/O (shutdown, EMFILE)."""
+ from cron import scheduler as sched
+ job = jobs_mod.create_job(prompt="hourly", schedule="every 1h")
+ with patch.object(sched, "_interpreter_shutting_down", return_value=True), \
+ patch.object(sched, "clear_run_claim") as mock_clear:
+ self._tick_one(job)
+ mock_clear.assert_not_called()