Skip to content

The agent that grows with you

fix(gateway): an unreadable stale gateway.lock is refused on Linux too, never unlinked

Teknium18ff1eaafa09
diff --git a/gateway/runtime_ownership.py b/gateway/runtime_ownership.py
index cebb3962124..d3bdd821b97 100644
--- a/gateway/runtime_ownership.py
+++ b/gateway/runtime_ownership.py
@@ -1,8 +1,9 @@
"""Nonblocking, all-or-nothing reservations of canonical profile homes.
A held lock inode is never removed: unlinking it creates a second owner. The one exception is a
-stale inode this user cannot use (left by a gateway that ran as root, #42685) that is provably
-unheld; it is replaced once, and every winner checks that the path still names the inode it locked.
+stale inode this user cannot write (left by a gateway that ran as root, #42685) that a flock through
+a read-only descriptor proves unheld; it is replaced once, and every winner checks that the path
+still names the inode it locked.
"""
from __future__ import annotations
@@ -56,23 +57,12 @@ def _open_checked(path: Path, flags: int) -> int:
return fd
-def _inode_locked_per_proc(ino: int) -> bool | None:
- """Linux ``/proc/locks``: whether any lock names inode *ino* (on any device, so a btrfs/overlay
- st_dev mismatch can only refuse, never miss a holder). None where the table is unavailable."""
- try:
- table = Path('/proc/locks').read_text(encoding='ascii', errors='replace')
- except OSError:
- return None
- suffix = f':{ino}'
- return any(field.endswith(suffix) and field.count(':') == 2
- for line in table.splitlines() for field in line.split())
-
-
def _unlink_stale_lock(path: Path) -> bool:
- """Unlink a ``gateway.lock`` this user cannot use (unopenable, or another uid's) when no process
- holds it: a flock probe through a read-only descriptor, else Linux ``/proc/locks``. Symlinks,
- hardlinks, non-regular files, a home whose ``gateway.pid`` names a live process and anything not
- provably unheld are left in place (False)."""
+ """Unlink a ``gateway.lock`` this user cannot write (another uid's) once a flock taken through a
+ read-only descriptor proves it unheld. Symlinks, hardlinks, non-regular files, a home whose
+ ``gateway.pid`` names a live process and anything not provably unheld are left in place (False).
+ An inode this user cannot even open is refused with the recovery command: an unheld snapshot
+ (``/proc/locks``) does not stop a holder locking it between the read and the unlink."""
from gateway.status import _live_pid_from_record, _read_pid_record
if os.name == 'nt':
return False # a sharing violation is a live holder there
@@ -84,10 +74,7 @@ def _unlink_stale_lock(path: Path) -> bool:
try:
probe = os.open(path, os.O_RDONLY | getattr(os, 'O_NOFOLLOW', 0) | os.O_NONBLOCK)
except PermissionError:
- held = _inode_locked_per_proc(info.st_ino)
- if held is None: # no /proc/locks (macOS/BSD): nothing can prove it unheld, so never guess
- raise PermissionError(foreign_lock_recovery(path)) from None
- return held is False and _unlink_if_same(path, info)
+ raise PermissionError(foreign_lock_recovery(path)) from None
import fcntl
try:
if not _same_inode(os.fstat(probe), info):
diff --git a/tests/gateway/test_foreign_stale_gateway_lock.py b/tests/gateway/test_foreign_stale_gateway_lock.py
index 621675ba94f..a0ec42ae4cb 100644
--- a/tests/gateway/test_foreign_stale_gateway_lock.py
+++ b/tests/gateway/test_foreign_stale_gateway_lock.py
@@ -1,5 +1,5 @@
-"""A gateway.lock this user cannot read and cannot prove unheld (a root run on macOS, which has no
-/proc/locks) is refused with a terminal reason and the exact recovery command, never guessed at."""
+"""A gateway.lock this user cannot read, so cannot flock to prove unheld (a root run), is refused
+with a terminal reason and the exact recovery command, never guessed at."""
import os
import pytest
@@ -17,10 +17,11 @@ def _unreadable_lock(tmp_path):
return home, lock
-def test_reserve_without_proc_locks_refuses_with_recovery_command(tmp_path, monkeypatch):
+def test_reserve_refuses_an_unreadable_lock_with_recovery_command(tmp_path):
+ """Linux too: an unheld /proc/locks snapshot cannot stop a holder (a root gateway) locking the
+ old inode between the read and the unlink, so nothing short of our own flock replaces it."""
from gateway import runtime_ownership
home, lock = _unreadable_lock(tmp_path)
- monkeypatch.setattr(runtime_ownership, '_inode_locked_per_proc', lambda ino: None) # macOS: no table
inode = lock.stat().st_ino
with pytest.raises(PermissionError) as raised:
runtime_ownership.ProfileOwnership().reserve([home])
diff --git a/tests/gateway/test_runtime_ownership.py b/tests/gateway/test_runtime_ownership.py
index 395bcd5330a..0642703390b 100644
--- a/tests/gateway/test_runtime_ownership.py
+++ b/tests/gateway/test_runtime_ownership.py
@@ -119,10 +119,11 @@ def test_profile_reservations_unwind_without_releasing_another_owner(tmp_path):
@pytest.mark.platforms("linux")
@pytest.mark.skipif(hasattr(os, 'geteuid') and os.geteuid() == 0, reason="root opens a mode-000 file")
[email protected]("mode", [0o000, 0o400])
[email protected]("mode", [0o400])
def test_stale_unopenable_gateway_lock_is_replaced_on_boot(tmp_path, monkeypatch, mode):
- """Main's #42685: a lock this user cannot open (left by a root-run gateway) and that no process
- holds is unlinked and recreated, so boot does not exit "already held" forever."""
+ """Main's #42685: a lock this user cannot write (left by a root-run gateway) that a read-only
+ flock proves unheld is unlinked and recreated, so boot does not exit "already held" forever.
+ An unreadable one is refused instead (test_foreign_stale_gateway_lock)."""
import json
from gateway.status import acquire_gateway_runtime_lock, release_gateway_runtime_lock
home = tmp_path / 'home'
diff --git a/tests/gateway/test_status.py b/tests/gateway/test_status.py
index de69238c999..542afd91d26 100644
--- a/tests/gateway/test_status.py
+++ b/tests/gateway/test_status.py
@@ -1523,8 +1523,8 @@ class TestPermissionErrorOnLockFile:
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
lock = tmp_path / "gateway.lock"
lock.write_text("existing owner")
- # The owner is live (its gateway.pid names a running process): an unopenable lock with no
- # provable holder is the stale #42685 case and is replaced (test_runtime_ownership).
+ # The owner is live (its gateway.pid names a running process). Without one, an unreadable
+ # lock is refused with the sudo recovery (test_foreign_stale_gateway_lock).
(tmp_path / "gateway.pid").write_text(json.dumps({"pid": os.getpid()}), encoding="utf-8")
inode = lock.stat().st_ino
lock.chmod(0)