Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
sdk/python: no resource manager, no background dialer on the host
Antonio Ojea190fe09f3eed
diff --git a/sdk/python/src/agent_mesh/host.py b/sdk/python/src/agent_mesh/host.pyindex f23f072b..3f7c4621 100644--- a/sdk/python/src/agent_mesh/host.py+++ b/sdk/python/src/agent_mesh/host.py@@ -33,6 +33,7 @@ from libp2p import new_hostfrom libp2p.abc import IHost, INetStreamfrom libp2p.crypto.ed25519 import create_new_key_pairfrom libp2p.custom_types import TProtocol+from libp2p.network.config import ConnectionConfigfrom libp2p.peer.id import IDfrom libp2p.peer.peerinfo import PeerInfo, info_from_p2p_addrfrom libp2p.security.tls.transport import PROTOCOL_ID as TLS_PROTOCOL_ID@@ -65,6 +66,15 @@ def _libp2p_version() -> tuple[int, int]:# Delete this block and its tests with the libp2p>=0.8 bump.LIBP2P_LEAKS_STREAM_SLOTS = _libp2p_version() < (0, 8)+# py-libp2p 0.7's Swarm.upgrade_*_raw_conn opens a ResourceManager connection+# scope and stores it on the muxed connection, then add_conn opens a second+# one and stores it on the SwarmConn; only the second is closed when the+# connection ends. A SecurityUpgradeFailure closes the raw connection and+# raises without closing the pre-upgrade scope either. Each leaked scope is+# one of the manager's 1000 connection slots for the life of the process.+# Retire with the same bump.+LIBP2P_LEAKS_CONNECTION_SCOPES = _libp2p_version() < (0, 8)+async def _open_stream_returning_slot(self: Yamux) -> YamuxStream:await self.stream_backlog_semaphore.acquire()@@ -153,7 +163,21 @@ def create_mesh_host(identity: Identity, listen_addrs: Sequence[str] = ()) -> tukey_pair=key_pair,sec_opt={TLS_PROTOCOL_ID: tls},muxer_opt={TProtocol(YAMUX_PROTOCOL_ID): Yamux},+ # A member connects to its routers and to the peers it calls or that+ # call it. py-libp2p's AutoConnector would otherwise dial every peer+ # in the peerstore every 30s while under 100 connections, including+ # addresses this host has no transport for and relay addresses it+ # dials as if direct, and each failed TLS handshake leaks below.+ connection_config=ConnectionConfig(min_connections=0, low_watermark=0),)+ if LIBP2P_LEAKS_CONNECTION_SCOPES:+ # py-libp2p 0.7's Swarm opens two ResourceManager connection scopes+ # per connection and closes one, and none after a failed security+ # upgrade. After 1000 leaked slots the manager degrades its limit to+ # one connection and never recovers, and the host refuses every peer.+ # The Swarm's own limits (max_connections, max_connections_per_peer)+ # do not depend on the manager and stay in force.+ host.get_network().set_resource_manager(None) # type: ignore[attr-defined]if str(host.get_id()) != identity.peer_id:raise RuntimeError(f"libp2p derived peer {host.get_id()} for identity {identity.peer_id}")return host, [multiaddr.Multiaddr(a) for a in listen_addrs]diff --git a/sdk/python/tests/test_host.py b/sdk/python/tests/test_host.pyindex 13725a99..aace0da2 100644--- a/sdk/python/tests/test_host.py+++ b/sdk/python/tests/test_host.py@@ -30,7 +30,7 @@ from libp2p.peer.peerinfo import PeerInfo, info_from_p2p_addrfrom libp2p.stream_muxer.yamux.yamux import FLAG_SYN, YAMUX_HEADER_FORMATfrom multiaddr.resolvers import DNSResolver-from agent_mesh.host import LIBP2P_LEAKS_STREAM_SLOTS, create_mesh_host, dial_addrs, open_stream, peer_info+from agent_mesh.host import LIBP2P_LEAKS_CONNECTION_SCOPES, LIBP2P_LEAKS_STREAM_SLOTS, create_mesh_host, dial_addrs, open_stream, peer_infofrom agent_mesh.identity import Identityfrom agent_mesh.session import DIAL_TIMEOUT, dial@@ -213,3 +213,51 @@ def test_the_stream_slot_workaround_retires_with_libp2p_0_8():major, minor = (int(p) for p in importlib.metadata.version("libp2p").split(".")[:2])assert LIBP2P_LEAKS_STREAM_SLOTS == ((major, minor) < (0, 8))+++def test_the_host_runs_without_a_resource_manager_and_a_background_dialer():+ """py-libp2p 0.7 leaks a ResourceManager connection slot per connection+ and per failed security upgrade; at 1000 the manager degrades to one+ connection for good, and the host refuses every peer. And its+ AutoConnector dials every peer in the peerstore every 30s while under+ 100 connections, which for a member is always. Neither runs here."""+ host, _ = create_mesh_host(Identity.generate())+ swarm = host.get_network()+ assert swarm._resource_manager is None # noqa: SLF001 - py-libp2p offers no getter+ assert swarm.connection_config.low_watermark == 0+ assert swarm.connection_config.min_connections == 0+ # The Swarm's own limits do not depend on the manager.+ assert swarm.connection_config.max_connections > 0+ assert swarm.connection_config.max_connections_per_peer > 0+++def test_a_connection_ends_with_no_slot_held():+ """Connections come and go for a member's whole life; each must leave+ the host as it found it. With the manager gone there is no slot to+ hold; this pins that a connect and a disconnect round-trip cleanly, so+ a future manager cannot come back with the leak unnoticed."""++ async def main():+ server, server_listen = create_mesh_host(Identity.generate(), ["/ip4/127.0.0.1/tcp/0"])+ client, _ = create_mesh_host(Identity.generate())+ async with server.run(listen_addrs=server_listen), client.run(listen_addrs=[]):+ info = info_from_p2p_addr(multiaddr.Multiaddr(f"{server.get_addrs()[0]}"))+ for _ in range(5):+ with trio.fail_after(10):+ await client.connect(info)+ assert server.get_id() in client.get_connected_peers()+ await client.disconnect(server.get_id())+ await trio.sleep(0.05)+ assert client.get_network().get_total_connections() == 0+ assert client.get_network()._resource_manager is None # noqa: SLF001++ trio.run(main)+++def test_the_connection_scope_workaround_retires_with_libp2p_0_8():+ """When the pin moves past 0.7, delete the set_resource_manager(None)+ call and this flag in host.py, and check the leak is gone upstream."""+ import importlib.metadata++ major, minor = (int(p) for p in importlib.metadata.version("libp2p").split(".")[:2])+ assert LIBP2P_LEAKS_CONNECTION_SCOPES == ((major, minor) < (0, 8))