Skip to content

Agent Mesh

sdk/python: drop the router connection when a renewal fails

Antonio Ojea1006a105827a
diff --git a/sdk/python/src/agent_mesh/session.py b/sdk/python/src/agent_mesh/session.py
index b6a8037f..6c96ed6f 100644
--- a/sdk/python/src/agent_mesh/session.py
+++ b/sdk/python/src/agent_mesh/session.py
@@ -619,8 +619,17 @@ async def _authenticate_router(host: IHost, mesh: "AgentMesh", peer_id: ID) -> V
async def _reserve_again(host: IHost, mesh: "AgentMesh", router: AdmittedRouter) -> AdmittedRouter:
peer_id = ID.from_base58(router.peer_id)
credential = router.credential
- if peer_id not in host.get_connected_peers():
- await dial(host, await peer_info(router.addr))
- credential = await _authenticate_router(host, mesh, peer_id)
- reservation = await reserve_relay(host, peer_id)
+ try:
+ if peer_id not in host.get_connected_peers():
+ await dial(host, await peer_info(router.addr))
+ credential = await _authenticate_router(host, mesh, peer_id)
+ reservation = await reserve_relay(host, peer_id)
+ except Exception:
+ # A connection that failed us is not kept: it may be half-open, or up
+ # but unauthenticated. The retry then dials and authenticates again.
+ try:
+ await host.disconnect(peer_id)
+ except Exception: # noqa: BLE001 - already gone
+ pass
+ raise
return replace(router, credential=credential, reservation=reservation)
diff --git a/sdk/python/tests/test_session.py b/sdk/python/tests/test_session.py
index aaae8749..ef2184af 100644
--- a/sdk/python/tests/test_session.py
+++ b/sdk/python/tests/test_session.py
@@ -97,15 +97,19 @@ def libp2p_host(identity: Identity):
)
-def hop_handler(relay_addr: str, grants: bool, ttl: int = 3600, events=None):
- """Answers RESERVE like a go-libp2p relay whose ACL either admits or refuses us."""
+def hop_handler(relay_addr: str, grants, ttl: int = 3600, events=None):
+ """Answers RESERVE like a go-libp2p relay whose ACL either admits or refuses
+ us; `grants` is a bool or a callable of the RESERVE count so far."""
+ reserves = 0
async def handle(stream):
+ nonlocal reserves
req = circuit.HopMessage.FromString(await read_varint_prefixed_bytes(stream))
assert req.type == circuit.HopMessage.RESERVE
+ reserves += 1
if events is not None:
events.append(("reserve", str(stream.muxed_conn.peer_id)))
- if grants:
+ if grants(reserves) if callable(grants) else grants:
resp = circuit.HopMessage(
type=circuit.HopMessage.STATUS,
status=circuit.OK,
@@ -268,3 +272,28 @@ def test_join_renews_the_reservation_and_reauthenticates_after_a_disconnect():
nursery.cancel_scope.cancel()
trio.run(with_timeout, 60, main)
+
+
+def test_a_refused_renewal_drops_the_connection_and_the_retry_authenticates_again():
+ """A router that refuses the renewal, PERMISSION_DENIED when it no longer
+ holds the admission, gets a fresh connection and handshake on the retry
+ instead of the same RESERVE on the same connection forever."""
+
+ async def wait_for(predicate):
+ while not predicate():
+ await trio.sleep(0.1)
+
+ async def main():
+ async with trio.open_nursery() as nursery:
+ events = []
+ _, addr = await start_router(nursery, grants=lambda n: n != 2, ttl=4, events=events)
+ mesh = AgentMesh.enroll("http://127.0.0.1:1", bootstrap_token="sbt", transport=fake_control_plane([addr]))
+ async with mesh.join(refresh_lead=0, refresh_retry=0.5, reservation_lead=1) as session:
+ first = session.routers[0].reservation.expire
+ await wait_for(lambda: events.count(("reserve", mesh.peer_id)) >= 3)
+ await wait_for(lambda: session.routers[0].reservation.expire > first)
+ auth, reserve = ("auth", mesh.peer_id), ("reserve", mesh.peer_id)
+ assert events[:5] == [auth, reserve, reserve, auth, reserve]
+ nursery.cancel_scope.cancel()
+
+ trio.run(with_timeout, 60, main)