From a65b7b028f64c3531eee97f9b327cbd067249888 Mon Sep 17 00:00:00 2001 From: Tanguy Date: Wed, 14 Jun 2023 17:23:39 +0200 Subject: [PATCH] GossipSub: remove peer if we can't establish sendConn (#886) --- libp2p/protocols/pubsub/pubsubpeer.nim | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index 6241a30b9..e1fc989bb 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -171,7 +171,7 @@ proc connectOnce(p: PubSubPeer): Future[void] {.async.} = try: if p.connectedFut.finished: p.connectedFut = newFuture[void]() - let newConn = await p.getConn() + let newConn = await p.getConn().wait(5.seconds) if newConn.isNil: raise (ref LPError)(msg: "Cannot establish send connection") @@ -198,6 +198,9 @@ proc connectOnce(p: PubSubPeer): Future[void] {.async.} = await p.sendConn.close() p.sendConn = nil + if not p.connectedFut.finished: + p.connectedFut.complete() + try: if p.onEvent != nil: p.onEvent(p, PubSubPeerEvent(kind: PubSubPeerEventKind.Disconnected)) @@ -246,11 +249,13 @@ proc sendEncoded*(p: PubSubPeer, msg: seq[byte]) {.raises: [], async.} = return if p.sendConn == nil: - discard await p.connectedFut.withTimeout(1.seconds) + # Wait for a send conn to be setup. `connectOnce` will + # complete this even if the sendConn setup failed + await p.connectedFut var conn = p.sendConn if conn == nil or conn.closed(): - debug "No send connection, skipping message", p, msg = shortLog(msg) + debug "No send connection", p, msg = shortLog(msg) return trace "sending encoded msgs to peer", conn, encoded = shortLog(msg)