The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: never evict pinned CIMD sockets from the callback reservation FIFO
Teknium5dd15872a687
diff --git a/tests/tools/test_mcp_cimd.py b/tests/tools/test_mcp_cimd.pyindex 5964894d2b7..5a638d75496 100644--- a/tests/tools/test_mcp_cimd.py+++ b/tests/tools/test_mcp_cimd.py@@ -213,6 +213,40 @@ def test_pinned_port_is_held_until_the_callback_adopts_it(thief.close()+def test_pinned_socket_survives_the_ephemeral_eviction_cap(+ tmp_path, monkeypatch, private_ports+):+ """The reservation FIFO cap must never close a parked pinned socket.++ Ephemeral reservations churn through ``_reserve_callback_port`` on every+ reconnect loop, and past the cap the oldest gets evicted. A pinned CIMD+ socket parked in the same dict would be the oldest under heavy+ concurrency — closing it converts the pinned flow back into a stealable+ window, the exact race the pin exists to prevent (#22161).+ """+ import tools.mcp_oauth as mod++ monkeypatch.setenv("HERMES_HOME", str(tmp_path))++ result = _maybe_use_cimd({}, HermesTokenStorage("srv"))+ assert result is not None+ pinned = result[1]++ # Churn well past the cap; the pinned socket must stay parked and bound.+ ephemeral = [mod._reserve_callback_port() for _ in range(mod._MAX_RESERVED_SOCKETS + 4)]+ try:+ assert pinned in mod._reserved_sockets+ thief = socket.socket(socket.AF_INET, socket.SOCK_STREAM)+ with pytest.raises(OSError):+ thief.bind(("127.0.0.1", pinned))+ thief.close()+ finally:+ for port in ephemeral:+ sock = mod._reserved_sockets.pop(port, None)+ if sock is not None:+ sock.close()++def test_concurrent_servers_get_different_pinned_ports(tmp_path, monkeypatch, private_ports):diff --git a/tools/mcp_oauth.py b/tools/mcp_oauth.pyindex 2ff3b9f5769..a4c3d91a079 100644--- a/tools/mcp_oauth.py+++ b/tools/mcp_oauth.py@@ -224,11 +224,25 @@ _MAX_RESERVED_SOCKETS = 8def _park_reserved_socket(port: int, sock: socket.socket) -> None:- """Hold *sock* bound to *port* until ``_wait_for_callback`` adopts it."""- # Evict oldest reservations past the cap (dict preserves insertion order).+ """Hold *sock* bound to *port* until ``_wait_for_callback`` adopts it.++ Pinned CIMD sockets are never evicted: the published metadata document+ only declares the pinned ports, so losing one mid-flow silently converts+ a pinned reservation back into a stealable window — the exact race the+ parking exists to prevent (#22161). The FIFO cap applies to ephemeral+ reservations only; the pinned range is already bounded by ``_CIMD_PORTS``.+ """+ # Evict oldest ephemeral reservations past the cap (dict preserves+ # insertion order).while len(_reserved_sockets) >= _MAX_RESERVED_SOCKETS:- stale_port, stale = next(iter(_reserved_sockets.items()))- _reserved_sockets.pop(stale_port, None)+ stale_port = next(+ (p for p in _reserved_sockets if p not in _CIMD_PORTS), None+ )+ if stale_port is None:+ break # only pinned sockets remain — never evict those+ stale = _reserved_sockets.pop(stale_port, None)+ if stale is None:+ continuetry:stale.close()except OSError: