From 18d69a5c41c03f493a9f80073ea304b9693350ac Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com> Date: Wed, 16 Dec 2020 03:32:11 +0900 Subject: [PATCH] Workaround the gossipsub table race condition (#486) --- libp2p/protocols/pubsub/floodsub.nim | 12 ++++++++++++ libp2p/protocols/pubsub/gossipsub.nim | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/libp2p/protocols/pubsub/floodsub.nim b/libp2p/protocols/pubsub/floodsub.nim index 0bf053da6..446e6c8d5 100644 --- a/libp2p/protocols/pubsub/floodsub.nim +++ b/libp2p/protocols/pubsub/floodsub.nim @@ -33,6 +33,18 @@ method subscribeTopic*(f: FloodSub, topic: string, subscribe: bool, peer: PubsubPeer) {.gcsafe.} = + logScope: + peer + topic + + # this is a workaround for a race condition + # that can happen if we disconnect the peer very early + # in the future we might use this as a test case + # and eventually remove this workaround + if subscribe and peer.peerId notin f.peers: + trace "ignoring unknown peer" + return + procCall PubSub(f).subscribeTopic(topic, subscribe, peer) if topic notin f.floodsub: diff --git a/libp2p/protocols/pubsub/gossipsub.nim b/libp2p/protocols/pubsub/gossipsub.nim index 349088121..529005c79 100644 --- a/libp2p/protocols/pubsub/gossipsub.nim +++ b/libp2p/protocols/pubsub/gossipsub.nim @@ -909,17 +909,25 @@ method subscribeTopic*(g: GossipSub, topic: string, subscribe: bool, peer: PubSubPeer) {.gcsafe.} = - # Skip floodsub - we don't want it to add the peer to `g.floodsub` - procCall PubSub(g).subscribeTopic(topic, subscribe, peer) - logScope: peer topic - g.onNewPeer(peer) + # this is a workaround for a race condition + # that can happen if we disconnect the peer very early + # in the future we might use this as a test case + # and eventually remove this workaround + if subscribe and peer.peerId notin g.peers: + trace "ignoring unknown peer" + return + + # Skip floodsub - we don't want it to add the peer to `g.floodsub` + procCall PubSub(g).subscribeTopic(topic, subscribe, peer) if subscribe: trace "peer subscribed to topic" + # populate scoring structs and such + g.onNewPeer(peer) # subscribe remote peer to the topic discard g.gossipsub.addPeer(topic, peer) if peer.peerId in g.parameters.directPeers: