From a224edeb9ce9cdb112c62829cb9b9b869ed8b3a8 Mon Sep 17 00:00:00 2001 From: Andrey Buzin Date: Mon, 16 Nov 2026 13:24:12 -0800 Subject: [PATCH] Scope MCP connection locks to the event loop The module-level MCP pool lock bound to the first event loop where connections contended. A later event loop could then fail with a bound-to-a-different-event-loop error. Use weakly held locks keyed by the running event loop. Active and waiting operations keep a strong local reference to their lock. Idle locks and their loops are not retained, and same-loop serialization stays unchanged. --- src/ai/agents/mcp/client.py | 13 +++- tests/agents/mcp/test_client.py | 123 ++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 3 deletions(-) diff --git a/src/ai/agents/mcp/client.py b/src/ai/agents/mcp/client.py index 4ff16246..fbb000f9 100644 --- a/src/ai/agents/mcp/client.py +++ b/src/ai/agents/mcp/client.py @@ -6,6 +6,7 @@ import dataclasses import importlib import json +import weakref from typing import TYPE_CHECKING, Any, cast if TYPE_CHECKING: @@ -37,7 +38,11 @@ class _Connection: contextvars.ContextVar("mcp_connections", default=None) ) -_pool_lock = asyncio.Lock() +# Active operations hold each lock alive. Weak values release idle locks +# and their event loops, including when a contended lock binds to its loop. +_pool_locks: weakref.WeakValueDictionary[ + asyncio.AbstractEventLoop, asyncio.Lock +] = weakref.WeakValueDictionary() def _import_mcp_module(module_name: str) -> Any: @@ -89,7 +94,8 @@ async def _get_or_create_connection( "The connection pool is not initialized." ) - async with _pool_lock: + lock = _pool_locks.setdefault(asyncio.get_running_loop(), asyncio.Lock()) + async with lock: if key in pool: return pool[key].client @@ -323,7 +329,8 @@ async def close_connections() -> None: if pool is None: return - async with _pool_lock: + lock = _pool_locks.setdefault(asyncio.get_running_loop(), asyncio.Lock()) + async with lock: if not pool: return diff --git a/tests/agents/mcp/test_client.py b/tests/agents/mcp/test_client.py index 79e7cfb5..00ca5dfc 100644 --- a/tests/agents/mcp/test_client.py +++ b/tests/agents/mcp/test_client.py @@ -2,15 +2,22 @@ from __future__ import annotations +import asyncio import contextlib import dataclasses +import gc import importlib +import weakref +from collections.abc import AsyncIterator +from types import SimpleNamespace from typing import Any +import mcp.client.session import mcp.types import pytest import ai +from ai.agents.mcp import client as mcp_client from ai.agents.mcp.client import _mcp_tool_to_native from ...conftest import ( @@ -148,3 +155,119 @@ def _spec(tool: ai.AgentTool) -> ai.tools.ToolSpec: spec = tool.tool.spec assert spec is not None return spec + + +class FakeSession: + """Stands in for ``mcp.client.session.ClientSession``.""" + + call_result: mcp.types.CallToolResult | None = None + + def __init__(self, read_stream: Any, write_stream: Any) -> None: + pass + + async def __aenter__(self) -> FakeSession: + return self + + async def __aexit__(self, *args: object) -> None: + return None + + async def initialize(self) -> None: + await asyncio.sleep(0.01) + + async def list_tools(self) -> SimpleNamespace: + tool = mcp.types.Tool( + name="echo", + description="Echo", + input_schema={"type": "object", "properties": {}}, + ) + return SimpleNamespace(tools=[tool]) + + async def call_tool( + self, name: str, arguments: dict[str, Any] + ) -> mcp.types.CallToolResult: + assert self.call_result is not None + return self.call_result + + +@pytest.fixture +def fake_session(monkeypatch: pytest.MonkeyPatch) -> type[FakeSession]: + monkeypatch.setattr(mcp.client.session, "ClientSession", FakeSession) + return FakeSession + + +def test_pool_lock_works_in_a_second_event_loop( + monkeypatch: pytest.MonkeyPatch, fake_session: type[FakeSession] +) -> None: + """Concurrent MCP connects must work in more than one event loop.""" + + @contextlib.asynccontextmanager + async def transport() -> AsyncIterator[tuple[None, None]]: + await asyncio.sleep(0.01) + yield None, None + + async def connect_two_servers() -> None: + async with mcp_client.ensure_connection_pool(): + await asyncio.gather( + mcp_client._get_or_create_connection("key:a", transport), + mcp_client._get_or_create_connection("key:b", transport), + ) + + asyncio.run(connect_two_servers()) + asyncio.run(connect_two_servers()) + + +async def test_concurrent_calls_share_one_connection( + fake_session: type[FakeSession], +) -> None: + started = 0 + + @contextlib.asynccontextmanager + async def transport() -> AsyncIterator[tuple[None, None]]: + nonlocal started + started += 1 + await asyncio.sleep(0.01) + yield None, None + + async with mcp_client.ensure_connection_pool(): + first, second = await asyncio.gather( + mcp_client._get_or_create_connection("key", transport), + mcp_client._get_or_create_connection("key", transport), + ) + assert first is second + + assert started == 1 + assert len(mcp_client._pool_locks) == 0 + + +def test_pool_locks_release_closed_event_loops( + fake_session: type[FakeSession], +) -> None: + loops: list[weakref.ReferenceType[asyncio.AbstractEventLoop]] = [] + locks: list[weakref.ReferenceType[asyncio.Lock]] = [] + + @contextlib.asynccontextmanager + async def transport() -> AsyncIterator[tuple[None, None]]: + loop = asyncio.get_running_loop() + loops.append(weakref.ref(loop)) + locks.append(weakref.ref(mcp_client._pool_locks[loop])) + await asyncio.sleep(0.01) + yield None, None + + async def connect() -> None: + async with mcp_client.ensure_connection_pool() as pool: + await asyncio.gather( + mcp_client._get_or_create_connection("first", transport), + mcp_client._get_or_create_connection("second", transport), + ) + assert len(pool) == 2 + assert not pool + + asyncio.run(connect()) + gc.collect() + assert all(ref() is None for ref in loops + locks) + assert len(mcp_client._pool_locks) == 0 + + asyncio.run(connect()) + gc.collect() + assert all(ref() is None for ref in loops + locks) + assert len(mcp_client._pool_locks) == 0