Skip to content

The agent that grows with you

fix(gateway): a read-only host-record check never creates the lock file

kshitijk4poor7b334fe3a97e
diff --git a/gateway/host_rendezvous.py b/gateway/host_rendezvous.py
index 66d3b5c58d4..7d27571cdc3 100644
--- a/gateway/host_rendezvous.py
+++ b/gateway/host_rendezvous.py
@@ -265,28 +265,22 @@ def _boot_time() -> Optional[float]:
def _host_lock_is_free(role: str) -> Optional[bool]:
"""True when no process holds the host lock for ``role``; ``None`` when it cannot be probed.
- The probe takes the lock for an instant, so a gateway claiming it in that instant gets
- HELD_BY_OTHER and its supervisor retries. Callers only reach this for a legacy record that
- is already suspect, never on the normal attach path.
+ Read-only callers (status, dashboard) reach this, so it never creates the lock dir or file: a
+ missing lock file means nobody holds the lock. The probe takes the lock for an instant, so a
+ gateway claiming it in that instant gets HELD_BY_OTHER and its supervisor retries.
"""
if owns_host_lock(role):
return False
- from gateway.status import _release_file_lock, _try_acquire_file_lock
+ from gateway.status import _probe_lock_file
try:
- ensure_host_state_dir()
- handle = open(lock_path(role), "a+", encoding="utf-8") # windows-footgun: ok — flock handle, never read
+ fd = os.open(str(lock_path(role)), os.O_RDWR)
+ except FileNotFoundError:
+ return True
except OSError:
logger.debug("host %s lock could not be probed", role, exc_info=True)
return None
- try:
- if not _try_acquire_file_lock(handle):
- return False
- _release_file_lock(handle)
- return True
- finally:
- with contextlib.suppress(OSError):
- handle.close()
+ return not _probe_lock_file(os.fdopen(fd, "r+", encoding="utf-8"))
def _current_boot_epoch() -> str:
@@ -306,14 +300,14 @@ 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 that differs from this boot's, or
- (for legacy records with no epoch) a create_time from before this boot began while nobody holds
- the host lock, 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
+ tolerance looks like the dead owner. A published boot epoch that differs from this boot's is a
+ mismatch. A gateway record with no epoch (published before ``bootEpoch`` existed, or on any
+ platform without /proc, where the epoch is always empty) whose create_time predates this boot
+ is a mismatch only while nobody holds the host lock. 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 ""
+ recorded_epoch = record.boot_epoch
epoch = _current_boot_epoch()
if recorded_epoch and epoch and recorded_epoch != epoch:
return False
@@ -333,7 +327,7 @@ def _record_incarnation_matches(record: HostRecord) -> Optional[bool]:
return matched
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:
+ if boot is not None and record.create_time < boot - _LEGACY_BOOT_SLACK_S:
lock_free = _host_lock_is_free(record.role)
if lock_free is None:
return None
diff --git a/tests/gateway/test_host_rendezvous.py b/tests/gateway/test_host_rendezvous.py
index f3e21742249..5c771b46701 100644
--- a/tests/gateway/test_host_rendezvous.py
+++ b/tests/gateway/test_host_rendezvous.py
@@ -140,9 +140,9 @@ def test_reused_early_pid_after_reboot_is_not_the_gateway(host_dir):
"""Boot-relative start ticks reset on reboot and collide within the 2s drift tolerance.
A legacy record (no bootEpoch) whose createTime predates this boot, with nobody holding the
- host lock, 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.
+ host lock, cannot describe a live process, and judging it creates no lock file. 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
@@ -167,6 +167,7 @@ def test_reused_early_pid_after_reboot_is_not_the_gateway(host_dir):
)
assert hr.liveness_is_proven(record) is False
assert hr.record_is_stale(record) is True
+ assert not hr.lock_path(hr.ROLE_GATEWAY).exists()
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