fix(cache): prefer positive cache over SERVFAIL in ncache (#8003)

When serve_stale is enabled, a cached SERVFAIL in ncache shadows a valid
positive entry in pcache because ncache is always checked first. SERVFAIL
is transient and should not mask a known-good answer.

When the ncache hit is a SERVFAIL, check pcache for a valid entry before
returning the SERVFAIL. NXDOMAIN and NODATA are unaffected and still
follow the existing ncache-first lookup per RFC 2308.

Fixes #7956

Signed-off-by: umut-polat <52835619+umut-polat@users.noreply.github.com>
This commit is contained in:
Umut Polat
2026-05-20 06:28:35 +03:00
committed by GitHub
parent 35391dd8a9
commit b1a7fc8db1
2 changed files with 50 additions and 0 deletions

View File

@@ -192,6 +192,17 @@ func (c *Cache) getIfNotStale(now time.Time, state request.Request, server strin
if i, ok := c.ncache.Get(k); ok {
ttl := i.ttl(now)
if i.matches(state) && (ttl > 0 || (c.staleUpTo > 0 && -ttl < int(c.staleUpTo.Seconds()))) {
// SERVFAIL is transient; prefer a valid positive cache entry if one
// exists, so a cached SERVFAIL does not shadow a previously good answer.
if i.Rcode == dns.RcodeServerFailure {
if p, pok := c.pcache.Get(k); pok {
pttl := p.ttl(now)
if p.matches(state) && (pttl > 0 || (c.staleUpTo > 0 && -pttl < int(c.staleUpTo.Seconds()))) {
cacheHits.WithLabelValues(server, Success, c.zonesMetricLabel, c.viewMetricLabel).Inc()
return p
}
}
}
cacheHits.WithLabelValues(server, Denial, c.zonesMetricLabel, c.viewMetricLabel).Inc()
return i
}