Skip to content

The agent that grows with you

fix(gateway): an unusable token-lock dir keeps the adapter retryable, so a token-only gateway stays up

Tekniume541f21ed811
diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py
index 8fb0c9aaed4..e185a78e799 100644
--- a/gateway/platforms/base.py
+++ b/gateway/platforms/base.py
@@ -2198,16 +2198,18 @@ class BasePlatformAdapter(ABC):
try:
acquired, existing = acquire_scoped_lock(scope, identity, metadata=lock_meta)
except ScopedLockDirUnusable as exc:
- # An ACL no retry can change (an elevated install created the lock dir, #134889 W2):
- # stay down with the repair named instead of a reconnect loop that fails identically.
- # The status module already logged the WARNING (once per dir).
+ # An ACL an elevated install left on the lock dir (#134889 W2): name the repair. Stay
+ # RETRYABLE: the dir is shared by every token-locked adapter, so a non-retryable park
+ # on a gateway that runs only those exits 78 and takes cron and the Desktop attach
+ # endpoint down with it. The status module logs the WARNING once per dir (DEBUG after),
+ # so the watcher's backoff retries quietly and picks the repair up without a restart.
self._platform_lock_identity = None
self._set_fatal_error(
"lock_dir_unusable",
f"{resource_desc} lock cannot be taken: the gateway lock directory {exc.filename} is not "
f"usable by this user (likely created by an elevated Hermes). To repair, "
- f"{unusable_dir_remedy(exc.filename)}, then restart the gateway.",
- retryable=False)
+ f"{unusable_dir_remedy(exc.filename)}; the gateway retries on its own.",
+ retryable=True)
return False
if acquired:
return True
diff --git a/tests/gateway/test_scoped_lock_dir_unusable.py b/tests/gateway/test_scoped_lock_dir_unusable.py
index 222f7cb5b96..247f86e91b5 100644
--- a/tests/gateway/test_scoped_lock_dir_unusable.py
+++ b/tests/gateway/test_scoped_lock_dir_unusable.py
@@ -5,6 +5,7 @@ leading cause is a ``gateway-locks`` dir created by an elevated install. The POS
here is real, not patched: a lock dir without write permission for this uid. The Windows ACL shape
itself is proved by ``test_scoped_lock_dir_windows_acl.py`` on Windows CI.
"""
+import asyncio
import logging
import os
import stat
@@ -16,6 +17,7 @@ import pytest
from gateway import status
from gateway.platforms.base import BasePlatformAdapter
from gateway.restart import is_global_startup_conflict
+from gateway.run_startup import GatewayStartupMixin
pytestmark = pytest.mark.skipif(os.name == "nt" or os.geteuid() == 0, # windows-footgun: ok — skipped on nt
reason="POSIX mode bits; root ignores them")
@@ -68,7 +70,9 @@ def test_unusable_lock_dir_parks_the_adapter_with_the_repair(locked_out_dir, cap
assert _adapter()._acquire_platform_lock("telegram-bot-token", "tok", "Telegram bot token") is False
assert adapter._fatal_error_code == "lock_dir_unusable"
- assert adapter._fatal_error_retryable is False
+ # Retryable: the shared lock dir parks EVERY token-locked adapter at once, and a non-retryable
+ # park on a Telegram-only gateway exits 78 (taking cron and the Desktop attach endpoint down).
+ assert adapter._fatal_error_retryable is True
assert not is_global_startup_conflict(adapter._fatal_error_code)
assert str(locked_out_dir) in adapter._fatal_error_message
assert "chown" in adapter._fatal_error_message
@@ -101,3 +105,46 @@ def test_unreadable_stale_token_lock_is_named_not_reported_as_a_holderless_confl
finally:
lock_dir.chmod(stat.S_IRWXU)
stale.chmod(stat.S_IRUSR | stat.S_IWUSR)
+
+
+class _StartupProbe:
+ """Only the state the two real startup decisions read; their bodies run unchanged."""
+
+ _startup_queue_transient_failure = GatewayStartupMixin._startup_queue_transient_failure
+ _start_aggregate_connect_results = GatewayStartupMixin._start_aggregate_connect_results
+ _start_handle_no_connections = GatewayStartupMixin._start_handle_no_connections
+ _startup_retry_entry = GatewayStartupMixin._startup_retry_entry
+
+ def __init__(self):
+ self._failed_platforms, self.status_updates, self.fatal = {}, [], []
+ self._startup_parked_platforms = False
+
+ async def _safe_adapter_disconnect(self, adapter, platform):
+ pass
+
+ def _update_platform_runtime_status(self, name, **kw):
+ self.status_updates.append((name, kw))
+
+ def _reconnect_queue_entry(self, platform, adapter, platform_config, **kw):
+ return {"adapter": adapter, **kw}
+
+ def _startup_fail_fatal_config(self, reason):
+ self.fatal.append(reason)
+
+
+def test_a_sole_token_locked_platform_on_an_unusable_lock_dir_keeps_the_gateway_up(locked_out_dir, monkeypatch):
+ """Review 2.1 on #140490: a Telegram-only gateway must not exit 78 over the lock dir."""
+ import gateway.run as run_mod
+ monkeypatch.setattr(run_mod, "_write_runtime_status_quiet", lambda **k: None, raising=False)
+ adapter = _adapter()
+ assert adapter._acquire_platform_lock("telegram-bot-token", "tok", "Telegram bot token") is False
+ platform = MagicMock(value="telegram")
+ runner = _StartupProbe()
+ retryable, nonretryable = [], []
+ connected = asyncio.run(runner._start_aggregate_connect_results(
+ [(platform, adapter, MagicMock(), "failed", None)], retryable, nonretryable))
+ must_exit = runner._start_handle_no_connections(connected, 1, retryable, nonretryable)
+
+ assert (connected, must_exit, nonretryable, runner.fatal) == (0, False, [], [])
+ assert platform in runner._failed_platforms # the reconnect watcher picks the repair up
+ assert retryable and "not usable" in retryable[0]
diff --git a/tests/test_hermes_dir_acl_windows.py b/tests/test_hermes_dir_acl_windows.py
index 765c2b8779e..5223150727c 100644
--- a/tests/test_hermes_dir_acl_windows.py
+++ b/tests/test_hermes_dir_acl_windows.py
@@ -234,7 +234,7 @@ def _w2(lock_dir):
if lvl == "WARNING" and "not usable" in m]}))
""", str(lock_dir), prelude=_DROP_PRIVILEGES)
for ok, code, retryable, message in result["codes"]:
- assert (ok, code, retryable) == (False, "lock_dir_unusable", False)
+ assert (ok, code, retryable) == (False, "lock_dir_unusable", True)
assert "icacls" in message and "/reset" in message and str(lock_dir) in message
assert len(result["warnings"]) == 1, result