diff --git a/net/dns/resolver/forwarder.go b/net/dns/resolver/forwarder.go index 1f204143e..83a87ccba 100644 --- a/net/dns/resolver/forwarder.go +++ b/net/dns/resolver/forwarder.go @@ -900,10 +900,13 @@ func (f *forwarder) sendUDP(ctx context.Context, fq *forwardQuery, rr resolverAn defer fq.closeOnCtxDone.Remove(conn) if _, err := conn.WriteToUDPAddrPort(fq.packet, ipp); err != nil { - metricDNSFwdUDPErrorWrite.Add(1) if err := ctx.Err(); err != nil { + // The query is already done (e.g. another upstream answered + // first) and its cleanup closed conn, so this isn't an + // upstream write failure. Don't count it as one. return nil, err } + metricDNSFwdUDPErrorWrite.Add(1) return nil, err } metricDNSFwdUDPWrote.Add(1) diff --git a/net/dns/resolver/forwarder_test.go b/net/dns/resolver/forwarder_test.go index 03ea841da..cf4995362 100644 --- a/net/dns/resolver/forwarder_test.go +++ b/net/dns/resolver/forwarder_test.go @@ -350,6 +350,9 @@ type testDNSServerOptions struct { // nothing listens, so the connect fails fast instead. HangTCP bool + // HangUDP reads UDP queries and never answers them. + HangUDP bool + // ResponseDelay is how long the server waits before answering, on either // transport. ResponseDelay time.Duration @@ -472,6 +475,9 @@ func runDNSServer(tb testing.TB, opts *testDNSServerOptions, response []byte, on handleUDP := func(addr netip.AddrPort, req []byte) { onRequest(false, req) + if opts != nil && opts.HangUDP { + return + } if opts != nil && opts.ResponseDelay > 0 { time.Sleep(opts.ResponseDelay) } @@ -1369,11 +1375,27 @@ func TestForwarderRaceLoserNoReadError(t *testing.T) { request := makeTestRequest(t, domain, dns.TypeA, 0) answer := makeTestResponse(t, domain, dns.RCodeSuccess, netip.MustParseAddr("127.0.0.1")) - answering := runDNSServer(t, nil, answer, func(isTCP bool, gotRequest []byte) {}) + // The answering resolver holds its UDP answer until the silent one has + // the query, so the losing sendUDP is past its write when the query ends. + silentGotUDP := make(chan struct{}, 1) + answering := runDNSServer(t, nil, answer, func(isTCP bool, gotRequest []byte) { + if isTCP { + return + } + select { + case <-silentGotUDP: + case <-time.After(5 * time.Second): + t.Errorf("silent resolver never got the UDP query") + } + }) // Bound on both transports but answering on neither, so its read is still // blocked when the other resolver's answer ends the query. - silent := runDNSServer(t, &testDNSServerOptions{SkipUDP: true, HangTCP: true}, - answer, func(isTCP bool, gotRequest []byte) {}) + silent := runDNSServer(t, &testDNSServerOptions{HangUDP: true, HangTCP: true}, + answer, func(isTCP bool, gotRequest []byte) { + if !isTCP { + silentGotUDP <- struct{}{} + } + }) const queries = 20 beforeRead := metricDNSFwdUDPErrorRead.Value()