]> Cypherpunks repositories - gostls13.git/commitdiff
net: Forget lookups for canceled contexts
authorTroels Thomsen <troels@thomsen.io>
Tue, 14 Nov 2017 22:22:19 +0000 (23:22 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 15 Nov 2017 22:34:55 +0000 (22:34 +0000)
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>
src/net/lookup.go
src/net/lookup_test.go

index c9f327050afad8a7c6323d63985b5c3fd18d18f5..607953bba58f59875b375dbe9cc91f1707516740 100644 (file)
@@ -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)
index 68a7abe95dfbf13124b25be9add619ab815fba02..8a0212a3f4a0d28782b5f039acce549a8101f25d 100644 (file)
@@ -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)
+       }
+}