Skip to content

The agent that grows with you

fix(cron): clear run_claim for one-shot jobs on dispatch failure (#86522)

RelaxJonh6d85f214ac7a
diff --git a/cron/jobs.py b/cron/jobs.py
index 205c040c379..1ef134fa800 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -2667,6 +2667,34 @@ def heartbeat_run_claim(job_id: str, *, expected_owner: str) -> bool:
return False
+def clear_run_claim(job_id: str) -> bool:
+ """Clear a one-shot job's ``run_claim`` when its dispatch fails.
+
+ ``get_due_jobs`` stamps a ``run_claim`` before returning a one-shot as
+ due (#59229). ``mark_job_run`` clears it on *successful* completion.
+ When dispatch itself fails (interpreter shutdown, executor submit error,
+ execution-creation error) the job never reaches ``mark_job_run`` and the
+ stale claim blocks re-dispatch until the TTL expires (default 30 min).
+
+ Calling this on every early-exit path restores the "the job stays due
+ and will fire on the next healthy tick" invariant that the scheduler
+ comment promises (#86522).
+ """
+ with _jobs_lock():
+ jobs = load_jobs()
+ for job in jobs:
+ if job.get("id") != job_id:
+ continue
+ if job.get("schedule", {}).get("kind") != "once":
+ return False
+ if job.get("run_claim") is not None:
+ job["run_claim"] = None
+ save_jobs(jobs)
+ return True
+ return False # already cleared
+ return False
+
+
def advance_next_runs(job_ids) -> int:
"""Batch form of :func:`advance_next_run` for the due-dispatch loop.
diff --git a/cron/scheduler.py b/cron/scheduler.py
index cdd0d269c65..e4f75c0710c 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -497,6 +497,7 @@ from cron.jobs import (
claim_dispatch,
claim_job_for_fire,
fire_claim_fence,
+ clear_run_claim,
get_due_jobs,
heartbeat_fire_claim,
heartbeat_run_claim,
@@ -6862,6 +6863,7 @@ def tick(
"Job '%s' not dispatched — interpreter is shutting down",
job.get("name", job_id),
)
+ clear_run_claim(job_id)
return None
if not try_register_running_job(job_id):
logger.info("Job '%s' already running — skipping", job.get("name", job_id))
@@ -6879,6 +6881,7 @@ def tick(
# audit requirement: every add is paired with guaranteed
# cleanup).
release_running_job(job_id)
+ clear_run_claim(job_id)
logger.exception(
"Job '%s' not dispatched: execution creation failed: %s",
job.get("name", job_id),
@@ -6896,6 +6899,7 @@ def tick(
fut = pool.submit(_run_and_release)
except Exception as submit_err:
release_running_job(job_id)
+ clear_run_claim(job_id)
finish_execution(
execution["id"],
success=False,