Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition with host in relay #1273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions waku/v2/protocol/relay/waku_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type WakuRelay struct {

minPeersToPublish int

hostMutex sync.Mutex
topicValidatorMutex sync.RWMutex
topicValidators map[string][]validatorFn
defaultTopicValidators []validatorFn
Expand Down Expand Up @@ -103,25 +104,31 @@ func NewWakuRelay(bcaster Broadcaster, minPeersToPublish int, timesource timesou
}

func (w *WakuRelay) peerScoreInspector(peerScoresSnapshots map[peer.ID]*pubsub.PeerScoreSnapshot) {
if w.host == nil {
w.hostMutex.Lock()
host := w.host
w.hostMutex.Unlock()

if host == nil {
return
}

for pid, snap := range peerScoresSnapshots {
if snap.Score < w.peerScoreThresholds.GraylistThreshold {
// Disconnect bad peers
err := w.host.Network().ClosePeer(pid)
err := host.Network().ClosePeer(pid)
if err != nil {
w.log.Error("could not disconnect peer", logging.HostID("peer", pid), zap.Error(err))
}
}
_ = w.host.Peerstore().(wps.WakuPeerstore).SetScore(pid, snap.Score)
_ = host.Peerstore().(wps.WakuPeerstore).SetScore(pid, snap.Score)
}
}

// SetHost sets the host to be able to mount or consume a protocol
func (w *WakuRelay) SetHost(h host.Host) {
w.hostMutex.Lock()
w.host = h
w.hostMutex.Unlock()
}

// Start initiates the WakuRelay protocol
Expand Down Expand Up @@ -155,8 +162,8 @@ func (w *WakuRelay) PubSub() *pubsub.PubSub {

// Topics returns a list of all the pubsub topics currently subscribed to
func (w *WakuRelay) Topics() []string {
defer w.topicsMutex.RUnlock()
w.topicsMutex.RLock()
defer w.topicsMutex.RUnlock()

var result []string
for topic := range w.topics {
Expand Down
Loading