From ca7616ca129bc5be7e1c8dc45f116c6ef36c3521 Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Sat, 5 Dec 2026 15:14:00 -0800 Subject: [PATCH] net/dns/resolver: make the TCP query timeout a forwarder field TestForwarderRcodeWithHungTCP replaced the tcpQueryTimeout global with tstest.Replace. A "tcp" query races the UDP and TCP arms from the start, and race.Start returns on the UDP answer without waiting for the TCP arm, so that goroutine could still be reading the global in sendTCP when the test's cleanup restored it, which the race detector flagged. Store the timeout on the forwarder instead, so that tests set it on their own forwarder before using it. Fixes #22943 Signed-off-by: Nick Khyl --- net/dns/resolver/forwarder.go | 29 ++++++++++++++++------------- net/dns/resolver/forwarder_test.go | 11 ++++++----- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/net/dns/resolver/forwarder.go b/net/dns/resolver/forwarder.go index 83a87ccba..23d587054 100644 --- a/net/dns/resolver/forwarder.go +++ b/net/dns/resolver/forwarder.go @@ -108,11 +108,9 @@ const ( udpRaceTimeout = 2 * time.Second ) -// tcpQueryTimeout is the timeout for a DNS query performed over TCP. +// defaultTCPQueryTimeout is the default timeout for a DNS query performed over TCP. // It matches the default 5sec timeout of the 'dig' utility. -// -// It's a var only so tests can shorten it. -var tcpQueryTimeout = 5 * time.Second +const defaultTCPQueryTimeout = 5 * time.Second // rcodeHoldGrace is how long forwardWithDestChan holds an upstream REFUSED or // SERVFAIL after every resolver has started, in case a healthier one answers. @@ -317,6 +315,10 @@ type forwarder struct { health *health.Tracker // always non-nil verboseFwd bool // if true, log all DNS forwarding + // tcpQueryTimeout is the timeout for a DNS query performed over TCP. + // It defaults to [defaultTCPQueryTimeout], but can be overridden by tests. + tcpQueryTimeout time.Duration + controlKnobs *controlknobs.Knobs // or nil ctx context.Context // good until Close @@ -373,13 +375,14 @@ func newForwarder(logf logger.Logf, netMon *netmon.Monitor, linkSel ForwardLinkS panic("nil netMon") } f := &forwarder{ - logf: logger.WithPrefix(logf, "forward: "), - netMon: netMon, - linkSel: linkSel, - dialer: dialer, - health: health, - controlKnobs: knobs, - verboseFwd: verboseDNSForward(), + logf: logger.WithPrefix(logf, "forward: "), + netMon: netMon, + linkSel: linkSel, + dialer: dialer, + health: health, + controlKnobs: knobs, + verboseFwd: verboseDNSForward(), + tcpQueryTimeout: defaultTCPQueryTimeout, } f.ctx, f.ctxCancel = context.WithCancel(context.Background()) return f @@ -1103,7 +1106,7 @@ func (f *forwarder) sendTCP(ctx context.Context, fq *forwardQuery, rr resolverAn tcpFam = "tcp6" } - ctx, cancel := context.WithTimeout(ctx, tcpQueryTimeout) + ctx, cancel := context.WithTimeout(ctx, f.tcpQueryTimeout) defer cancel() conn, err := f.getDialerType()(ctx, tcpFam, ipp.String()) @@ -1114,7 +1117,7 @@ func (f *forwarder) sendTCP(ctx context.Context, fq *forwardQuery, rr resolverAn // Canceling ctx can't interrupt reads blocked in the runtime, so the // deadline has to go on the conn. ctx already carries - // min(caller's deadline, tcpQueryTimeout). + // min(caller's deadline, f.tcpQueryTimeout). if deadline, ok := ctx.Deadline(); ok { if err := conn.SetDeadline(deadline); err != nil { // Best effort. The caller's ctx still bounds the read. diff --git a/net/dns/resolver/forwarder_test.go b/net/dns/resolver/forwarder_test.go index cf4995362..e6f021f0a 100644 --- a/net/dns/resolver/forwarder_test.go +++ b/net/dns/resolver/forwarder_test.go @@ -1439,14 +1439,17 @@ func TestForwarderRcodeWithHungTCP(t *testing.T) { // Longer than the query helper's context, so a regression that waits on // the hung TCP arm fails as a context error. - tstest.Replace(t, &tcpQueryTimeout, 10*time.Second) + modify := func(f *forwarder) { + beVerbose(f) + f.tcpQueryTimeout = 10 * time.Second + } for _, family := range []string{"udp", "tcp"} { t.Run(family, func(t *testing.T) { // sendTCP bumps this before it dials. beforeTCP := metricDNSFwdTCP.Value() beforeCtx := metricDNSFwdErrorContext.Value() - resp, err := runTestQueryWithFamily(t, request, family, beVerbose, port) + resp, err := runTestQueryWithFamily(t, request, family, modify, port) if err != nil { t.Fatalf("runTestQueryWithFamily: %v", err) } @@ -1652,9 +1655,7 @@ func TestSendTCPReadTimeout(t *testing.T) { makeTestResponse(t, domain, dns.RCodeSuccess), func(isTCP bool, gotRequest []byte) {}) const shortTimeout = 250 * time.Millisecond - tstest.Replace(t, &tcpQueryTimeout, shortTimeout) - - fwd := newTestForwarder(t, nil) + fwd := newTestForwarder(t, func(f *forwarder) { f.tcpQueryTimeout = shortTimeout }) fq := &forwardQuery{ txid: getTxID(request),