Skip to content

The agent that grows with you

fix(mcp-oauth): only treat contention errnos as "a peer holds the fence"

kshitijk4poorf07ea70de702
diff --git a/tests/tools/test_mcp_oauth_manager.py b/tests/tools/test_mcp_oauth_manager.py
index ba3d6fc2614..5d9599cb21a 100644
--- a/tests/tools/test_mcp_oauth_manager.py
+++ b/tests/tools/test_mcp_oauth_manager.py
@@ -902,3 +902,26 @@ async def test_refresh_restarts_flow_when_disk_pair_is_from_another_issuer(tmp_p
assert not provider.context.current_tokens.refresh_token, "foreign refresh token must be stripped"
assert (await storage.get_tokens()).refresh_token is None, "strip must reach disk"
assert provider._hermes_fence is None
+
+
+async def test_refresh_fence_surfaces_non_contention_lock_errors_immediately(tmp_path, monkeypatch):
+ """A lock syscall failing for a reason other than contention must not spin to the deadline."""
+ import errno
+
+ import tools.mcp_oauth as mcp_oauth
+
+ if mcp_oauth.fcntl is None:
+ pytest.skip("flock-based fence only")
+
+ def broken_flock(fd, op):
+ if op & mcp_oauth.fcntl.LOCK_UN:
+ return None
+ raise OSError(errno.ENOLCK, "No locks available")
+
+ monkeypatch.setattr(mcp_oauth.fcntl, "flock", broken_flock)
+ started = time.monotonic()
+ with pytest.raises(mcp_oauth.RefreshFenceTimeout, match="unavailable on this filesystem"):
+ async with mcp_oauth._refresh_fence(tmp_path / "srv.json", timeout=5.0):
+ pass
+ assert time.monotonic() - started < 1.0, "must fail fast, not wait out the deadline"
diff --git a/tools/mcp_oauth.py b/tools/mcp_oauth.py
index 7875971788c..c08888e8f7c 100644
--- a/tools/mcp_oauth.py
+++ b/tools/mcp_oauth.py
@@ -11,6 +11,7 @@ redirect_host, client_name, client_metadata_url, cimd, user_agent, timeout."""
import asyncio
import contextlib
import contextvars
+import errno
import html
import importlib.util as _importlib_util
import json
@@ -67,6 +68,11 @@ class RefreshFenceTimeout(RuntimeError):
"""
+# POSIX flock: EWOULDBLOCK/EAGAIN, EACCES on some NFS; msvcrt.locking: EACCES/EDEADLK.
+# Same set as cron.scheduler._is_lock_contention_errno (not imported: that module is heavy).
+_FENCE_CONTENTION_ERRNOS = frozenset({errno.EWOULDBLOCK, errno.EAGAIN, errno.EACCES, errno.EDEADLK})
+
+
@contextlib.asynccontextmanager
async def _refresh_fence(path: "Path", *, timeout: float = _REFRESH_FENCE_TIMEOUT_SECONDS):
"""Own one refresh generation across read -> POST -> persist.
@@ -124,7 +130,14 @@ async def _refresh_fence(path: "Path", *, timeout: float = _REFRESH_FENCE_TIMEOU
)
acquired = True
break
- except (OSError, IOError):
+ except OSError as exc:
+ if exc.errno not in _FENCE_CONTENTION_ERRNOS:
+ # Not "a peer holds it" but "this filesystem cannot lock"
+ # (e.g. some network mounts). Still fail closed, but say so
+ # now instead of spinning to the deadline and blaming a peer.
+ raise RefreshFenceTimeout(
+ f"refresh fence unavailable on this filesystem: {exc}"
+ ) from exc
if time.monotonic() >= deadline:
raise RefreshFenceTimeout(
f"refresh fence held by a peer for {timeout:.0f}s ({lock_path.name})"