Skip to content

The agent that grows with you

fix(gateway): anchor the legacy host-record check on boot time, not a live create_time

ethernet6c4fb96fc758
diff --git a/gateway/host_rendezvous.py b/gateway/host_rendezvous.py
index 0c491419ffc..29d19662e68 100644
--- a/gateway/host_rendezvous.py
+++ b/gateway/host_rendezvous.py
@@ -244,11 +244,22 @@ def _pid_incarnation_matches(pid: int, create_time: Optional[float]) -> Optional
return _pid_alive_matches(pid, create_time)
-# Wall-clock create_time drifts by seconds on WSL (#117505). A reboot moves it by minutes
-# and resets boot-relative start ticks, so a tick match within START_TIME_DRIFT_TOLERANCE
-# after reboot is a recycled early PID, not the owner. 120s is far outside that drift and
-# inside every reboot gap that has actually wedged a supervised gateway.
-_LEGACY_CREATE_TIME_REBOOT_GAP_S = 120.0
+# A record published before ``bootEpoch`` existed cannot name its boot, but its ``create_time``
+# still says when the owner started. An owner started before this boot began cannot be alive in
+# it, so a tick match against such a record is a recycled early PID. Anchoring on boot time (not
+# on a live ``create_time`` reading) keeps #117505 intact: a wall-clock resync moves the live
+# reading of a running owner, never the point in time the record says it started.
+_LEGACY_BOOT_SLACK_S = 120.0
+
+
+def _boot_time() -> Optional[float]:
+ try:
+ import psutil
+
+ return float(psutil.boot_time())
+ except Exception:
+ logger.debug("boot time unreadable; legacy record age check skipped", exc_info=True)
+ return None
def _current_boot_epoch() -> str:
@@ -268,15 +279,19 @@ def _record_incarnation_matches(record: HostRecord) -> Optional[bool]:
New records prefer the canonical start fingerprint: on Linux/WSL it comes from /proc start
ticks and is immune to wall-clock shifts that move psutil.create_time(). Those ticks are
NOT unique across reboots — they reset, and an early-boot PID reused within the drift
- tolerance looks like the dead owner. A published boot epoch, or (for legacy records with
- no epoch) a create_time gap past reboot scale, is a mismatch. Empty epoch is a no-op so
- macOS and pre-upgrade records are not false-staled. Do not infer identity from argv.
- Legacy records without a start fingerprint keep the create-time check.
+ tolerance looks like the dead owner. A published boot epoch that differs from this boot's, or
+ (for legacy records with no epoch) a create_time from before this boot began, is a mismatch.
+ Empty epoch is a no-op so macOS and pre-upgrade records are not false-staled. Do not infer
+ identity from argv. A record with neither create_time nor start_time proves nothing about
+ its pid, so it never vouches for one. Legacy records without a start fingerprint keep the
+ create-time check.
"""
recorded_epoch = getattr(record, "boot_epoch", "") or ""
epoch = _current_boot_epoch()
if recorded_epoch and epoch and recorded_epoch != epoch:
return False
+ if record.start_time is None and record.create_time is None:
+ return False
if record.start_time is None:
matched = _pid_incarnation_matches(record.pid, record.create_time)
else:
@@ -289,17 +304,9 @@ def _record_incarnation_matches(record: HostRecord) -> Optional[bool]:
matched = None if current is None else start_time_fingerprints_match(record.start_time, current)
if matched is not True:
return matched
- if (
- record.role == ROLE_GATEWAY
- and not recorded_epoch
- and record.start_time is not None
- and record.create_time is not None
- ):
- live_create = process_create_time(record.pid)
- if (
- live_create is not None
- and abs(float(live_create) - float(record.create_time)) > _LEGACY_CREATE_TIME_REBOOT_GAP_S
- ):
+ if record.role == ROLE_GATEWAY and not recorded_epoch and record.create_time is not None:
+ boot = _boot_time()
+ if boot is not None and float(record.create_time) < boot - _LEGACY_BOOT_SLACK_S:
return False
return True
diff --git a/tests/gateway/test_host_rendezvous.py b/tests/gateway/test_host_rendezvous.py
index 1f4a3e46d5d..fb1ab96f038 100644
--- a/tests/gateway/test_host_rendezvous.py
+++ b/tests/gateway/test_host_rendezvous.py
@@ -139,21 +139,24 @@ def test_live_host_record_survives_wall_clock_create_time_drift(host_dir, monkey
def test_reused_early_pid_after_reboot_is_not_the_gateway():
"""Boot-relative start ticks reset on reboot and collide within the 2s drift tolerance.
- A legacy record (no bootEpoch) with a reboot-scale createTime gap must not keep a
- supervised unit in the transient-refuse loop. The same tick match with a WSL-scale
- gap (#117505) stays live.
+ A legacy record (no bootEpoch) whose createTime predates this boot cannot describe a live
+ process. A record dated inside this boot stays live however far the live create_time
+ reading has since drifted from it (#117505), and a record with no incarnation proof at
+ all never vouches for the pid.
"""
+ import psutil
+
from gateway.status import get_process_start_time
child = subprocess.Popen(["sleep", "30"])
try:
start = get_process_start_time(child.pid)
- live_create = hr.process_create_time(child.pid)
- assert start is not None and live_create is not None
+ assert start is not None
+ boot = psutil.boot_time()
record = hr.HostRecord(
role=hr.ROLE_GATEWAY,
pid=child.pid,
- create_time=float(live_create) - 3600.0,
+ create_time=boot - 3600.0,
start_time=int(start) + 10,
host="",
port=None,
@@ -164,9 +167,12 @@ def test_reused_early_pid_after_reboot_is_not_the_gateway():
)
assert hr.liveness_is_proven(record) is False
assert hr.record_is_stale(record) is True
- drifted = dataclasses.replace(record, create_time=float(live_create) - 5.0)
+ drifted = dataclasses.replace(record, create_time=boot + 1.0)
assert hr.liveness_is_proven(drifted) is True
assert hr.record_is_stale(drifted) is False
+ unprovable = dataclasses.replace(record, create_time=None, start_time=None)
+ assert hr.liveness_is_proven(unprovable) is False
+ assert hr.record_is_stale(unprovable) is True
finally:
child.kill()
child.wait(timeout=10)