Skip to content

The agent that grows with you

fix(cron): treat a drifted start-time fingerprint as a live owner in recovery

liuhao102452abb64c8f17
diff --git a/cron/executions.py b/cron/executions.py
index cc4c0c54ff3..75ec666249d 100644
--- a/cron/executions.py
+++ b/cron/executions.py
@@ -1,8 +1,8 @@
"""Profile-local durable audit ledger for cron execution attempts.
The ledger records what is known about each attempt; it is not a retry queue. Interrupted attempts
-become ``unknown`` only after their exact owner process is proved gone. Terminal states are
-immutable.
+become ``unknown`` only after their owner process is proved gone — a start-time reading that fails
+to match the claim-time fingerprint is not proof of death. Terminal states are immutable.
"""
from __future__ import annotations
@@ -30,6 +30,12 @@ MAX_TERMINAL_EXECUTIONS = 1000
HANDOFF_ADOPTION_GRACE_SECONDS = 30.0
# Floor for the live-owner stale-claim bound (#115692); see _live_owner_stale_after_seconds.
LIVE_OWNER_STALE_CLAIM_FLOOR_SECONDS = 7200.0
+# Same-host start-time readings can drift by ~1 s between the claim-time and recovery-time reads
+# (macOS ``kern.boottime`` adjustment, #117505). Both fingerprint scales are ×100 (Linux /proc
+# ticks, psutil centiseconds), so 200 means 2 s on either platform — a recycled PID is essentially
+# never that close to the original's start time, and a live misread is still bounded by the
+# stale-claim sweep above.
+_OWNER_START_TIME_DRIFT_TOLERANCE = 200
_TERMINAL_STATES = ("completed", "failed", "unknown")
_lock = threading.RLock()
_PROCESS_ID = uuid.uuid4().hex
@@ -136,7 +142,9 @@ def _owner_is_live(pid: int, started_at: Optional[int]) -> bool:
if started_at is None:
return pid == os.getpid()
current = _process_start_time(pid)
- return current is not None and current == started_at
+ if current is None:
+ return True # cannot compare -> cannot prove death; a misread must not rewrite state
+ return abs(current - started_at) <= _OWNER_START_TIME_DRIFT_TOLERANCE
def _live_owner_stale_after_seconds() -> Optional[float]:
diff --git a/tests/cron/test_owner_start_time_drift.py b/tests/cron/test_owner_start_time_drift.py
new file mode 100644
index 00000000000..d8bc14f8807
--- /dev/null
+++ b/tests/cron/test_owner_start_time_drift.py
@@ -0,0 +1,74 @@
+"""Start-time fingerprint drift between claim and recovery reads (#117505).
+
+``_owner_is_live()`` used to require the recomputed start-time fingerprint to be exactly
+equal to the one recorded at claim time. On hosts where same-host readings drift by ~1 s
+(macOS ``kern.boottime`` adjustment), every long-running execution was reconciled to
+``unknown`` while its process was still alive and healthy. A mismatch is now treated as
+inconclusive within a small tolerance — a recycled PID is still reaped, an unreadable
+reading is still fail-safe.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+import cron.executions as executions_mod
+
+
[email protected](autouse=True)
+def _ledger(monkeypatch, tmp_path):
+ import gateway.status
+
+ monkeypatch.setattr(executions_mod, "EXECUTIONS_FILE", tmp_path / "cron" / "executions.db")
+ # The owner process exists for every row in this module; only the fingerprint decides.
+ monkeypatch.setattr(gateway.status, "_pid_exists", lambda pid: True)
+
+
+def _seed_external_running(monkeypatch, *, claimed_start_time: int) -> str:
+ """Running row owned by a foreign process, with the given claim-time fingerprint."""
+ with monkeypatch.context() as m:
+ m.setattr(executions_mod, "_PROCESS_ID", "ext-proc")
+ m.setattr(executions_mod, "_process_start_time", lambda pid: claimed_start_time)
+ record = executions_mod.create_execution("job-drift", source="cron")
+ executions_mod.mark_execution_running(record["id"])
+ return record["id"]
+
+
+def _status(execution_id: str) -> str:
+ return executions_mod.latest_execution("job-drift")["status"]
+
+
+ "drift,expect_live",
+ [
+ (0, True), # exact match: the historical guarantee
+ (100, True), # 1 s drift: the reported macOS boot-time step (#117505)
+ (200, True), # tolerance boundary
+ (201, False), # just outside tolerance: presumed recycled PID
+ (3600, False), # PID-reuse scale
+ ],
+)
+def test_drift_within_tolerance_keeps_execution_running(monkeypatch, drift, expect_live):
+ """Recovery only reaps a claimed/running row when the recomputed fingerprint is far from
+ the claim-time one, not merely unequal to it."""
+ claimed = 178864182760 # the exact reading from the report
+ execution_id = _seed_external_running(monkeypatch, claimed_start_time=claimed)
+ monkeypatch.setattr(executions_mod, "_process_start_time", lambda pid: claimed + drift)
+
+ recovered = executions_mod.recover_interrupted_executions()
+
+ if expect_live:
+ assert recovered == 0
+ assert _status(execution_id) == "running"
+ else:
+ assert recovered == 1
+ assert _status(execution_id) == "unknown"
+
+
+def test_unreadable_start_time_fail_safe(monkeypatch):
+ """A start time that cannot be read now proves nothing about death: the row must survive."""
+ execution_id = _seed_external_running(monkeypatch, claimed_start_time=178864182760)
+ monkeypatch.setattr(executions_mod, "_process_start_time", lambda pid: None)
+
+ assert executions_mod.recover_interrupted_executions() == 0
+ assert _status(execution_id) == "running"