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 }