From 6c721f7ffd68c65c03d8c981d8c8c28a2f36f2e7 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 23 Nov 2021 11:01:36 -0400 Subject: [PATCH] fix: wg --- waku/v2/discv5/discover.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/waku/v2/discv5/discover.go b/waku/v2/discv5/discover.go index af96b557..d4a7d28b 100644 --- a/waku/v2/discv5/discover.go +++ b/waku/v2/discv5/discover.go @@ -227,13 +227,15 @@ func (d *DiscoveryV5) Start() error { d.Lock() defer d.Unlock() + d.wg.Wait() // Waiting for other go routines to stop + + d.quit = make(chan struct{}, 1) + err := d.listen() if err != nil { return err } - d.quit = make(chan struct{}) - return nil } @@ -241,11 +243,11 @@ func (d *DiscoveryV5) Stop() { d.Lock() defer d.Unlock() + close(d.quit) + d.listener.Close() d.listener = nil - close(d.quit) - log.Info("Stopped Discovery V5") d.wg.Wait() @@ -361,6 +363,8 @@ func (c *DiscoveryV5) Advertise(ctx context.Context, ns string, opts ...discover } func (d *DiscoveryV5) iterate(ctx context.Context, iterator enode.Iterator, limit int, doneCh chan struct{}) { + defer d.wg.Done() + for { if len(d.peerCache.recs) >= limit { break @@ -393,7 +397,6 @@ func (d *DiscoveryV5) iterate(ctx context.Context, iterator enode.Iterator, limi } } - d.wg.Done() close(doneCh) }