Skip to content

Commit 6a3d4be

Browse files
ttbradfitz
authored andcommitted
net: Forget lookups for canceled contexts
A sequential lookup using any non-canceled context has a risk of returning the result of the previous lookup for a canceled context (i.e. an error). This is already prevented for timed out context by forgetting the host immediately and extending this to also compare the error to `context.Canceled` resolves this issue. Fixes #22724 Change-Id: I7aafa1459a0de4dc5c4332988fbea23cbf4dba07 Reviewed-on: https://go-review.googlesource.com/77670 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 3a181dc commit 6a3d4be

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/net/lookup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]IPAddr, err
200200
// rather than waiting for the current lookup to
201201
// complete. See issue 8602.
202202
ctxErr := ctx.Err()
203-
if ctxErr == context.DeadlineExceeded {
203+
if ctxErr == context.Canceled || ctxErr == context.DeadlineExceeded {
204204
lookupGroup.Forget(host)
205205
}
206206
err := mapErr(ctxErr)

src/net/lookup_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,25 @@ func TestLookupNonLDH(t *testing.T) {
739739
t.Fatalf("lookup error = %v, want %v", err, errNoSuchHost)
740740
}
741741
}
742+
743+
func TestLookupContextCancel(t *testing.T) {
744+
if runtime.GOOS == "nacl" {
745+
t.Skip("skip on NaCl")
746+
}
747+
748+
ctx, ctxCancel := context.WithCancel(context.Background())
749+
ctxCancel()
750+
751+
_, err := DefaultResolver.LookupIPAddr(ctx, "google.com")
752+
if err != errCanceled {
753+
testenv.SkipFlakyNet(t)
754+
t.Fatalf("unexpected error: %q", err)
755+
}
756+
757+
ctx = context.Background()
758+
759+
_, err = DefaultResolver.LookupIPAddr(ctx, "google.com")
760+
if err != nil {
761+
t.Fatalf("unexpected error: %q", err)
762+
}
763+
}

0 commit comments

Comments
 (0)