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 <nickk@tailscale.com>
This commit is contained in:
Nick Khyl committed 2026-12-05 15:14:00 -08:00
1 parent 717ac8d76e
commit ca7616ca12
2 files changed
+22 -18

No files matched your search

+16 -13
View File
@@ -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.
+6 -5
View File
@@ -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),