diff --git a/libp2p/network/swarm.py b/libp2p/network/swarm.py index 4d53b80..711dc88 100644 --- a/libp2p/network/swarm.py +++ b/libp2p/network/swarm.py @@ -7,6 +7,7 @@ from multiaddr import Multiaddr from libp2p.peer.id import ID from libp2p.peer.peerstore import PeerStoreError from libp2p.peer.peerstore_interface import IPeerStore +from libp2p.protocol_muxer.exceptions import MultiselectClientError from libp2p.protocol_muxer.multiselect import Multiselect from libp2p.protocol_muxer.multiselect_client import MultiselectClient from libp2p.protocol_muxer.multiselect_communicator import MultiselectCommunicator @@ -176,9 +177,16 @@ class Swarm(INetwork): muxed_stream = await muxed_conn.open_stream() # Perform protocol muxing to determine protocol to use - selected_protocol = await self.multiselect_client.select_one_of( - list(protocol_ids), MultiselectCommunicator(muxed_stream) - ) + try: + selected_protocol = await self.multiselect_client.select_one_of( + list(protocol_ids), MultiselectCommunicator(muxed_stream) + ) + except MultiselectClientError as error: + logger.debug("fail to open a stream to peer %s, error=%s", peer_id, error) + await muxed_stream.reset() + raise SwarmException( + "failt to open a stream to peer %s", peer_id + ) from error # Create a net stream with the selected protocol net_stream = NetStream(muxed_stream) diff --git a/libp2p/pubsub/pubsub.py b/libp2p/pubsub/pubsub.py index ef069d8..e413b28 100644 --- a/libp2p/pubsub/pubsub.py +++ b/libp2p/pubsub/pubsub.py @@ -19,11 +19,12 @@ from lru import LRU from libp2p.exceptions import ParseError, ValidationError from libp2p.host.host_interface import IHost from libp2p.io.exceptions import IncompleteReadError +from libp2p.network.exceptions import SwarmException +from libp2p.network.stream.exceptions import StreamEOF, StreamReset from libp2p.network.stream.net_stream_interface import INetStream from libp2p.peer.id import ID from libp2p.typing import TProtocol from libp2p.utils import encode_varint_prefixed, read_varint_prefixed_bytes -from libp2p.network.stream.exceptions import StreamEOF, StreamReset from .pb import rpc_pb2 from .pubsub_notifee import PubsubNotifee @@ -245,7 +246,11 @@ class Pubsub: # disconnect the peer? async def _handle_new_peer(self, peer_id: ID) -> None: - stream: INetStream = await self.host.new_stream(peer_id, self.protocols) + try: + stream: INetStream = await self.host.new_stream(peer_id, self.protocols) + except SwarmException as error: + logger.debug("fail to add new peer %s, error %s", peer_id, error) + return self.peers[peer_id] = stream diff --git a/libp2p/transport/upgrader.py b/libp2p/transport/upgrader.py index 46656aa..96234c6 100644 --- a/libp2p/transport/upgrader.py +++ b/libp2p/transport/upgrader.py @@ -10,10 +10,7 @@ from libp2p.security.secure_transport_interface import ISecureTransport from libp2p.security.security_multistream import SecurityMultistream from libp2p.stream_muxer.abc import IMuxedConn from libp2p.stream_muxer.muxer_multistream import MuxerClassType, MuxerMultistream -from libp2p.transport.exceptions import ( - MuxerUpgradeFailure, - SecurityUpgradeFailure, -) +from libp2p.transport.exceptions import MuxerUpgradeFailure, SecurityUpgradeFailure from libp2p.typing import TProtocol from .listener_interface import IListener