Skip to content

The agent that grows with you

fix(kanban): worker liveness and kills require the spawn-time start fingerprint

teknium17abe9502eee1
diff --git a/hermes_cli/kanban_db.py b/hermes_cli/kanban_db.py
index 53a22a0e063..966f5f42dee 100644
--- a/hermes_cli/kanban_db.py
+++ b/hermes_cli/kanban_db.py
@@ -876,6 +876,10 @@ CREATE TABLE IF NOT EXISTS tasks (
-- exceeds DEFAULT_FAILURE_LIMIT consecutive non-successes.
consecutive_failures INTEGER NOT NULL DEFAULT 0,
worker_pid INTEGER,
+ -- Start-time fingerprint of worker_pid (gateway.status.get_process_start_time) recorded at
+ -- spawn: liveness and kills require pid AND fingerprint to agree, so a PID recycled after a
+ -- reboot is never read as our worker or signalled. NULL = legacy row (pre-fingerprint spawn).
+ worker_started_at INTEGER,
-- Short excerpt of the most recent failure's error text.
last_failure_error TEXT,
max_runtime_seconds INTEGER,
@@ -2303,7 +2307,7 @@ def release_stale_claims(conn: sqlite3.Connection, *, signal_fn=None) -> int:
reclaimed = 0
host_prefix = _host_prefix()
stale = conn.execute(
- "SELECT id, claim_lock, worker_pid, claim_expires, last_heartbeat_at, "
+ "SELECT id, claim_lock, worker_pid, worker_started_at, claim_expires, last_heartbeat_at, "
" assignee "
"FROM tasks "
"WHERE status = 'running' AND claim_expires IS NOT NULL "
@@ -2315,12 +2319,14 @@ def release_stale_claims(conn: sqlite3.Connection, *, signal_fn=None) -> int:
# Backstop: a heartbeat older than the max-stale threshold means no
# observable progress — reclaim even if the PID is alive (logic loop).
heartbeat_stale = hb is not None and (now - int(hb)) > DEFAULT_CLAIM_HEARTBEAT_MAX_STALE_SECONDS
- if host_local and row["worker_pid"] and _pid_alive(row["worker_pid"]) and not heartbeat_stale:
+ started_at = _row_get(row, "worker_started_at")
+ if (host_local and row["worker_pid"] and _worker_alive(row["worker_pid"], started_at)
+ and not heartbeat_stale):
_extend_live_stale_claim(conn, row, now)
continue
termination = _terminate_reclaimed_worker(
- row["worker_pid"], row["claim_lock"], signal_fn=signal_fn,
+ row["worker_pid"], row["claim_lock"], signal_fn=signal_fn, started_at=started_at,
)
# A live worker of ours must keep its claim (else a duplicate spawns beside it).
if _worker_survived_termination(termination):
@@ -2414,7 +2420,7 @@ def reclaim_task(
"""Operator reclaim regardless of TTL: release the claim, restore the source
phase, reset the failure counter. False when not running."""
row = conn.execute(
- "SELECT status, claim_lock, worker_pid FROM tasks WHERE id = ?", (task_id,),
+ "SELECT status, claim_lock, worker_pid, worker_started_at FROM tasks WHERE id = ?", (task_id,),
).fetchone()
if not row:
return False
@@ -2422,7 +2428,8 @@ def reclaim_task(
# Nothing to reclaim — already ready / blocked / done.
return False
prev_lock = row["claim_lock"]
- termination = _terminate_reclaimed_worker(row["worker_pid"], prev_lock, signal_fn=signal_fn)
+ termination = _terminate_reclaimed_worker(
+ row["worker_pid"], prev_lock, signal_fn=signal_fn, started_at=row["worker_started_at"])
with write_txn(conn):
retry_status = _retry_status_for_run(conn, task_id)
cur = conn.execute(
@@ -3401,12 +3408,12 @@ def invalidate_descendants_for_parent_reopen(
action), the opposite of :func:`reopen_review_task`.
Returns ``{"invalidated": [{id, prior_status, new_status, resume_status}],
- "terminations": [(worker_pid, claim_lock)]}``.
+ "terminations": [(worker_pid, claim_lock, worker_started_at)]}``.
"""
caller_owns_txn = bool(conn.in_transaction)
now = int(time.time())
invalidated: list[dict[str, Any]] = []
- terminations: list[tuple[Optional[int], Optional[str]]] = []
+ terminations: list[tuple[Optional[int], Optional[str], Optional[int]]] = []
with write_txn(conn, allow_nested=True):
rows = conn.execute(
"""
@@ -3417,7 +3424,7 @@ def invalidate_descendants_for_parent_reopen(
FROM task_links l
JOIN descendants d ON d.id = l.parent_id
)
- SELECT t.id, t.status, t.current_run_id, t.worker_pid, t.claim_lock
+ SELECT t.id, t.status, t.current_run_id, t.worker_pid, t.claim_lock, t.worker_started_at
FROM descendants d
JOIN tasks t ON t.id = d.id
ORDER BY t.id
@@ -3434,7 +3441,7 @@ def invalidate_descendants_for_parent_reopen(
resume_status = "review"
elif previous_status == "running":
resume_status = _retry_status_for_run(conn, row["id"], row["current_run_id"])
- terminations.append((row["worker_pid"], row["claim_lock"]))
+ terminations.append((row["worker_pid"], row["claim_lock"], row["worker_started_at"]))
run_id = _end_run(
conn, row["id"], outcome="reclaimed", status="todo",
summary=f"ancestor {task_id} reopened",
@@ -3474,8 +3481,8 @@ def invalidate_descendants_for_parent_reopen(
if not caller_owns_txn:
# Standalone: committed above, audit trail durable, safe to kill now.
# Composed calls leave this to the caller post-commit.
- for pid, claim_lock in terminations:
- _terminate_reclaimed_worker(pid, claim_lock)
+ for pid, claim_lock, started_at in terminations:
+ _terminate_reclaimed_worker(pid, claim_lock, started_at=started_at)
return {"invalidated": invalidated, "terminations": terminations}
@@ -3552,13 +3559,13 @@ def archive_task(conn: sqlite3.Connection, task_id: str, *, signal_fn=None) -> b
"""
with write_txn(conn):
row = conn.execute(
- "SELECT status, claim_lock, worker_pid FROM tasks WHERE id = ?",
+ "SELECT status, claim_lock, worker_pid, worker_started_at FROM tasks WHERE id = ?",
(task_id,),
).fetchone()
if not row:
return False
was_running = row["status"] == "running"
- prev_pid, prev_lock = row["worker_pid"], row["claim_lock"]
+ prev_pid, prev_lock, prev_started = row["worker_pid"], row["claim_lock"], row["worker_started_at"]
cur = conn.execute(
"UPDATE tasks SET status = 'archived', "
" claim_lock = NULL, claim_expires = NULL, worker_pid = NULL "
@@ -3573,7 +3580,7 @@ def archive_task(conn: sqlite3.Connection, task_id: str, *, signal_fn=None) -> b
)
_append_event(conn, task_id, "archived", None, run_id=run_id)
if was_running:
- termination = _terminate_reclaimed_worker(prev_pid, prev_lock, signal_fn=signal_fn)
+ termination = _terminate_reclaimed_worker(prev_pid, prev_lock, signal_fn=signal_fn, started_at=prev_started)
with write_txn(conn):
_append_event(conn, task_id, "archive_worker_termination", termination, run_id=run_id)
# ``archived`` parents no longer block children; promote them now.
@@ -4120,6 +4127,7 @@ from hermes_cli.kanban_db_dispatch import ( # noqa: E402
_defer_reclaim_for_live_worker,
_pid_alive,
_terminate_reclaimed_worker,
+ _worker_alive,
_worker_survived_termination,
_worker_terminal_timeout_env,
)
diff --git a/hermes_cli/kanban_db_connect.py b/hermes_cli/kanban_db_connect.py
index 9b9eaad49db..998d96abbb6 100644
--- a/hermes_cli/kanban_db_connect.py
+++ b/hermes_cli/kanban_db_connect.py
@@ -813,6 +813,8 @@ _LATER_TASK_COLUMNS = (
# Typed block reason (VALID_BLOCK_KINDS); NULL = generic human blocker.
("block_kind", "block_kind TEXT"),
("block_recurrences", "block_recurrences INTEGER NOT NULL DEFAULT 0"),
+ # Spawn-time start fingerprint of worker_pid (PID-reuse guard; NULL = legacy row).
+ ("worker_started_at", "worker_started_at INTEGER"),
)
_NOTIFY_SUB_COLUMNS = (
diff --git a/hermes_cli/kanban_db_dispatch.py b/hermes_cli/kanban_db_dispatch.py
index d0b0056c4d1..ed37025ac2b 100644
--- a/hermes_cli/kanban_db_dispatch.py
+++ b/hermes_cli/kanban_db_dispatch.py
@@ -256,6 +256,30 @@ def _pid_alive(pid: Optional[int]) -> bool:
return True
+def _worker_alive(pid: Optional[int], started_at: Optional[int]) -> bool:
+ """True when ``pid`` is live AND is still the worker we spawned. ``started_at`` is the start-time
+ fingerprint recorded by ``_set_worker_pid``; after a reboot (or any PID recycle) an unrelated
+ process can own the number, so bare existence is never enough to extend a claim or to signal.
+ A legacy row without a fingerprint keeps the existence answer: killing it is the pre-fingerprint
+ behaviour and the row is rewritten with a fingerprint on its next spawn."""
+ return _kb._pid_alive(pid) and not _pid_recycled(pid, started_at)
+
+
+def _pid_recycled(pid: Optional[int], started_at: Optional[int]) -> bool:
+ """True when a live ``pid`` is NOT the process fingerprinted at spawn (or the fingerprint can no
+ longer be read). Signalling it would hit a stranger. ``None`` fingerprint = legacy row, never recycled."""
+ if started_at is None or not pid:
+ return False
+ from gateway.status import _start_times_agree, get_process_start_time
+ current = get_process_start_time(int(pid))
+ if current is None:
+ return True
+ try:
+ return not _start_times_agree(current, started_at)
+ except (TypeError, ValueError):
+ return True
+
+
def _kill_fn(signal_fn) -> Optional[Callable[[int, int], None]]:
"""``signal_fn`` test hook, else ``os.kill`` when the platform has one."""
if signal_fn is not None:
@@ -263,10 +287,10 @@ def _kill_fn(signal_fn) -> Optional[Callable[[int, int], None]]:
return os.kill if hasattr(os, "kill") else None
-def _poll_worker_exit(pid: int) -> bool:
+def _poll_worker_exit(pid: int, started_at: Optional[int] = None) -> bool:
"""Poll ~5 s (10 x 0.5 s) for ``pid`` to die; True once it is gone."""
for _ in range(10):
- if not _kb._pid_alive(pid):
+ if not _worker_alive(pid, started_at):
return True
time.sleep(0.5)
return False
@@ -287,8 +311,11 @@ def _terminate_reclaimed_worker(
claim_lock: Optional[str],
*,
signal_fn=None,
+ started_at: Optional[int] = None,
) -> dict[str, Any]:
- """Best-effort host-local worker termination for reclaim paths."""
+ """Best-effort host-local worker termination for reclaim paths. ``started_at`` is the spawn-time
+ fingerprint: when the live process no longer matches it, the PID was recycled and nothing is
+ signalled — the worker is gone, which is what the reclaim wanted (``terminated`` = True)."""
info: dict[str, Any] = {
"prev_pid": int(pid) if pid else None,
"host_local": False,
@@ -305,6 +332,10 @@ def _terminate_reclaimed_worker(
kill = _kill_fn(signal_fn)
if kill is None:
return info
+ if _kb._pid_alive(pid) and _pid_recycled(pid, started_at):
+ info["terminated"] = True
+ info["pid_recycled"] = True
+ return info
info["termination_attempted"] = True
try:
@@ -317,14 +348,14 @@ def _terminate_reclaimed_worker(
except OSError:
return info
- if _poll_worker_exit(pid):
+ if _poll_worker_exit(pid, started_at):
info["terminated"] = True
return info
- if _kb._pid_alive(pid):
+ if _worker_alive(pid, started_at):
if not _sigkill(kill, pid):
return info
info["sigkill"] = True
- info["terminated"] = not _kb._pid_alive(pid)
+ info["terminated"] = not _worker_alive(pid, started_at)
return info
@@ -427,7 +458,7 @@ def enforce_max_runtime(conn: sqlite3.Connection, *, signal_fn=None) -> list[str
host_prefix = _kb._host_prefix()
rows = conn.execute(
- "SELECT t.id, t.worker_pid, "
+ "SELECT t.id, t.worker_pid, t.worker_started_at, "
" COALESCE(r.started_at, t.started_at) AS active_started_at, "
" t.max_runtime_seconds, t.claim_lock "
"FROM tasks t "
@@ -449,16 +480,18 @@ def enforce_max_runtime(conn: sqlite3.Connection, *, signal_fn=None) -> list[str
pid = int(row["worker_pid"])
tid = row["id"]
+ started_at = _kb._row_get(row, "worker_started_at")
# SIGTERM then SIGKILL after 5 s grace; workers wanting a cleaner
- # shutdown install their own SIGTERM handler.
+ # shutdown install their own SIGTERM handler. A recycled PID (fingerprint
+ # mismatch) is never signalled: the worker is already gone.
killed = False
kill = _kill_fn(signal_fn)
- if kill is not None:
+ if kill is not None and not (_kb._pid_alive(pid) and _pid_recycled(pid, started_at)):
with contextlib.suppress(ProcessLookupError, OSError):
kill(pid, signal.SIGTERM)
# Short polling wait — no time.sleep on the write txn.
- _poll_worker_exit(pid)
- if _kb._pid_alive(pid):
+ _poll_worker_exit(pid, started_at)
+ if _worker_alive(pid, started_at):
killed = _sigkill(kill, pid)
error = f"elapsed {int(elapsed)}s > limit {limit}s"
@@ -529,7 +562,7 @@ def detect_stale_running(
reclaimed: list[str] = []
rows = conn.execute(
- "SELECT t.id, t.worker_pid, t.last_heartbeat_at, t.claim_lock, "
+ "SELECT t.id, t.worker_pid, t.worker_started_at, t.last_heartbeat_at, t.claim_lock, "
" COALESCE(r.started_at, t.started_at) AS active_started_at "
"FROM tasks t "
"LEFT JOIN task_runs r ON r.id = t.current_run_id "
@@ -552,7 +585,8 @@ def detect_stale_running(
tid = row["id"]
lock = row["claim_lock"] or ""
- termination = _kb._terminate_reclaimed_worker(pid, lock, signal_fn=signal_fn)
+ termination = _kb._terminate_reclaimed_worker(
+ pid, lock, signal_fn=signal_fn, started_at=_kb._row_get(row, "worker_started_at"))
# Never release a claim while our own worker is still alive: that would
# spawn a duplicate beside it. Hold the claim and retry next tick.
@@ -615,14 +649,14 @@ def reconcile_orphaned_running(conn: sqlite3.Connection) -> list[str]:
now = int(time.time())
reconciled: list[str] = []
rows = conn.execute(
- "SELECT id, claim_lock, claim_expires, worker_pid FROM tasks "
+ "SELECT id, claim_lock, claim_expires, worker_pid, worker_started_at FROM tasks "
"WHERE status = 'running' "
" AND (claim_lock IS NULL OR claim_expires IS NULL)"
).fetchall()
for row in rows:
tid = row["id"]
pid = row["worker_pid"]
- if pid and _kb._pid_alive(pid):
+ if pid and _worker_alive(pid, _kb._row_get(row, "worker_started_at")):
# Never requeue beside a live process. Retry next tick.
_kb._log.debug(
"kanban reconcile: task %s has broken claim bookkeeping but "
@@ -807,7 +841,7 @@ def _reclaim_dead_workers(conn: sqlite3.Connection) -> _CrashSweep:
sweep = _CrashSweep()
with _kb.write_txn(conn):
rows = conn.execute(
- "SELECT id, worker_pid, claim_lock, started_at, assignee "
+ "SELECT id, worker_pid, worker_started_at, claim_lock, started_at, assignee "
"FROM tasks "
"WHERE status = 'running' AND worker_pid IS NOT NULL"
).fetchall()
@@ -821,7 +855,7 @@ def _reclaim_dead_workers(conn: sqlite3.Connection) -> _CrashSweep:
started_at = _kb._row_get(row, "started_at")
if started_at is not None and time.time() - started_at < _kb._resolve_crash_grace_seconds():
continue
- if _kb._pid_alive(row["worker_pid"]):
+ if _worker_alive(row["worker_pid"], _kb._row_get(row, "worker_started_at")):
continue
pid = int(row["worker_pid"])
@@ -1098,13 +1132,18 @@ def _record_task_failure(
def _set_worker_pid(conn: sqlite3.Connection, task_id: str, pid: int) -> None:
- """Record the spawned child's pid + emit a ``spawned`` event carrying it."""
+ """Record the spawned child's pid + its start-time fingerprint, and emit a ``spawned`` event
+ carrying them. The fingerprint is what lets every later liveness/kill decision tell OUR worker
+ from a process that recycled the PID after a reboot."""
+ from gateway.status import get_process_start_time
+ started_at = get_process_start_time(int(pid))
with _kb.write_txn(conn):
- conn.execute("UPDATE tasks SET worker_pid = ? WHERE id = ?", (int(pid), task_id))
+ conn.execute("UPDATE tasks SET worker_pid = ?, worker_started_at = ? WHERE id = ?",
+ (int(pid), started_at, task_id))
run_id = _kb._current_run_id(conn, task_id)
if run_id is not None:
conn.execute("UPDATE task_runs SET worker_pid = ? WHERE id = ?", (int(pid), run_id))
- _kb._append_event(conn, task_id, "spawned", {"pid": int(pid)}, run_id=run_id)
+ _kb._append_event(conn, task_id, "spawned", {"pid": int(pid), "started_at": started_at}, run_id=run_id)
def _clear_failure_counter(conn: sqlite3.Connection, task_id: str) -> None:
diff --git a/plugins/kanban/dashboard/plugin_api.py b/plugins/kanban/dashboard/plugin_api.py
index b15ed8af72e..ef6b3c45211 100644
--- a/plugins/kanban/dashboard/plugin_api.py
+++ b/plugins/kanban/dashboard/plugin_api.py
@@ -691,11 +691,12 @@ def _set_status_direct(conn: sqlite3.Connection, task_id: str, new_status: str)
"""Direct status write for drag-drop moves without a structured verb (todo<->ready,
running<->ready) + a ``status`` event. Leaving ``running`` closes the run as 'reclaimed'
so attempt history isn't orphaned; the worker is killed only AFTER the txn commits."""
- terminations: list[tuple[Optional[int], Optional[str]]] = []
+ terminations: list[tuple[Optional[int], Optional[str], Optional[int]]] = []
effective_status = new_status
with kanban_db.write_txn(conn):
prev = conn.execute(
- "SELECT status, current_run_id, worker_pid, claim_lock FROM tasks WHERE id = ?", (task_id,)).fetchone()
+ "SELECT status, current_run_id, worker_pid, claim_lock, worker_started_at FROM tasks WHERE id = ?",
+ (task_id,)).fetchone()
if prev is None:
return False
if prev["status"] == "running" and new_status == "ready":
@@ -722,7 +723,7 @@ def _set_status_direct(conn: sqlite3.Connection, task_id: str, new_status: str)
run_id = kanban_db._end_run(
conn, task_id, outcome="reclaimed", status="reclaimed",
summary=f"status changed to {effective_status} (dashboard/direct)")
- terminations.append((prev["worker_pid"], prev["claim_lock"]))
+ terminations.append((prev["worker_pid"], prev["claim_lock"], prev["worker_started_at"]))
conn.execute(
"INSERT INTO task_events (task_id, run_id, kind, payload, created_at) VALUES (?, ?, 'status', ?, ?)",
(task_id, run_id, json.dumps({"status": effective_status, "requested_status": new_status}), int(time.time())))
@@ -731,8 +732,8 @@ def _set_status_direct(conn: sqlite3.Connection, task_id: str, new_status: str)
# back worker terminations to perform post-commit.
result = kanban_db.invalidate_descendants_for_parent_reopen(conn, task_id, author="dashboard")
terminations.extend(result["terminations"])
- for pid, claim_lock in terminations:
- kanban_db._terminate_reclaimed_worker(pid, claim_lock)
+ for pid, claim_lock, started_at in terminations:
+ kanban_db._terminate_reclaimed_worker(pid, claim_lock, started_at=started_at)
# Re-opening something may have made children stale.
if effective_status in {"done", "ready", "review"}:
kanban_db.recompute_ready(conn)
diff --git a/tests/hermes_cli/test_kanban_parent_reopen_invalidation.py b/tests/hermes_cli/test_kanban_parent_reopen_invalidation.py
index b040b66f006..35a4fe4ae13 100644
--- a/tests/hermes_cli/test_kanban_parent_reopen_invalidation.py
+++ b/tests/hermes_cli/test_kanban_parent_reopen_invalidation.py
@@ -105,7 +105,7 @@ def test_running_descendant_event_precedes_termination_via_reclaim_helper(
kills: list[tuple] = []
- def fake_terminate(pid, claim_lock, **kwargs):
+ def fake_terminate(pid, claim_lock, started_at=None, **kwargs):
# The audit trail must already be durable when the kill fires:
# standalone calls commit before terminating.
side = kbc.connect(tmp_path / "kanban.db")
@@ -114,7 +114,7 @@ def test_running_descendant_event_precedes_termination_via_reclaim_helper(
finally:
side.close()
assert "descendant_invalidated" in kinds
- kills.append((pid, claim_lock))
+ kills.append((pid, claim_lock, started_at))
return {"terminated": True}
monkeypatch.setattr(kb, "_terminate_reclaimed_worker", fake_terminate)
diff --git a/tests/hermes_cli/test_kanban_worker_pid_fingerprint.py b/tests/hermes_cli/test_kanban_worker_pid_fingerprint.py
new file mode 100644
index 00000000000..a7fc571987b
--- /dev/null
+++ b/tests/hermes_cli/test_kanban_worker_pid_fingerprint.py
@@ -0,0 +1,80 @@
+"""A recycled worker PID is never mistaken for our worker.
+
+``tasks.worker_pid`` survives a reboot; the number can then belong to an unrelated process. Every
+liveness decision (extend/defer the claim) and every kill (SIGTERM/SIGKILL on timeout or reclaim)
+must require the spawn-time start fingerprint to match, never bare PID existence.
+"""
+
+import os
+import signal
+import time
+
+import pytest
+
+from hermes_cli import kanban_db as kb
+from hermes_cli import kanban_db_connect as kbc
+from hermes_cli import kanban_db_dispatch as kbd
+
+
+def board(tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setenv("HERMES_KANBAN_CRASH_GRACE_SECONDS", "0")
+ conn = kbc.connect(tmp_path / "kanban.db")
+ try:
+ yield conn
+ finally:
+ conn.close()
+
+
+def _claimed_running(conn, *, pid: int, started_at, max_runtime=None) -> str:
+ tid = kb.create_task(conn, title="job", assignee="worker", max_runtime_seconds=max_runtime)
+ kb.claim_task(conn, tid)
+ kbd._set_worker_pid(conn, tid, pid)
+ old = int(time.time()) - 3600
+ with kb.write_txn(conn):
+ conn.execute("UPDATE tasks SET worker_started_at = ?, started_at = ?, claim_expires = ? WHERE id = ?",
+ (started_at, old, old, tid))
+ conn.execute("UPDATE task_runs SET started_at = ? WHERE id = (SELECT current_run_id FROM tasks WHERE id = ?)",
+ (old, tid))
+ return tid
+
+
+def test_recycled_pid_is_reclaimed_without_being_signalled(board):
+ """Our own live PID with a foreign fingerprint models a post-reboot recycle: the claim is released
+ (dead worker), no signal is sent, and max-runtime enforcement does not SIGTERM the stranger either."""
+ conn = board
+ killed = []
+ stranger_fingerprint = 1 # no live process started at tick 1
+ tid = _claimed_running(conn, pid=os.getpid(), started_at=stranger_fingerprint, max_runtime=1)
+
+ assert kbd._worker_alive(os.getpid(), stranger_fingerprint) is False
+ assert tid in kbd.enforce_max_runtime(conn, signal_fn=lambda pid, sig: killed.append((pid, sig)))
+ assert killed == []
+ task = kb.get_task(conn, tid)
+ assert task.status == "ready" and task.worker_pid is None
+
+ tid2 = _claimed_running(conn, pid=os.getpid(), started_at=stranger_fingerprint)
+ assert kb.release_stale_claims(conn, signal_fn=lambda pid, sig: killed.append((pid, sig))) == 1
+ assert killed == []
+ assert kb.get_task(conn, tid2).status == "ready"
+
+
+def test_matching_fingerprint_keeps_the_live_worker(board):
+ """The same PID with ITS OWN fingerprint (recorded at spawn) is our worker: the expired claim is
+ extended rather than reclaimed, and the timeout path signals it."""
+ from gateway.status import get_process_start_time
+
+ conn = board
+ killed = []
+ tid = _claimed_running(conn, pid=os.getpid(), started_at=get_process_start_time(os.getpid()))
+ assert kbd._worker_alive(os.getpid(), get_process_start_time(os.getpid())) is True
+ assert kb.release_stale_claims(conn) == 0
+ assert kb.get_task(conn, tid).status == "running"
+ kinds = [e.kind for e in kb.list_events(conn, tid)]
+ assert "claim_extended" in kinds
+
+ with kb.write_txn(conn):
+ conn.execute("UPDATE tasks SET max_runtime_seconds = 1 WHERE id = ?", (tid,))
+ kbd.enforce_max_runtime(conn, signal_fn=lambda pid, sig: killed.append((pid, sig)))
+ assert killed and killed[0] == (os.getpid(), signal.SIGTERM)