From 7e613f831b5326ef9b00be034764238948e67f7e Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Thu, 22 Feb 2024 14:05:23 +0100 Subject: [PATCH] "Stop" should wait until stopped in DNS monitor. --- dns_monitor.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dns_monitor.go b/dns_monitor.go index c1cbdcd..c0ec361 100644 --- a/dns_monitor.go +++ b/dns_monitor.go @@ -145,6 +145,7 @@ type DnsMonitor struct { stopCtx context.Context stopFunc func() + stopped chan struct{} mu sync.RWMutex cond *sync.Cond @@ -167,6 +168,7 @@ func NewDnsMonitor(interval time.Duration) (*DnsMonitor, error) { stopCtx: stopCtx, stopFunc: stopFunc, + stopped: make(chan struct{}), hostnames: make(map[string]*dnsMonitorEntry), } @@ -183,6 +185,7 @@ func (m *DnsMonitor) Start() error { func (m *DnsMonitor) Stop() { m.stopFunc() m.cond.Signal() + <-m.stopped } func (m *DnsMonitor) Add(target string, callback DnsMonitorCallback) (*DnsMonitorEntry, error) { @@ -296,6 +299,7 @@ func (m *DnsMonitor) waitForEntries() (waited bool) { func (m *DnsMonitor) run() { ticker := time.NewTicker(m.interval) defer ticker.Stop() + defer close(m.stopped) for { if m.waitForEntries() {