From 5beb0235bf3ba388fd2ceef8718fdcffa4b26f8b Mon Sep 17 00:00:00 2001 From: lukechampine Date: Wed, 10 Jan 2024 18:47:41 -0500 Subject: [PATCH] syncer: Wait for peer goroutines before returning from (Syncer).Run --- syncer/syncer.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/syncer/syncer.go b/syncer/syncer.go index d3b3054..ed378c1 100644 --- a/syncer/syncer.go +++ b/syncer/syncer.go @@ -773,13 +773,23 @@ func (s *Syncer) Run() error { s.l.Close() s.mu.Lock() s.l = nil - for addr, p := range s.peers { + for _, p := range s.peers { p.Close() - delete(s.peers, addr) } s.mu.Unlock() <-errChan <-errChan + + // wait for all peer goroutines to exit + // TODO: a cond would be nicer than polling here + s.mu.Lock() + for len(s.peers) != 0 { + s.mu.Unlock() + time.Sleep(100 * time.Millisecond) + s.mu.Lock() + } + s.mu.Unlock() + if errors.Is(err, net.ErrClosed) { return nil // graceful shutdown }