watcher: a lookup cut short by shutdown is not logged as an error (closes #229)
check / check (push) Canceled after 0s

Stopping dnswatcher during a DNS check logged every lookup the stop cut
short as an error, "context canceled". The watcher now logs a failed
lookup only when its context was not cancelled; the port and TLS checks
already return quietly. It asks the context, not the lookup's error,
because the resolver reports a cancelled lookup with its own error,
which does not wrap context.Canceled. A context whose deadline passed is
not cancelled, so a lookup that ran out of time is still logged as an
error. The tests run a check on a cancelled context and on one whose
deadline passed; neither sends a query.

Model: opus-5-5
This commit is contained in:
2026-10-02 06:04:46 +00:00
parent a18803ff28
commit d20918179f
4 changed files with 111 additions and 20 deletions
+33 -20
View File
@@ -2,6 +2,7 @@ package watcher
import (
"context"
"errors"
"fmt"
"log/slog"
"slices"
@@ -217,11 +218,14 @@ func (w *Watcher) checkDomain(
) {
nameservers, err := w.resolver.LookupNS(ctx, domain)
if err != nil {
w.log.Error(
"failed to lookup NS",
"domain", domain,
"error", err,
)
// Shutdown cancels ctx; a lookup it cut short did not fail.
if !errors.Is(ctx.Err(), context.Canceled) {
w.log.Error(
"failed to lookup NS",
"domain", domain,
"error", err,
)
}
return
}
@@ -257,11 +261,14 @@ func (w *Watcher) checkDomain(
// the domain's IP addresses.
results, err := w.resolver.LookupAllRecords(ctx, domain)
if err != nil {
w.log.Error(
"failed to lookup records for domain",
"domain", domain,
"error", err,
)
// Shutdown cancels ctx; a lookup it cut short did not fail.
if !errors.Is(ctx.Err(), context.Canceled) {
w.log.Error(
"failed to lookup records for domain",
"domain", domain,
"error", err,
)
}
return
}
@@ -337,11 +344,14 @@ func (w *Watcher) resolveNameserverAddresses(
continue
}
w.log.Error(
"no addresses found for nameserver",
"nameserver", ns,
"error", err,
)
// Shutdown cancels ctx; a lookup it cut short did not fail.
if !errors.Is(ctx.Err(), context.Canceled) {
w.log.Error(
"no addresses found for nameserver",
"nameserver", ns,
"error", err,
)
}
if prevIPs, ok := prev[ns]; ok {
addresses[ns] = prevIPs
@@ -389,11 +399,14 @@ func (w *Watcher) checkHostname(
) {
results, err := w.resolver.LookupAllRecords(ctx, hostname)
if err != nil {
w.log.Error(
"failed to lookup records",
"hostname", hostname,
"error", err,
)
// Shutdown cancels ctx; a lookup it cut short did not fail.
if !errors.Is(ctx.Err(), context.Canceled) {
w.log.Error(
"failed to lookup records",
"hostname", hostname,
"error", err,
)
}
return
}