Skip to content

Commit

Permalink
net: Forget lookups for canceled contexts
Browse files Browse the repository at this point in the history
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 <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
tt authored and bradfitz committed Nov 15, 2017
1 parent 3a181dc commit 6a3d4be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/net/lookup.go
Expand Up @@ -200,7 +200,7 @@ func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]IPAddr, err
// rather than waiting for the current lookup to
// complete. See issue 8602.
ctxErr := ctx.Err()
if ctxErr == context.DeadlineExceeded {
if ctxErr == context.Canceled || ctxErr == context.DeadlineExceeded {
lookupGroup.Forget(host)
}
err := mapErr(ctxErr)
Expand Down
22 changes: 22 additions & 0 deletions src/net/lookup_test.go
Expand Up @@ -739,3 +739,25 @@ func TestLookupNonLDH(t *testing.T) {
t.Fatalf("lookup error = %v, want %v", err, errNoSuchHost)
}
}

func TestLookupContextCancel(t *testing.T) {
if runtime.GOOS == "nacl" {
t.Skip("skip on NaCl")
}

ctx, ctxCancel := context.WithCancel(context.Background())
ctxCancel()

_, err := DefaultResolver.LookupIPAddr(ctx, "google.com")
if err != errCanceled {
testenv.SkipFlakyNet(t)
t.Fatalf("unexpected error: %q", err)
}

ctx = context.Background()

_, err = DefaultResolver.LookupIPAddr(ctx, "google.com")
if err != nil {
t.Fatalf("unexpected error: %q", err)
}
}

0 comments on commit 6a3d4be

Please sign in to comment.