Skip to content

Agent Mesh

sdk/python: py-libp2p 0.8, without the 0.7 workarounds

Antonio Ojeadb8b7175230d
diff --git a/sdk/README.md b/sdk/README.md
index 1dd3e22e..89deccd2 100644
--- a/sdk/README.md
+++ b/sdk/README.md
@@ -68,19 +68,23 @@ its credential under the new key.
Facts about the libp2p implementations that the SDKs work around, each
pinned by a test:
-- py-libp2p 0.7 advertises early muxer negotiation in TLS ALPN but does not
- complete it, and go-libp2p then refuses the mux upgrade. The Python host
- sets no ALPN muxer list, so the muxer is negotiated with
- multistream-select (`sdk/python/src/agent_mesh/host.py`).
+- py-libp2p's TLS transport advertises early muxer negotiation in ALPN but
+ cannot complete it (Python's `ssl` has no ALPN select callback), and
+ go-libp2p then refuses the mux upgrade. The Python host sets no ALPN
+ muxer list, so the muxer is negotiated with multistream-select
+ (`sdk/python/src/agent_mesh/host.py`).
- js-libp2p's `@libp2p/tls` reads the libp2p extension from
`extensions[0]` of the peer certificate. py-libp2p's default certificate
puts BasicConstraints and KeyUsage first, so the Python host uses a
certificate template with only the libp2p extension.
-- py-libp2p's circuit relay v2 client sends its protobufs without the
- varint length prefix that go-libp2p (and js-libp2p) use, so it cannot talk
- to a router. The Python SDK carries the relay protocol itself
- (`sdk/python/src/agent_mesh/relay.py`, `sdk/python/proto/circuit.proto`)
- on top of py-libp2p's raw-connection upgrade.
+- A router grants a relay reservation only after the auth handshake on
+ the same connection, and drops it with the connection. The Python SDK
+ carries the relay protocol itself (`sdk/python/src/agent_mesh/relay.py`,
+ `sdk/python/proto/circuit.proto`) on top of py-libp2p's raw-connection
+ upgrade, so the session decides when to reserve, renew and, after a
+ dropped connection, authenticate and reserve again; py-libp2p's own
+ `RelayDiscovery` reserves again only after the expiry has passed, when
+ the router has already dropped the slot.
- go-libp2p's relay leaves private addresses out of a reservation, so a
router on loopback (every test) grants a reservation that lists no
address. js-libp2p falls back to the connection's address; the Python SDK
@@ -90,26 +94,32 @@ pinned by a test:
handshake. The JS SDK starts the listener after the handshake, through the
transport manager, which is not on the public `Libp2p` interface.
- go-libp2p's relay grants a reservation for one hour and drops it when
- that passes; a member that still advertises the relayed address is then
- unreachable (`NO_RESERVATION`). js-libp2p's listener renews on its own.
- The Python SDK renews two minutes before the expiry the router returned,
- and runs the auth handshake again first when the router has closed the
- connection in between, since the router forgets the admission with it.
- py-libp2p 0.7 cannot take this over: its relay client has the framing
- problem above, and its `RelayDiscovery` reserves again only after the
- expiry has passed, when the router has already dropped the slot.
-- py-libp2p 0.7's yamux takes one of 256 per-connection backlog slots for
- every outbound stream and returns it only when sending the SYN fails, so
- the 257th `open_stream` on a connection blocks forever with no error. A
- member that keeps one connection to its router reaches that in hours (a
- reservation renewal every hour, a control plane pull every fifteen
- minutes). `main`
- releases the slot on close (libp2p/py-libp2p#1426); until that is
- released, `host.py` replaces `Yamux.open_stream` with one that does the
- same, since py-libp2p constructs `Yamux` by name whatever `muxer_opt`
- says. Every stream the SDK opens also goes through `open_stream`, which
- bounds `new_stream` with the caller's deadline, so a muxer that cannot
- open a stream fails the call instead of parking it.
+ that passes, and with the connection it was made on; a member that still
+ advertises the relayed address is then unreachable (`NO_RESERVATION`).
+ js-libp2p's listener renews on its own, but only on a connection the
+ router still holds the admission of; the JS SDK runs the handshake again
+ on every new connection to a router and, when the relayed address is
+ gone, authenticates and reserves again within thirty seconds. The Python
+ SDK renews two minutes before the expiry the router returned, and within
+ thirty seconds of the connection to that router going, running the auth
+ handshake again first.
+- py-libp2p's yamux gives a stream's backlog slot (256 per connection) back
+ when the stream closes since 0.8, but not for the `trio.Cancelled` of a
+ caller's deadline while the SYN waits on a stalled connection. Every
+ stream the SDK opens goes through `open_stream`, which bounds
+ `new_stream` with the caller's deadline and hangs up on a connection that
+ cannot open a stream in time, so the call fails instead of parking and
+ the slots go with the connection.
+- py-libp2p's yamux drops a reset stream's bookkeeping once its buffer is
+ drained, and a bounded read then reports the end of the stream. The
+ Python `/libp2p-http` client reads the muxed stream's reset flag on EOF,
+ so a body cut by a reset is an error and not a body that ended.
+- py-libp2p's WebSocket transport resolves a `/dns4/<host>/tcp/443/wss`
+ address to its IP before dialing and names the IP in the TLS SNI and the
+ Host header, which a TLS-terminating edge answers with 403; the Python
+ host dials such addresses by name (libp2p/py-libp2p#1549). It also dials
+ `wss` with certificate verification off unless given a TLS context
+ (libp2p/py-libp2p#1550); the host gives it the system roots.
- py-libp2p's Kademlia client takes a protocol prefix but its provider
lookups still speak `/ipfs/kad/1.0.0`, so it cannot reach the mesh DHT.
The Python SDK does a bounded GET_PROVIDERS walk itself on
@@ -403,7 +413,7 @@ holds against the control plane's records.
WebSocket or not at all; `tests/integration/standalone_sdk_test.go` holds
both SDKs to it. JS: `libp2p`, `@libp2p/tcp`, `@libp2p/websockets`,
`@libp2p/tls`, `@chainsafe/libp2p-yamux`, `@libp2p/circuit-relay-v2`,
- `@libp2p/identify`. Python: `libp2p>=0.7` (`security.tls`,
+ `@libp2p/identify`. Python: `libp2p>=0.8` (`security.tls`,
`stream_muxer.yamux`) plus the SDK's own relay client; py-libp2p is
trio-based, so `join()` is an async context manager.
- Biscuit verification of a peer's credential: signature under any trusted
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index 9492ff74..380f6c02 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -16,7 +16,7 @@ dependencies = [
# HTTP/1.1 on a libp2p stream (/libp2p-http) and forwarding to a backend.
"h11>=0.14",
"httpx>=0.27",
- "libp2p>=0.7,<0.8",
+ "libp2p>=0.8,<0.9",
"mcp>=2,<3",
"multiaddr>=0.2",
# py-libp2p requires protobuf<7; stay co-installable.
diff --git a/sdk/python/src/agent_mesh/host.py b/sdk/python/src/agent_mesh/host.py
index 3d04b651..1576b375 100644
--- a/sdk/python/src/agent_mesh/host.py
+++ b/sdk/python/src/agent_mesh/host.py
@@ -19,11 +19,8 @@ trio-based, so everything here is trio async."""
from __future__ import annotations
-import importlib.metadata
import os
-import re
import ssl
-import struct
from datetime import datetime, timedelta, timezone
from typing import Sequence
@@ -43,102 +40,14 @@ from libp2p.security.noise.transport import PROTOCOL_ID as NOISE_PROTOCOL_ID
from libp2p.security.noise.transport import Transport as NoiseTransport
from libp2p.security.tls.transport import PROTOCOL_ID as TLS_PROTOCOL_ID
from libp2p.security.tls.transport import IdentityConfig, TLSTransport
-from libp2p.stream_muxer.exceptions import MuxedStreamError
-from libp2p.stream_muxer.yamux.yamux import FLAG_SYN, TYPE_WINDOW_UPDATE, YAMUX_HEADER_FORMAT
from libp2p.stream_muxer.yamux.yamux import PROTOCOL_ID as YAMUX_PROTOCOL_ID
-from libp2p.stream_muxer.yamux.yamux import Yamux, YamuxStream
+from libp2p.stream_muxer.yamux.yamux import Yamux
from libp2p.transport.websocket.transport import WebsocketTransport
from multiaddr.resolvers import DNSResolver
from .identity import Identity
-def _libp2p_version() -> tuple[int, int]:
- # PEP 440 lets a pre-release follow the minor directly (0.8a1), so the
- # string is not split on dots.
- m = re.match(r"(\d+)\.(\d+)", importlib.metadata.version("libp2p"))
- return (int(m.group(1)), int(m.group(2))) if m else (0, 0)
-
-
-# py-libp2p 0.7 takes one of the 256 slots of a connection's
-# stream_backlog_semaphore for every outbound stream and gives it back only
-# when sending the SYN fails, so after 256 streams on one connection every
-# open_stream blocks forever, silently. main releases the slot when the local
-# side closes the stream (libp2p/py-libp2p#1426); until that is released,
-# Yamux.open_stream is replaced here with the same steps plus that release,
-# and the slot is returned on any failure, a trio.Cancelled after the acquire
-# included, which 0.7 (except Exception) keeps. A subclass would not do:
-# MuxerMultistream.new_conn constructs Yamux by name, whatever muxer_opt says.
-# 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()
- released = False
-
- def release_once() -> None:
- nonlocal released
- if not released:
- released = True
- self.stream_backlog_semaphore.release()
-
- stream_id: int | None = None
- try:
- async with self.streams_lock:
- if self.event_shutting_down.is_set():
- raise MuxedStreamError("Connection is shutting down")
- stream_id = self.next_stream_id
- self.next_stream_id += 2
- stream = YamuxStream(stream_id, self, True)
- self.streams[stream_id] = stream
- self.stream_buffers[stream_id] = bytearray()
- self.stream_events[stream_id] = trio.Event()
- await self._write_frame(struct.pack(YAMUX_HEADER_FORMAT, 0, TYPE_WINDOW_UPDATE, FLAG_SYN, stream_id, 0))
- except BaseException as err:
- release_once()
- if stream_id is not None:
- with trio.CancelScope(shield=True):
- async with self.streams_lock:
- self.streams.pop(stream_id, None)
- self.stream_buffers.pop(stream_id, None)
- self.stream_events.pop(stream_id, None)
- if isinstance(err, Exception) and not isinstance(err, MuxedStreamError):
- raise MuxedStreamError(f"Failed to send SYN: {err}") from err
- raise
-
- close, reset = stream.close, stream.reset
-
- async def close_and_release() -> None:
- try:
- await close()
- finally:
- release_once()
-
- async def reset_and_release() -> None:
- try:
- await reset()
- finally:
- release_once()
-
- stream.close = close_and_release # type: ignore[method-assign]
- stream.reset = reset_and_release # type: ignore[method-assign]
- return stream
-
-
-if LIBP2P_LEAKS_STREAM_SLOTS:
- Yamux.open_stream = _open_stream_returning_slot # type: ignore[method-assign]
-
-
def _certificate_template() -> x509.CertificateBuilder:
"""The libp2p TLS certificate with nothing but the libp2p extension.
py-libp2p's default template also adds BasicConstraints and KeyUsage,
@@ -163,9 +72,10 @@ def create_mesh_host(identity: Identity, listen_addrs: Sequence[str] = ()) -> tu
relay instead. The host dials TCP and WebSocket: the testnets' routers
listen on TCP, sam-one's single port is a WebSocket listener."""
key_pair = create_new_key_pair(identity.seed)
- # No ALPN muxer list: py-libp2p 0.7 advertises early muxer negotiation
- # but does not complete it, and go-libp2p then refuses the mux upgrade.
- # Without it the muxer is negotiated with multistream-select as before.
+ # No ALPN muxer list: py-libp2p's TLS transport advertises early muxer
+ # negotiation but cannot complete it (Python's ssl has no ALPN select
+ # callback), and go-libp2p then refuses the mux upgrade. Without it the
+ # muxer is negotiated with multistream-select as before.
tls = TLSTransport(key_pair, identity_config=IdentityConfig(cert_template=_certificate_template()))
# TLS first, as every Go peer and the JS SDK offer it; Noise accepted, so
# a member in a browser, which speaks Noise alone, is reached end to end
@@ -176,24 +86,17 @@ def create_mesh_host(identity: Identity, listen_addrs: Sequence[str] = ()) -> tu
sec_opt={TLS_PROTOCOL_ID: tls, NOISE_PROTOCOL_ID: noise},
muxer_opt={TProtocol(YAMUX_PROTOCOL_ID): Yamux},
enable_websocket=True,
- # py-libp2p 0.7 dials wss with certificate verification off unless
- # given a context; the system roots, as go-libp2p and js-libp2p use.
+ # py-libp2p dials wss with certificate verification off unless given
+ # a context (libp2p/py-libp2p#1550); the system roots, as go-libp2p
+ # and js-libp2p use.
tls_client_config=ssl.create_default_context(),
# 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.
+ # dials as if direct.
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]
for transport in host.get_network().transport_manager.get_transports(): # type: ignore[attr-defined]
if isinstance(transport, WebsocketTransport):
_dial_websockets_by_name(transport)
@@ -203,12 +106,13 @@ def create_mesh_host(identity: Identity, listen_addrs: Sequence[str] = ()) -> tu
def _dial_websockets_by_name(transport: WebsocketTransport) -> None:
- """py-libp2p 0.7 resolves a `/dns4/<host>/tcp/443/wss` address to its IP
+ """py-libp2p resolves a `/dns4/<host>/tcp/443/wss` address to its IP
before dialing and then names the IP in the TLS SNI and the Host header,
which a TLS-terminating edge (sam-one behind a tunnel) answers with 403.
The transport's own dial of an unresolved address keeps the name; the
dial here goes straight to it, as go-libp2p and js-libp2p do. Fixed
- upstream by libp2p/py-libp2p#1549; drop this with the release that has it."""
+ upstream by libp2p/py-libp2p#1549, not in 0.8; drop this with the
+ release that has it."""
# Bound here so a py-libp2p that renamed it fails at host construction.
dial_resolved = transport._dial_resolved # noqa: SLF001
@@ -221,11 +125,19 @@ def _dial_websockets_by_name(transport: WebsocketTransport) -> None:
async def open_stream(host: IHost, peer_id: ID, protocol: TProtocol, timeout: float) -> INetStream:
"""host.new_stream bounded by a timeout. py-libp2p bounds the protocol
negotiation but not the muxer, and a muxer that cannot open a stream
- would otherwise park the caller forever without a word."""
+ would otherwise park the caller forever without a word. A connection
+ that stalled so is hung up on: the muxer keeps the backlog slot of a SYN
+ it never sent past a cancelled write, and a connection the peer does not
+ answer on is no path to it; the next call dials afresh."""
try:
with trio.fail_after(timeout):
return await host.new_stream(peer_id, [protocol])
except trio.TooSlowError:
+ with trio.CancelScope(shield=True):
+ try:
+ await host.disconnect(peer_id)
+ except Exception: # noqa: BLE001 - already gone
+ pass
raise ConnectionError(f"no {protocol} stream to {peer_id} within {timeout:g}s") from None
diff --git a/sdk/python/src/agent_mesh/libp2p_http.py b/sdk/python/src/agent_mesh/libp2p_http.py
index 1642fa5a..05a826d8 100644
--- a/sdk/python/src/agent_mesh/libp2p_http.py
+++ b/sdk/python/src/agent_mesh/libp2p_http.py
@@ -285,7 +285,12 @@ async def _read_or_eof(stream: INetStream, peer_id: ID) -> bytes:
tell a body that ended from one that was cut."""
try:
return await stream.read(_READ_CHUNK)
- except StreamEOF:
+ except StreamEOF as err:
+ # py-libp2p 0.8's yamux drops a reset stream's bookkeeping once its
+ # buffer is drained, and a bounded read then reports the end of the
+ # stream; the muxed stream itself still knows it was reset.
+ if getattr(getattr(stream, "muxed_stream", None), "reset_received", False):
+ raise ConnectionError(f"peer {peer_id} broke the stream mid-response: reset") from err
return b""
except Exception as err:
raise ConnectionError(f"peer {peer_id} broke the stream mid-response: {err}") from err
diff --git a/sdk/python/src/agent_mesh/relay.py b/sdk/python/src/agent_mesh/relay.py
index 17704b6c..ca3d2b5d 100644
--- a/sdk/python/src/agent_mesh/relay.py
+++ b/sdk/python/src/agent_mesh/relay.py
@@ -14,9 +14,11 @@
"""Circuit relay v2 as a client of a router: reserve a slot, dial a peer
through the router, accept a peer the router relays to us. Messages are
-varint-length-prefixed protobufs (go-libp2p's pbio framing); py-libp2p 0.7's
-own relay client sends them unframed and cannot talk to a Go relay, which is
-why this module speaks the protocol directly."""
+varint-length-prefixed protobufs (go-libp2p's pbio framing). The module
+speaks the protocol directly rather than through py-libp2p's relay client:
+a router grants a reservation only after the auth handshake on the same
+connection, and the session owns when to reserve, renew and, after a
+dropped connection, authenticate and reserve again."""
from __future__ import annotations
diff --git a/sdk/python/tests/test_host.py b/sdk/python/tests/test_host.py
index 09a191a9..1770a96c 100644
--- a/sdk/python/tests/test_host.py
+++ b/sdk/python/tests/test_host.py
@@ -32,7 +32,7 @@ from libp2p.stream_muxer.yamux.yamux import FLAG_SYN, YAMUX_HEADER_FORMAT
from libp2p.transport.websocket.transport import WebsocketTransport
from multiaddr.resolvers import DNSResolver
-from agent_mesh.host import LIBP2P_LEAKS_CONNECTION_SCOPES, LIBP2P_LEAKS_STREAM_SLOTS, create_mesh_host, dial_addrs, open_stream, peer_info
+from agent_mesh.host import create_mesh_host, dial_addrs, open_stream, peer_info
from agent_mesh.identity import Identity
from agent_mesh.session import DIAL_TIMEOUT, dial
@@ -112,9 +112,9 @@ def test_a_concrete_address_passes_through():
def test_a_websocket_address_is_dialed_by_its_name():
"""A TLS-terminating edge in front of the router (sam-one behind a tunnel)
selects the origin by the name in the TLS SNI and the Host header.
- py-libp2p 0.7 resolves the name first and sends the IP; the host sends
- the name. Pinned on the Host header of the upgrade request, the SNI is
- the same string."""
+ py-libp2p resolves the name first and sends the IP (libp2p/py-libp2p#1549
+ not yet released); the host sends the name. Pinned on the Host header of
+ the upgrade request, the SNI is the same string."""
seen: dict[str, str] = {}
async def record(stream):
@@ -190,8 +190,9 @@ ECHO = TProtocol("/test/echo/1.0.0")
def test_a_connection_outlives_the_yamux_stream_backlog():
"""A member keeps one connection to its router for days and opens a
short stream on it every few minutes (DHT provide, reservation renewal).
- py-libp2p 0.7 never returns a closed stream's slot to the connection's
- 256-slot backlog, so the 257th open_stream parks forever; ours must not."""
+ py-libp2p 0.7 never returned a closed stream's slot to the connection's
+ 256-slot backlog, so the 257th open_stream parked forever; 0.8 returns
+ it, and this holds it to that."""
async def echo(stream):
try:
@@ -218,19 +219,22 @@ def test_a_connection_outlives_the_yamux_stream_backlog():
trio.run(main)
-def test_a_stream_open_cut_by_its_deadline_returns_the_slot():
- """py-libp2p 0.7 releases the backlog slot only for an Exception; a
- trio.Cancelled from the caller's deadline, raised while the SYN waits on
- a stalled connection, kept it. Every such timeout was one slot fewer."""
+def test_a_stream_open_cut_by_its_deadline_ends_the_connection():
+ """py-libp2p's yamux gives a backlog slot back for an Exception while
+ sending the SYN, not for the trio.Cancelled of the caller's deadline: a
+ SYN that stalls on a dead connection would keep one of the 256 slots
+ each time. A connection that cannot open a stream in time is no path to
+ the peer, so open_stream hangs up on it, slots and all, and the next
+ dial starts afresh."""
async def main():
server, server_listen = create_mesh_host(Identity.generate(), ["/ip4/127.0.0.1/tcp/0"])
client, _ = create_mesh_host(Identity.generate())
server.set_stream_handler(ECHO, lambda stream: stream.close())
async with server.run(listen_addrs=server_listen), client.run(listen_addrs=[]):
- await client.connect(info_from_p2p_addr(multiaddr.Multiaddr(f"{server.get_addrs()[0]}")))
+ info = info_from_p2p_addr(multiaddr.Multiaddr(f"{server.get_addrs()[0]}"))
+ await client.connect(info)
mux = client.get_network().connections[server.get_id()][0].muxed_conn
- slots = mux.stream_backlog_semaphore.value
write_frame = mux._write_frame
async def syn_stalls(header):
@@ -240,11 +244,14 @@ def test_a_stream_open_cut_by_its_deadline_returns_the_slot():
await write_frame(header)
mux._write_frame = syn_stalls
- for _ in range(3):
- with pytest.raises(ConnectionError, match="within 0.2s"):
- await open_stream(client, server.get_id(), ECHO, 0.2)
- mux._write_frame = write_frame
- assert mux.stream_backlog_semaphore.value == slots
+ with pytest.raises(ConnectionError, match="within 0.2s"):
+ await open_stream(client, server.get_id(), ECHO, 0.2)
+ assert server.get_id() not in client.get_connected_peers()
+
+ # A fresh connection has every slot.
+ await client.connect(info)
+ mux = client.get_network().connections[server.get_id()][0].muxed_conn
+ slots = mux.stream_backlog_semaphore.value
with trio.fail_after(5):
stream = await open_stream(client, server.get_id(), ECHO, 5)
await stream.close()
@@ -267,19 +274,10 @@ def test_open_stream_ends_at_its_timeout():
trio.run(main, clock=trio.testing.MockClock(autojump_threshold=0))
-def test_the_stream_slot_workaround_retires_with_libp2p_0_8():
- """When the pin moves past 0.7, delete the Yamux.open_stream patch and
- this flag in host.py, and the backlog test above."""
- import importlib.metadata
-
- major, minor = (int(p) for p in importlib.metadata.version("libp2p").split(".")[:2])
- assert LIBP2P_LEAKS_STREAM_SLOTS == ((major, minor) < (0, 8))
-
-
def test_wss_is_dialed_with_certificate_verification():
- """py-libp2p 0.7 dials wss with verification off unless given a TLS
- client context; the host gives it the system roots, so an edge's
- certificate is checked as go-libp2p and js-libp2p check it."""
+ """py-libp2p dials wss with verification off unless given a TLS client
+ context (libp2p/py-libp2p#1550); the host gives it the system roots, so
+ an edge's certificate is checked as go-libp2p and js-libp2p check it."""
host, _ = create_mesh_host(Identity.generate())
transports = host.get_network().transport_manager.get_transports() # type: ignore[attr-defined]
ws = next(t for t in transports if isinstance(t, WebsocketTransport))
@@ -289,28 +287,23 @@ def test_wss_is_dialed_with_certificate_verification():
assert context.check_hostname
-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."""
+def test_the_host_runs_without_a_background_dialer():
+ """py-libp2p's AutoConnector dials every peer in the peerstore every 30s
+ while under 100 connections, which for a member is always. It does not
+ run here; the Swarm's own limits do."""
host, _ = create_mesh_host(Identity.generate())
swarm = host.get_network()
- if LIBP2P_LEAKS_CONNECTION_SCOPES:
- 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. While the manager is off there is no slot to
- hold; once a libp2p without the leak brings it back, the slots it
- counts must return to zero when the connections do."""
+ the host as it found it. py-libp2p 0.7 kept one ResourceManager
+ connection slot per connection and degraded to one connection at 1000;
+ the slots 0.8 counts must return to zero when the connections do."""
async def main():
server, server_listen = create_mesh_host(Identity.generate(), ["/ip4/127.0.0.1/tcp/0"])
@@ -325,18 +318,7 @@ def test_a_connection_ends_with_no_slot_held():
await trio.sleep(0.05)
assert client.get_network().get_total_connections() == 0
manager = client.get_network()._resource_manager # noqa: SLF001 - py-libp2p offers no getter
- if LIBP2P_LEAKS_CONNECTION_SCOPES:
- assert manager is None
- elif manager is not None:
- assert manager._current_connections == 0 # noqa: SLF001
+ assert manager is not None
+ assert manager._current_connections == 0 # 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))