fix: data race in filter unsubscribe

This commit is contained in:
Prem Chaitanya Prathi 2023-08-05 08:03:19 +05:30
parent 22398b2868
commit 20040f2e9b
No known key found for this signature in database
1 changed files with 8 additions and 4 deletions

View File

@ -407,11 +407,14 @@ func (wf *WakuFilterLightnode) UnsubscribeAll(ctx context.Context, opts ...Filte
localWg := sync.WaitGroup{} localWg := sync.WaitGroup{}
resultChan := make(chan WakuFilterPushResult, len(wf.subscriptions.items)) resultChan := make(chan WakuFilterPushResult, len(wf.subscriptions.items))
var peersUnsubscribed []peer.ID
for peerID := range wf.subscriptions.items { for peerID := range wf.subscriptions.items {
if params.selectedPeer != "" && peerID != params.selectedPeer { if params.selectedPeer != "" && peerID != params.selectedPeer {
continue continue
} }
peersUnsubscribed = append(peersUnsubscribed, peerID)
localWg.Add(1) localWg.Add(1)
go func(peerID peer.ID) { go func(peerID peer.ID) {
defer localWg.Done() defer localWg.Done()
@ -423,9 +426,6 @@ func (wf *WakuFilterLightnode) UnsubscribeAll(ctx context.Context, opts ...Filte
if err != nil { if err != nil {
wf.log.Error("could not unsubscribe from peer", logging.HostID("peerID", peerID), zap.Error(err)) wf.log.Error("could not unsubscribe from peer", logging.HostID("peerID", peerID), zap.Error(err))
} }
delete(wf.subscriptions.items, peerID)
resultChan <- WakuFilterPushResult{ resultChan <- WakuFilterPushResult{
Err: err, Err: err,
PeerID: peerID, PeerID: peerID,
@ -435,6 +435,10 @@ func (wf *WakuFilterLightnode) UnsubscribeAll(ctx context.Context, opts ...Filte
localWg.Wait() localWg.Wait()
close(resultChan) close(resultChan)
for _, peerID := range peersUnsubscribed {
if len(wf.subscriptions.items[peerID].subscriptionsPerTopic) == 0 {
delete(wf.subscriptions.items, peerID)
}
}
return resultChan, nil return resultChan, nil
} }