Skip to content

Commit 43a6627

Browse files
committed
Refuse resolver dials under a finished context (#295)
Go runs A and AAAA lookups in a goroutine that keeps retrying after the caller's timeout, under a cancelled context that has lost the caller's values. With pooled sockets such a retry reached the wire outside -rate and the query count. The Dial hook now returns the context's error before counting, pacing or dialing, for UDP, TCP, DoT and DoH alike. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Fixes #295
1 parent 274df44 commit 43a6627

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6262
- Shell completion for `-type` offers `NS`, `MX` and `TXT`, which `-type` has accepted since #194. The record types and output formats now come from one list each, which completion, the MCP schema and flag checking share (#264).
6363
- When `-max-queries` or `-max-duration` stops a scan, candidates `-exclude` would have kept out are counted as `excluded`, not skipped, so a budget that ran out only past excluded names leaves the verdict `complete` (and `-diff` reporting removals) instead of `degraded` (#238).
6464
- `-resume` no longer skips names when a passive source failed in the interrupted run and works on resume. The failed source was fetched again, its names were inserted before later sources' names, and the saved position then pointed past candidates that were never tested while the verdict could still read `complete`. The resume state now records a failed source with no names, and the resumed run replays that (#237).
65+
- An A or AAAA lookup that timed out no longer sends the same name again in the background. The repeats went out about five seconds apart on a reused socket, outside `-rate` and `-rate-per-resolver`, uncounted in the query total, and could continue after the scan had ended; a lookup whose time is up now gets no connection (#295).
6566

6667
### Security
6768
- `go.mod` names a patched toolchain (`toolchain go1.26.11`) beside the unchanged `go 1.26.0` floor, so `go install` and the action's `version: source` build with the security-fixed `net/http`, `crypto/tls` and `crypto/x509` instead of the original 1.26.0 standard library; with the default `GOTOOLCHAIN=auto` an older local Go fetches it. Release binaries were already built with a current Go (#243).

‎internal/dns/resolver.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ func newResolver(timeout time.Duration, dnsServer string, budget *idleBudget) *n
152152
r := &net.Resolver{
153153
PreferGo: true,
154154
Dial: func(dialCtx context.Context, network, _ string) (net.Conn, error) {
155+
// Go runs A and AAAA lookups in a goroutine of its own, which
156+
// keeps retrying after the caller's timeout under a context that
157+
// is canceled and has lost the caller's values. A pooled
158+
// connection would let such a retry onto the wire unpaced and
159+
// uncounted, so a dial under a finished context is refused (#295).
160+
if err := dialCtx.Err(); err != nil {
161+
return nil, err
162+
}
155163
if c, ok := dialCtx.Value(queryCounterKey{}).(*atomic.Int64); ok {
156164
c.Add(1)
157165
}

‎internal/dns/transport_test.go‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dns
33
import (
44
"context"
55
"crypto/x509"
6+
"errors"
67
"net/http"
78
"net/http/httptest"
89
"slices"
@@ -134,6 +135,69 @@ func TestDoTReusesConnections(t *testing.T) {
134135
}
135136
}
136137

138+
// TestDialRefusesDoneContext covers #295: once the dial context is done the
139+
// Dial hook returns its error instead of a pooled connection, and counts no
140+
// query.
141+
func TestDialRefusesDoneContext(t *testing.T) {
142+
_, servers := startAllTransports(t)
143+
for _, transport := range []string{TransportUDP, TransportTLS} {
144+
t.Run(transport, func(t *testing.T) {
145+
r := NewResolver(2*time.Second, servers[transport])
146+
defer CloseIdleConnections(r)
147+
// One lookup leaves a connection in the pool.
148+
if _, _, err := (Lookup{Resolver: r, Timeout: 2 * time.Second, Types: []string{"A"}}).ResolveTypes(context.Background(), "www.example.com"); err != nil {
149+
t.Fatal(err)
150+
}
151+
var sent atomic.Int64
152+
ctx, cancel := context.WithCancel(WithQueryCounter(context.Background(), &sent))
153+
cancel()
154+
c, err := r.Dial(ctx, "udp", "")
155+
if c != nil {
156+
_ = c.Close()
157+
}
158+
if !errors.Is(err, context.Canceled) {
159+
t.Errorf("Dial under a canceled context: conn %v, error %v; want context.Canceled", c, err)
160+
}
161+
if n := sent.Load(); n != 0 {
162+
t.Errorf("counted %d queries for a refused dial, want 0", n)
163+
}
164+
})
165+
}
166+
}
167+
168+
// TestTimedOutLookupSendsNoLateQueries covers #295 end to end: after an A
169+
// lookup times out, Go's resolver goroutine must not send the name again on
170+
// a pooled socket, so the server sees exactly the queries that were counted.
171+
// Go would retry after the system resolver timeout (5s by default), so the
172+
// test has to wait that long.
173+
func TestTimedOutLookupSendsNoLateQueries(t *testing.T) {
174+
if testing.Short() {
175+
t.Skip("waits out the system resolver timeout")
176+
}
177+
t.Parallel()
178+
srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
179+
if q.Name == "slow.example.com" {
180+
return dnstest.Dropped
181+
}
182+
return dnstest.NXDomain
183+
})
184+
r := NewResolver(300*time.Millisecond, srv.Addr)
185+
defer CloseIdleConnections(r)
186+
var sent atomic.Int64
187+
ctx := WithQueryCounter(context.Background(), &sent)
188+
if _, o := (Lookup{Resolver: r, Timeout: 300 * time.Millisecond, Attempts: 1, Types: []string{"A"}}).Resolve(ctx, "slow.example.com"); o != OutcomeTimeout {
189+
t.Fatalf("dropped query: outcome %v, want timeout", o)
190+
}
191+
time.Sleep(6500 * time.Millisecond)
192+
got := srv.QueriesFor("slow.example.com", dnsmessage.TypeA)
193+
if got != 1 {
194+
t.Errorf("server received %d queries for the timed-out name, want 1", got)
195+
}
196+
if n := sent.Load(); n != got {
197+
t.Errorf("counted %d queries, server received %d", n, got)
198+
}
199+
}
200+
137201
// TestDoTDiscardsClosedConnections covers #113: a server that closes each
138202
// connection after one answer costs one dial per query, not two, because a
139203
// pooled connection the server closed is detected before it is reused.

0 commit comments

Comments
 (0)