diff --git a/discovery/consul/consul.go b/discovery/consul/consul.go index ccc09b072..f0db8d762 100644 --- a/discovery/consul/consul.go +++ b/discovery/consul/consul.go @@ -357,6 +357,13 @@ func (d *Discovery) watchServices(ctx context.Context, ch chan<- []*targetgroup. elapsed := time.Since(t0) rpcDuration.WithLabelValues("catalog", "services").Observe(elapsed.Seconds()) + // Check the context before in order to exit early. + select { + case <-ctx.Done(): + return + default: + } + if err != nil { level.Error(d.logger).Log("msg", "Error refreshing service list", "err", err) rpcFailuresCount.Inc() diff --git a/discovery/dns/dns.go b/discovery/dns/dns.go index 8710bf327..014d5239e 100644 --- a/discovery/dns/dns.go +++ b/discovery/dns/dns.go @@ -151,7 +151,7 @@ func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) { wg.Add(len(d.names)) for _, name := range d.names { go func(n string) { - if err := d.refreshOne(ctx, n, ch); err != nil { + if err := d.refreshOne(ctx, n, ch); err != nil && err != context.Canceled { level.Error(d.logger).Log("msg", "Error refreshing DNS targets", "err", err) } wg.Done()