Skip to content

Commit

Permalink
GossipSub: remove peer if we can't establish sendConn (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
Menduist authored and diegomrsantos committed Jun 23, 2023
1 parent 936a1cc commit 643bc60
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions libp2p/protocols/pubsub/pubsubpeer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 643bc60

Please sign in to comment.