Skip to content

Agent Mesh

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.py
index 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: fl
except 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 gone
diff --git a/sdk/python/tests/test_host.py b/sdk/python/tests/test_host.py
index 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_FORMAT
from libp2p.transport.websocket.transport import WebsocketTransport
from 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_info
from agent_mesh.identity import Identity
ROUTER = "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 whose
SYN 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))