From d9334999babf33cd6fe8a86bb74678173d4c3e30 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 29 Apr 2020 17:02:31 +0800 Subject: [PATCH] fix a potential context leak --- component/fakedns/middleware.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/component/fakedns/middleware.go b/component/fakedns/middleware.go index 5a82df6..0bd6ddd 100644 --- a/component/fakedns/middleware.go +++ b/component/fakedns/middleware.go @@ -33,13 +33,15 @@ func dnsExchange(r *D.Msg) (msg *D.Msg) { c := new(D.Client) c.Net = "tcp" for _, dns := range backendDNS { - ctx, _ := context.WithTimeout(context.Background(), time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) msg, _, _ = c.ExchangeContext(ctx, r, dns) + cancel() // call cancel as soon as the operations running in this Context complete if msg != nil { - // success, exit query. - break + // success, exit query + goto Out } } +Out: return msg }