From 13930ae718bd29042adb823b9922d218813c2126 Mon Sep 17 00:00:00 2001 From: mhchia Date: Tue, 4 Feb 2020 22:51:21 +0800 Subject: [PATCH] SwarmConn: perform `close` right away In `_handle_new_streams`, when the underlying muxed conn is unavailable, close `SwarmConn` itself right away, to reset all the streams. Therefore, the stream processed by `_handle_muxed_stream` are conscious of the fact that they are reset. It allows a more graceful clean up. --- libp2p/network/connection/swarm_connection.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libp2p/network/connection/swarm_connection.py b/libp2p/network/connection/swarm_connection.py index d21da48..f6e6078 100644 --- a/libp2p/network/connection/swarm_connection.py +++ b/libp2p/network/connection/swarm_connection.py @@ -61,12 +61,11 @@ class SwarmConn(INetConn): try: stream = await self.muxed_conn.accept_stream() except MuxedConnUnavailable: + await self.close() break # Asynchronously handle the accepted stream, to avoid blocking the next stream. nursery.start_soon(self._handle_muxed_stream, stream) - await self.close() - async def _handle_muxed_stream(self, muxed_stream: IMuxedStream) -> None: net_stream = self._add_stream(muxed_stream) try: