Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
sdk/python: cap the hang-up after a stalled stream open at one second
Antonio Ojea2f788dd9278e
diff --git a/sdk/python/src/agent_mesh/host.py b/sdk/python/src/agent_mesh/host.pyindex 1482575f..b82a0eb4 100644--- a/sdk/python/src/agent_mesh/host.py+++ b/sdk/python/src/agent_mesh/host.py@@ -130,6 +130,9 @@ def _dial_websockets_by_name(transport: WebsocketTransport) -> None:# a closer peer to every lookup.DIAL_TIMEOUT = 15.0+# How long open_stream waits for the close of a connection it gave up on.+HANGUP_GRACE = 1.0+async def dial(host: IHost, info: PeerInfo) -> None:"""host.connect, bounded by DIAL_TIMEOUT."""@@ -153,7 +156,7 @@ async def open_stream(host: IHost, peer_id: ID, protocol: TProtocol, timeout: flexcept trio.TooSlowError:# Closing writes a GO_AWAY on the very connection that stalled; give# it a moment, then let the peer time the socket out on its own.- with trio.CancelScope(shield=True), trio.move_on_after(timeout):+ with trio.CancelScope(shield=True), trio.move_on_after(min(timeout, HANGUP_GRACE)):try:await host.disconnect(peer_id)except Exception: # noqa: BLE001 - already gonediff --git a/sdk/python/tests/test_host.py b/sdk/python/tests/test_host.pyindex 7da7360b..80301896 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_FORMATfrom libp2p.transport.websocket.transport import WebsocketTransportfrom multiaddr.resolvers import DNSResolver-from agent_mesh.host import DIAL_TIMEOUT, create_mesh_host, dial, dial_addrs, open_stream, peer_info+from agent_mesh.host import DIAL_TIMEOUT, HANGUP_GRACE, create_mesh_host, dial, dial_addrs, open_stream, peer_infofrom agent_mesh.identity import IdentityROUTER = "12D3KooWG1pA6goegCncqwbZLSr8pnjUZ6JMAAe6SmnHTgUNCk88"@@ -276,7 +276,7 @@ def test_open_stream_ends_at_its_timeout():def test_hanging_up_on_a_stalled_connection_is_bounded_too():"""Closing the connection writes a GO_AWAY on the very connection whoseSYN stalled; on a blackholed peer that write parks as well. The caller- gets its ConnectionError within a second timeout, not never."""+ gets its ConnectionError a moment after its timeout, not never."""class Host:async def new_stream(self, peer_id, protocols):@@ -289,7 +289,7 @@ def test_hanging_up_on_a_stalled_connection_is_bounded_too():started = trio.current_time()with pytest.raises(ConnectionError, match="within 10s"):await open_stream(Host(), ID.from_base58(ROUTER), ECHO, 10)- assert trio.current_time() - started == pytest.approx(20)+ assert trio.current_time() - started == pytest.approx(10 + HANGUP_GRACE)trio.run(main, clock=trio.testing.MockClock(autojump_threshold=0))