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