Skip to content

The agent that grows with you

fix(kanban): a timed-out managed run is booked timed_out, not crashed

Tekniumb183c54106ab
diff --git a/hermes_cli/kanban_db.py b/hermes_cli/kanban_db.py
index 5f5e79f07d7..fe890c6861b 100644
--- a/hermes_cli/kanban_db.py
+++ b/hermes_cli/kanban_db.py
@@ -2352,7 +2352,7 @@ def release_stale_claims(
"WHERE status = 'running' AND claim_expires IS NOT NULL "
" AND claim_expires < ?", (now,),
).fetchall()
- from hermes_cli.kanban_owner_recovery import owner_reclaim_blocked
+ from hermes_cli.kanban_owner_recovery import owner_reclaim_blocked, owner_reclaim_blocked_after_kill
for row in stale:
if owner_reclaim_blocked(conn, row["id"]):
continue
@@ -2380,7 +2380,7 @@ def release_stale_claims(
with write_txn(conn):
# Again under the write lock: an interpreter that bound since the check above
# changes neither claim_lock nor worker_pid, so the CAS below cannot see it.
- if owner_reclaim_blocked(conn, row["id"]):
+ if owner_reclaim_blocked_after_kill(conn, row["id"]):
continue
retry_status = _retry_status_for_run(conn, row["id"])
cur = conn.execute(
diff --git a/hermes_cli/kanban_db_dispatch.py b/hermes_cli/kanban_db_dispatch.py
index de395318696..e1ab0bb5a94 100644
--- a/hermes_cli/kanban_db_dispatch.py
+++ b/hermes_cli/kanban_db_dispatch.py
@@ -664,7 +664,7 @@ def enforce_max_runtime(conn: sqlite3.Connection, *, signal_fn=None) -> list[str
" AND COALESCE(r.started_at, t.started_at) IS NOT NULL "
" AND t.worker_pid IS NOT NULL"
).fetchall()
- from hermes_cli.kanban_owner_recovery import owner_reclaim_blocked
+ from hermes_cli.kanban_owner_recovery import owner_reclaim_blocked, owner_reclaim_blocked_after_kill
for row in rows:
if owner_reclaim_blocked(conn, row["id"]):
continue
@@ -704,7 +704,7 @@ def enforce_max_runtime(conn: sqlite3.Connection, *, signal_fn=None) -> list[str
with _kb.write_txn(conn):
# Again under the write lock, after the kill wait: an interpreter that bound
# meanwhile is invisible to the pid/claim CAS below.
- if owner_reclaim_blocked(conn, tid):
+ if owner_reclaim_blocked_after_kill(conn, tid):
continue
retry_status = _kb._retry_status_for_run(conn, tid)
cur = conn.execute(
@@ -779,7 +779,7 @@ def detect_stale_running(
"WHERE t.status = 'running'"
).fetchall()
- from hermes_cli.kanban_owner_recovery import owner_reclaim_blocked
+ from hermes_cli.kanban_owner_recovery import owner_reclaim_blocked, owner_reclaim_blocked_after_kill
for row in rows:
if owner_reclaim_blocked(conn, row["id"]):
continue
@@ -812,7 +812,7 @@ def detect_stale_running(
with _kb.write_txn(conn):
# Again under the write lock, after the termination wait (see enforce_max_runtime).
- if owner_reclaim_blocked(conn, tid):
+ if owner_reclaim_blocked_after_kill(conn, tid):
continue
retry_status = _kb._retry_status_for_run(conn, tid)
cur = conn.execute(
diff --git a/hermes_cli/kanban_owner_recovery.py b/hermes_cli/kanban_owner_recovery.py
index dfeaf633952..dc02ea918cd 100644
--- a/hermes_cli/kanban_owner_recovery.py
+++ b/hermes_cli/kanban_owner_recovery.py
@@ -162,6 +162,18 @@ def owner_reclaim_blocked(conn, task_id, *, crash_sweep=False) -> bool:
return not owner_attempt_provably_over(conn, task_id)
+def owner_reclaim_blocked_after_kill(conn, task_id) -> bool:
+ """The under-lock re-check for a reclaim path that has just signalled the run's worker.
+
+ Killing the worker takes its bound interpreter down with it (``_watch_submitter``), and the
+ owner then settles the admission ``unknown``: a pause this path caused itself, which would
+ leave a timed-out run for the crash sweep to misbook ``crashed``. Only an interpreter that is
+ still LIVE (one that bound after the first look) keeps the claim; a dead bound interpreter is
+ the one this path just stopped.
+ """
+ return owner_reclaim_blocked(conn, task_id, crash_sweep=True)
+
+
def managed_run_exit_code(conn, task_id, worker_pid, claim_lock):
"""The exit code a managed run's authority recorded for the task's CURRENT run, else None.
diff --git a/tests/hermes_cli/test_kanban_owner_receipt_reclaim.py b/tests/hermes_cli/test_kanban_owner_receipt_reclaim.py
index da04ba80ff4..280581cca00 100644
--- a/tests/hermes_cli/test_kanban_owner_receipt_reclaim.py
+++ b/tests/hermes_cli/test_kanban_owner_receipt_reclaim.py
@@ -234,6 +234,40 @@ def test_an_interpreter_bound_after_the_gate_keeps_its_claim(board, monkeypatch,
assert (row["status"], row["current_run_id"]) == ("running", run)
[email protected]("linux", "macos")
+def test_a_timed_out_managed_run_is_booked_timed_out_not_left_for_the_crash_sweep(board):
+ """Killing a managed run's worker takes its bound interpreter down with it, and the owner
+ settles the admission ``unknown``. The under-lock re-check must not read that pause, which
+ the timeout itself caused, as an owner still in flight: the run is ``timed_out``, never left
+ running for the crash sweep to book ``crashed`` on the next tick."""
+ worker, interpreter = (subprocess.Popen([sys.executable, "-c", "input()"], stdin=subprocess.PIPE)
+ for _ in range(2))
+ try:
+ run = board.dead_attempt(
+ "t_slow", worker_pid=worker.pid, worker_fingerprint=kbd._process_fingerprint(worker.pid),
+ owner_pid=os.getpid(), owner_birth=process_birth(psutil.Process()), admission=("started", None))
+ board.event("t_slow", run, "worker_bound", {"pid": interpreter.pid,
+ "started_at": kbd._process_fingerprint(interpreter.pid)}, NOW)
+ board.conn.execute("UPDATE tasks SET max_runtime_seconds = 1 WHERE id = 't_slow'")
+ board.conn.commit()
+
+ def sigterm_ends_the_attempt(pid, sig):
+ os.kill(pid, sig)
+ interpreter.kill() # _watch_submitter: the interpreter exits with its worker
+ interpreter.wait()
+ with sqlite3.connect(board.ledger) as store: # the owner settles the lost turn
+ store.execute("UPDATE session_admissions SET status = 'unknown'")
+
+ assert kbd.enforce_max_runtime(board.conn, signal_fn=sigterm_ends_the_attempt) == ["t_slow"]
+ outcome = board.conn.execute("SELECT outcome FROM task_runs WHERE id = ?", (run,)).fetchone()[0]
+ assert (board.state("t_slow")["status"], outcome) == ("ready", "timed_out")
+ assert kbd.detect_crashed_workers(board.conn) == []
+ finally:
+ for child in (worker, interpreter):
+ child.kill()
+ child.wait()
+
+
@pytest.mark.platforms("linux", "macos")
def test_an_exited_unreaped_owner_no_longer_holds_the_slot(board):
"""A zombie keeps its birth until reaped, but it has exited: it cannot be executing anything."""