diff --git a/libp2p/protocols/secure/secure.nim b/libp2p/protocols/secure/secure.nim index 0fe40ebe6..3809ae089 100644 --- a/libp2p/protocols/secure/secure.nim +++ b/libp2p/protocols/secure/secure.nim @@ -78,8 +78,13 @@ proc handleConn*(s: Secure, proc cleanup() {.async.} = try: - await conn.join() or sconn.join() - await allFuturesThrowing(sconn.close(), conn.close()) + let futs = @[conn.join(), sconn.join()] + await futs[0] or futs[1] + for f in futs: + if not f.finished: f.cancel # cancel outstanding join() + + await allFuturesThrowing( + sconn.close(), conn.close()) except CancelledError: # This is top-level procedure which will work as separate task, so it # do not need to propagate CancelledError. diff --git a/libp2p/transports/tcptransport.nim b/libp2p/transports/tcptransport.nim index 43f1e5c19..655246431 100644 --- a/libp2p/transports/tcptransport.nim +++ b/libp2p/transports/tcptransport.nim @@ -73,7 +73,11 @@ proc connHandler*(t: TcpTransport, proc onClose() {.async.} = try: - await client.join() or conn.join() + let futs = @[client.join(), conn.join()] + await futs[0] or futs[1] + for f in futs: + if not f.finished: f.cancel # cancel outstanding join() + trace "Cleaning up client", addrs = $client.remoteAddress, conn