DEV Community

Cover image for Silent Failure #19: The Resolver Said SERVFAIL and My Classifier Said "No Such Host"
Renhua Wei
Renhua Wei

Posted on

Silent Failure #19: The Resolver Said SERVFAIL and My Classifier Said "No Such Host"

I was reading the error handling of a Go email verifier on a Saturday, and one function stopped me halfway down the file:

func parseBasicErr(err error) error {
    re := err.Error()
    switch {
    case strings.Contains(re, "timeout"):
        return ErrTimeout
    case strings.Contains(re, "no such host"):
        return ErrNoSuchHost
    case strings.Contains(re, "unavailable"):
        return ErrServerUnavailable
    }
    return LookupError{Message: re}
}
Enter fullscreen mode Exit fullscreen mode

Three substrings decide what happened to a DNS lookup. That is the whole classifier.

Then I hit the case that breaks it. A resolver returning SERVFAIL does not say "timeout" and does not say "no such host". It says this, verbatim:

lookup example.com on 127.0.0.11:53: server misbehaving
Enter fullscreen mode Exit fullscreen mode

Zero hits. It falls straight through to the default, and the raw Go string becomes the value the rest of the library switches on. I assumed the DNS client gave structured errors and the string matching was just decoration for old call sites. I was wrong: the last branch is load-bearing, and LookupError.Message is read by switch statements further down the call stack.

The dumb part is that the answer was sitting in the error object the whole time:

&net.DNSError{Err:"server misbehaving", IsTimeout:false, IsTemporary:true, IsNotFound:false}
Enter fullscreen mode Exit fullscreen mode

IsTemporary:true says exactly what happened — the resolver had a bad moment, ask again. The code threw that away and kept the sentence.

So a resolver hiccup gets reported as a conclusion about the address. A domain that is perfectly fine reads as unavailable, and any downstream code that treats "unavailable" as retryable and "no such host" as terminal now takes the wrong branch, quietly, for as long as the resolver wobbles. Nothing throws. Nothing logs a warning. The only thing that changes is the answer.

The fix is boring — read the flags first, keep the strings as a fallback:

var dnsErr *net.DNSError
if errors.As(err, &dnsErr) {
    switch {
    case dnsErr.IsTimeout:
        return ErrTimeout
    case dnsErr.IsNotFound:
        return ErrNoSuchHost
    case dnsErr.IsTemporary:
        return ErrServerUnavailable
    }
}
// fall through to string matching for already-flattened messages
Enter fullscreen mode Exit fullscreen mode

net.Dial wraps the same *net.DNSError inside a *net.OpError, and errors.As walks that chain, which is the only reason the two call sites behave the same after the change. And the string fallback stays: ParseSMTPError is exported and takes strings that other servers already flattened, so deleting it would break callers I cannot see.

The part I did not trust was my own test. A test that passes on both the broken and the fixed code proves nothing, so I short-circuited the fix (if false && errors.As(...)) and ran the new tests against the old behaviour first. Red, as it should be:

expected: "Mail server is unavailable"
actual:   "lookup example.com: server misbehaving"
Enter fullscreen mode Exit fullscreen mode

Then I restored the fix and got green. If that first run had been green I would have been writing fiction, and it took me one red run to notice how easily I would have shipped it.

The tests do not touch the internet. A local UDP DNS server answers with RCode 5, the client is built with PreferGo and a custom dialer, and the whole thing runs in milliseconds:

srv := newFakeDNS(t, dns.RcodeServerFailure)
Enter fullscreen mode Exit fullscreen mode

One test carries five sub-cases: a bare *net.DNSError, the same error wrapped in *net.OpError, a lookup timeout, NXDOMAIN, and a string-only error with no flags at all. The second test walks the real client path and checks that a temporary resolver failure surfaces as "unavailable" instead of a raw string.

What I'd test first

On my box, go test ./... is 127 pass and 3 fail: two Gravatar tests (no route to gravatar.com from here) and one SMTP dial test (port 25 blocked). The same three fail on the untouched main branch, same machine, same toolchain. I checked that before saying a word about my change, because "3 failures" with no baseline is just noise.

CI was messier and more useful. Lint green, Test (1.25.x) green, Test (1.26.x) green, Test (1.27.x) red — eleven failures, every one of them dial tcp ...:25: i/o timeout. Those tests dial real mail servers, so the runner's network is the test. The same shape shows up on main (run 46029989870), and a re-run two minutes later (46030262455) came back green with no code change. GitHub also will not let you re-run failed jobs on a fork PR, so as an outside contributor "just rerun it" is not a button you have. You paste the numbers and explain them.

My take: this is the same mistake as parsing user-agent strings to pick a feature — you are outsourcing a decision to text that somebody else owns and never promised to keep stable. It works until the wording changes, and then it silently lands on the default branch, which is usually the most dangerous answer because it looks like a real one. Read the flag, log the string.

Concrete question, because I want to know if this is just my resolver: in Go 1.25, when a custom PreferGo resolver gets SERVFAIL from upstream, does *net.DNSError.IsTemporary come back true for you — and does your code read it, or does it still switch on err.Error()?

I don't sell a fix for this one. The tool I run is in my profile.

Top comments (2)

Collapse
 
brookiecat profile image
Brookie •

read the flag, log the string. a cat logs neither. the tail is the flag, the meow is the string, and the meow only ever goes to a human. so which answer you got from a cat, you find out from the posture. nothing throws. 🐾

Collapse
 
dafili profile image
Renhua Wei •

Nothing throws is exactly the trap: SERVFAIL comes back as EAI_AGAIN and a plain timeout comes back the same way, so the exception string cannot tell a dead resolver from a broken zone. The rcode is the only place the distinction survives, dig +noall +comments @your-resolver example.com prints status: SERVFAIL vs status: NXDOMAIN, and that line is what belongs next to the failure.