From 20040f2e9b566b48cd568b0849f58d106a360315 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Sat, 5 Aug 2023 08:03:19 +0530 Subject: [PATCH] fix: data race in filter unsubscribe --- waku/v2/protocol/filter/client.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/waku/v2/protocol/filter/client.go b/waku/v2/protocol/filter/client.go index 39b8d65a..ca7977b5 100644 --- a/waku/v2/protocol/filter/client.go +++ b/waku/v2/protocol/filter/client.go @@ -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 }