From 8de4eb7ba188520c36899b0a5b00cfdbbbecbb1b Mon Sep 17 00:00:00 2001 From: Robert Obryk Date: Mon, 23 Nov 2020 09:34:04 +0100 Subject: [PATCH] internal/dns: prevent upstreams from being lost during reordering (#63) If upstreams were reordered between start of an upstream request and its conclusion, the move-to-front operation would likely incorrectly reorder upstreams: duplicate one and remove another. Instead, we abandon the move-to-front operation if that was about to happen. --- internal/dns/dns.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/dns/dns.go b/internal/dns/dns.go index dc7d83d..4b8b398 100644 --- a/internal/dns/dns.go +++ b/internal/dns/dns.go @@ -428,7 +428,10 @@ func (s *Server) handleRequest(w dns.ResponseWriter, r *dns.Msg) { if idx > 0 { // re-order this upstream to the front of s.upstream. s.upstreamMu.Lock() - s.upstream = append(append([]string{u}, s.upstream[:idx]...), s.upstream[idx+1:]...) + // if the upstreams were reordered in the meantime leave them alone + if s.upstream[idx] == u { + s.upstream = append(append([]string{u}, s.upstream[:idx]...), s.upstream[idx+1:]...) + } s.upstreamMu.Unlock() } return