From ba451196e8103d1cb394c2969370115d70c3dfce Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Fri, 6 Jan 2023 15:18:16 +0100 Subject: [PATCH] Avoid closing connection on channel EOF (#816) --- libp2p/muxers/mplex/lpchannel.nim | 6 +++++- libp2p/muxers/mplex/mplex.nim | 10 ++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/libp2p/muxers/mplex/lpchannel.nim b/libp2p/muxers/mplex/lpchannel.nim index 5f6faa809..599056ae1 100644 --- a/libp2p/muxers/mplex/lpchannel.nim +++ b/libp2p/muxers/mplex/lpchannel.nim @@ -244,7 +244,11 @@ proc completeWrite( except CancelledError as exc: # Chronos may still send the data raise exc - except LPStreamClosedError as exc: + except LPStreamConnDownError as exc: + await s.reset() + await s.conn.close() + raise exc + except LPStreamEOFError as exc: raise exc except CatchableError as exc: trace "exception in lpchannel write handler", s, msg = exc.msg diff --git a/libp2p/muxers/mplex/mplex.nim b/libp2p/muxers/mplex/mplex.nim index fc0294c2d..42e2a5de0 100644 --- a/libp2p/muxers/mplex/mplex.nim +++ b/libp2p/muxers/mplex/mplex.nim @@ -177,8 +177,14 @@ method handle*(m: Mplex) {.async, gcsafe.} = raise newLPStreamLimitError() trace "pushing data to channel", m, channel, len = data.len - await channel.pushData(data) - trace "pushed data to channel", m, channel, len = data.len + try: + await channel.pushData(data) + trace "pushed data to channel", m, channel, len = data.len + except LPStreamClosedError as exc: + # Channel is being closed, but `cleanupChann` was not yet triggered. + trace "pushing data to channel failed", m, channel, len = data.len, + msg = exc.msg + discard # Ignore message, same as if `cleanupChann` had completed. of MessageType.CloseIn, MessageType.CloseOut: await channel.pushEof()