From c0ab6095599c7a299070b623af645b44ce50eb02 Mon Sep 17 00:00:00 2001 From: mhchia Date: Tue, 4 Feb 2020 21:57:59 +0800 Subject: [PATCH] Mplex: catch `RawConnError` when writing Also, do nothing in `MplexStream.reset` if `MuxedConnUnavailable` is raised when sending the message. --- libp2p/stream_muxer/mplex/mplex.py | 8 +++++++- libp2p/stream_muxer/mplex/mplex_stream.py | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libp2p/stream_muxer/mplex/mplex.py b/libp2p/stream_muxer/mplex/mplex.py index 6f5f3fd..70f26b3 100644 --- a/libp2p/stream_muxer/mplex/mplex.py +++ b/libp2p/stream_muxer/mplex/mplex.py @@ -166,7 +166,13 @@ class Mplex(IMuxedConn): :param _bytes: byte array to write :return: length written """ - await self.secured_conn.write(_bytes) + try: + await self.secured_conn.write(_bytes) + except RawConnError as e: + raise MplexUnavailable( + "failed to write message to the underlying connection" + ) from e + return len(_bytes) async def handle_incoming(self) -> None: diff --git a/libp2p/stream_muxer/mplex/mplex_stream.py b/libp2p/stream_muxer/mplex/mplex_stream.py index 933de20..7967574 100644 --- a/libp2p/stream_muxer/mplex/mplex_stream.py +++ b/libp2p/stream_muxer/mplex/mplex_stream.py @@ -3,6 +3,7 @@ from typing import TYPE_CHECKING import trio from libp2p.stream_muxer.abc import IMuxedStream +from libp2p.stream_muxer.exceptions import MuxedConnUnavailable from .constants import HeaderTags from .datastructures import StreamID @@ -189,7 +190,11 @@ class MplexStream(IMuxedStream): if self.is_initiator else HeaderTags.ResetReceiver ) - await self.muxed_conn.send_message(flag, None, self.stream_id) + # Try to send reset message to the other side. Ignore if there is anything wrong. + try: + await self.muxed_conn.send_message(flag, None, self.stream_id) + except MuxedConnUnavailable: + pass self.event_local_closed.set() self.event_remote_closed.set()