From 9ce34a16cb0a4adc55efaa9abaceb41ede280e1c Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Fri, 20 Sep 2019 16:04:06 +0800 Subject: [PATCH] Fix: dial should return when dns failed (#311) --- adapters/outbound/util.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/adapters/outbound/util.go b/adapters/outbound/util.go index 3b173119..79229916 100644 --- a/adapters/outbound/util.go +++ b/adapters/outbound/util.go @@ -105,7 +105,7 @@ func dialTimeout(network, address string, timeout time.Duration) (net.Conn, erro return nil, err } - dialer := net.Dialer{Timeout: timeout} + dialer := net.Dialer{} ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() @@ -122,31 +122,31 @@ func dialTimeout(network, address string, timeout time.Duration) (net.Conn, erro var primary, fallback dialResult startRacer := func(ctx context.Context, host string, ipv6 bool) { - var err error + result := dialResult{ipv6: ipv6, done: true} + defer func() { + select { + case results <- result: + case <-returned: + if result.Conn != nil { + result.Conn.Close() + } + } + }() var ip net.IP if ipv6 { - ip, err = dns.ResolveIPv6(host) + ip, result.error = dns.ResolveIPv6(host) } else { - ip, err = dns.ResolveIPv4(host) + ip, result.error = dns.ResolveIPv4(host) } - if err != nil { + if result.error != nil { return } - var c net.Conn if ipv6 { - c, err = dialer.DialContext(ctx, "tcp6", net.JoinHostPort(ip.String(), port)) + result.Conn, result.error = dialer.DialContext(ctx, "tcp6", net.JoinHostPort(ip.String(), port)) } else { - c, err = dialer.DialContext(ctx, "tcp4", net.JoinHostPort(ip.String(), port)) - } - - select { - case results <- dialResult{Conn: c, error: err, ipv6: ipv6, done: true}: - case <-returned: - if c != nil { - c.Close() - } + result.Conn, result.error = dialer.DialContext(ctx, "tcp4", net.JoinHostPort(ip.String(), port)) } }