diff --git a/libp2p/muxers/mplex/mplex.nim b/libp2p/muxers/mplex/mplex.nim index 5b0a092..489f41b 100644 --- a/libp2p/muxers/mplex/mplex.nim +++ b/libp2p/muxers/mplex/mplex.nim @@ -49,7 +49,10 @@ proc newStreamInternal*(m: Mplex, lazy: bool = false): Future[LPChannel] {.async, gcsafe.} = ## create new channel/stream - let id = if initiator: m.currentId.inc(); m.currentId else: chanId + let id = if initiator: + m.currentId.inc(); m.currentId + else: chanId + trace "creating new channel", channelId = id, initiator = initiator, name = name, @@ -100,12 +103,7 @@ method handle*(m: Mplex) {.async, gcsafe.} = var fut = newFuture[void]() proc handler() {.async.} = try: - try: - await m.streamHandler(stream) - trace "streamhandler ended", oid = stream.oid - finally: - if not(stream.closed): - await stream.close() + await m.streamHandler(stream) except CatchableError as exc: trace "exception in stream handler", exc = exc.msg finally: diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 88e54d5..f11fb33 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -425,8 +425,15 @@ proc newSwitch*(peerInfo: PeerInfo, let s = result # can't capture result result.streamHandler = proc(stream: Connection) {.async, gcsafe.} = - trace "handling connection for", peerInfo = $stream.peerInfo - await s.ms.handle(stream) # handle incoming connection + try: + trace "handling connection for", peerInfo = $stream.peerInfo + try: + await s.ms.handle(stream) # handle incoming connection + finally: + if not(stream.closed): + await stream.close() + except CatchableError as exc: + trace "excepton in stream handler", exc = exc.msg result.mount(identity) for key, val in muxers: