Skip to content

The agent that grows with you

fix(cron): stale-run reclaim acts only on the snapshot it judged

Maciej Lisiewski6b0e0a33a96e
diff --git a/cron/executions.py b/cron/executions.py
index 51fae88029d..a3dfc01f0dc 100644
--- a/cron/executions.py
+++ b/cron/executions.py
@@ -473,15 +473,19 @@ def recover_interrupted_executions() -> int:
< HANDOFF_ADOPTION_GRACE_SECONDS
):
continue
+ # The SELECT ran outside a write transaction and the liveness probe takes time: the
+ # owner may have stamped progress since, so act only on the snapshot that was judged.
cur = conn.execute(
"""UPDATE executions
SET status='unknown', finished_at=?, error=?,
handoff_pending=0, handoff_started_at=NULL
WHERE id=? AND status=? AND process_id=? AND pid=?
AND handoff_pending=?
- AND handoff_started_at IS ?""",
+ AND handoff_started_at IS ?
+ AND claimed_at=? AND progress_at IS ?""",
(now, reason, row["id"], row["status"], row["process_id"], row["pid"],
- row["handoff_pending"], row["handoff_started_at"]),
+ row["handoff_pending"], row["handoff_started_at"],
+ row["claimed_at"], row["progress_at"]),
)
changed += cur.rowcount
if cur.rowcount:
diff --git a/tests/cron/test_stale_running_recovery.py b/tests/cron/test_stale_running_recovery.py
index 63fd97d6641..d69f2017d8d 100644
--- a/tests/cron/test_stale_running_recovery.py
+++ b/tests/cron/test_stale_running_recovery.py
@@ -9,6 +9,7 @@ timeout is unlimited or not a finite positive number.
from __future__ import annotations
+import sqlite3
import time
import uuid
from datetime import timedelta
@@ -100,3 +101,26 @@ def test_live_owner_recent_claim_not_recovered(monkeypatch, timeout):
else:
assert executions_mod._live_owner_stale_after_seconds() is None
assert recovered == 0 and _status(ancient) == "running"
+
+
+def test_progress_stamped_during_the_sweep_keeps_the_run(monkeypatch):
+ """A worker that stamps progress after the sweep read its silent row is not reclaimed: the
+ reclaim UPDATE must not act on the stale snapshot (race audit threads-4: the live run would be
+ filed ``unknown`` and its job could fire a second time)."""
+ monkeypatch.setenv("HERMES_CRON_TIMEOUT", "600")
+ bound = executions_mod._live_owner_stale_after_seconds()
+ eid = _seed_running("job-resumed", age_seconds=bound + 3600)
+
+ def owner_resumes(pid, started_at):
+ # The external worker process (touch_execution_progress there) commits a fresh stamp
+ # while this sweep is probing its liveness.
+ worker = sqlite3.connect(executions_mod.EXECUTIONS_FILE)
+ with worker:
+ worker.execute("UPDATE executions SET progress_at=? WHERE id=?", (_hermes_now().isoformat(), eid))
+ worker.close()
+ return True
+
+ monkeypatch.setattr(executions_mod, "_owner_is_live", owner_resumes)
+
+ assert recover_interrupted_executions() == 0
+ assert _status(eid) == "running"