fix: retry transient MCP ownership failures
This commit is contained in:
+11
-11
@@ -491,9 +491,10 @@ def _acquire_mcp_writer_lock() -> tuple[bool, str]:
|
||||
the original holder exits — the OS releases its flock on process death —
|
||||
the next mutating call transparently promotes this server to writer, with
|
||||
no restart. The flock is arbitrated by the kernel (LOCK_NB), so two servers
|
||||
can never both win the retry. ``_MCP_WRITER_READ_ONLY`` is now only a
|
||||
status flag; it no longer short-circuits the retry (that sticky latch used
|
||||
to strand a server read-only for life even after the peer was long gone).
|
||||
can never both win the retry. ``_MCP_WRITER_READ_ONLY`` and
|
||||
``_MCP_WRITER_LOCK_FAILED`` are now only status flags for the last attempt;
|
||||
neither short-circuits a later retry. Peer ownership and transient setup
|
||||
failures can both be corrected without restarting the MCP host.
|
||||
"""
|
||||
|
||||
global _MCP_WRITER_LOCK_CM, _MCP_WRITER_READ_ONLY, _MCP_WRITER_LOCK_FAILED
|
||||
@@ -502,12 +503,10 @@ def _acquire_mcp_writer_lock() -> tuple[bool, str]:
|
||||
if _MCP_WRITER_LOCK_CM is not None:
|
||||
return True, ""
|
||||
|
||||
# NB: deliberately NO sticky read-only short-circuit here. If a peer held
|
||||
# the lease at startup we fall through and retry mine_palace_lock below, so
|
||||
# the server self-heals into the writer the moment the peer exits. A broken
|
||||
# lock *mechanism* (below) is still cached, since retrying it can't help.
|
||||
if _MCP_WRITER_LOCK_FAILED:
|
||||
return False, _MCP_WRITER_LOCK_ERROR
|
||||
# Deliberately no sticky failure short-circuit here. A peer can exit, a
|
||||
# backend mismatch can be corrected, and lock-directory permissions can be
|
||||
# repaired while this long-lived stdio host remains alive. Each mutating
|
||||
# request therefore gets a fresh ownership attempt.
|
||||
|
||||
try:
|
||||
from .palace import (
|
||||
@@ -540,8 +539,9 @@ def _acquire_mcp_writer_lock() -> tuple[bool, str]:
|
||||
_MCP_WRITER_LOCK_FAILED = True
|
||||
_MCP_WRITER_LOCK_ERROR = (
|
||||
"could not acquire MCP peer-writer lock for "
|
||||
f"{_config.palace_path!r}: {exc!r}; refusing mutating tools "
|
||||
"because peer-writer protection could not be established"
|
||||
f"{_config.palace_path!r}: {exc!r}; refusing this mutating tool "
|
||||
"because peer-writer protection could not be established; a later "
|
||||
"mutating request will retry ownership"
|
||||
)
|
||||
logger.error(_MCP_WRITER_LOCK_ERROR)
|
||||
return False, _MCP_WRITER_LOCK_ERROR
|
||||
|
||||
@@ -5198,17 +5198,27 @@ def test_status_tool_does_not_acquire_peer_writer_lock(monkeypatch):
|
||||
assert mcp_server.tool_status()["total_drawers"] == 0
|
||||
|
||||
|
||||
def test_peer_writer_lock_setup_failure_is_cached(monkeypatch):
|
||||
def test_peer_writer_lock_setup_failure_retries_and_recovers(monkeypatch):
|
||||
from mempalace import mcp_server, palace
|
||||
|
||||
class _DummyLock:
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc):
|
||||
return False
|
||||
|
||||
calls = {"count": 0}
|
||||
|
||||
def broken_mine_palace_lock(palace_path):
|
||||
def flaky_mine_palace_lock(palace_path):
|
||||
calls["count"] += 1
|
||||
raise RuntimeError(f"permission denied for {palace_path}")
|
||||
if calls["count"] == 1:
|
||||
raise RuntimeError(f"permission denied for {palace_path}")
|
||||
return _DummyLock()
|
||||
|
||||
monkeypatch.delenv(mcp_server._MCP_ALLOW_PEER_WRITER_ENV, raising=False)
|
||||
monkeypatch.setattr(palace, "mine_palace_lock", broken_mine_palace_lock)
|
||||
monkeypatch.setattr(palace, "mine_palace_lock", flaky_mine_palace_lock)
|
||||
monkeypatch.setattr(mcp_server, "_discard_mcp_storage_handles", lambda: None)
|
||||
|
||||
monkeypatch.setattr(mcp_server, "_MCP_WRITER_LOCK_CM", None)
|
||||
monkeypatch.setattr(mcp_server, "_MCP_WRITER_READ_ONLY", False)
|
||||
@@ -5219,11 +5229,13 @@ def test_peer_writer_lock_setup_failure_is_cached(monkeypatch):
|
||||
ok_second, reason_second = mcp_server._acquire_mcp_writer_lock()
|
||||
|
||||
assert ok_first is False
|
||||
assert ok_second is False
|
||||
assert calls["count"] == 1
|
||||
assert mcp_server._MCP_WRITER_LOCK_FAILED is True
|
||||
assert "refusing mutating tools" in reason_first
|
||||
assert reason_second == reason_first
|
||||
assert "later mutating request will retry ownership" in reason_first
|
||||
assert ok_second is True
|
||||
assert reason_second == ""
|
||||
assert calls["count"] == 2
|
||||
assert mcp_server._MCP_WRITER_LOCK_FAILED is False
|
||||
assert mcp_server._MCP_WRITER_LOCK_CM is not None
|
||||
mcp_server._release_mcp_writer_lock()
|
||||
|
||||
|
||||
def test_peer_writer_override_cannot_bypass_local_backend_lock(monkeypatch):
|
||||
|
||||
Reference in New Issue
Block a user