net/dns/resolver: remove unnecessary/racy WaitGroup.

Fixes #1663

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson 2021-04-20 19:10:45 -07:00 committed by Dave Anderson
parent 8a449c4dcd
commit 30f5d706a1

View File

@ -13,7 +13,6 @@
"hash/crc32" "hash/crc32"
"math/rand" "math/rand"
"net" "net"
"os"
"sync" "sync"
"time" "time"
@ -39,8 +38,6 @@
var errNoUpstreams = errors.New("upstream nameservers not set") var errNoUpstreams = errors.New("upstream nameservers not set")
var aLongTimeAgo = time.Unix(0, 1)
type forwardingRecord struct { type forwardingRecord struct {
src netaddr.IPPort src netaddr.IPPort
createdAt time.Time createdAt time.Time
@ -303,8 +300,6 @@ type fwdConn struct {
// logf allows a fwdConn to log. // logf allows a fwdConn to log.
logf logger.Logf logf logger.Logf
// wg tracks the number of outstanding conn.Read and conn.Write calls.
wg sync.WaitGroup
// change allows calls to read to block until a the network connection has been replaced. // change allows calls to read to block until a the network connection has been replaced.
change *sync.Cond change *sync.Cond
@ -352,15 +347,12 @@ func (c *fwdConn) send(packet []byte, dst netaddr.IPPort) {
} }
c.mu.Unlock() c.mu.Unlock()
a := dst.UDPAddr() _, err := conn.WriteTo(packet, dst.UDPAddr())
c.wg.Add(1)
_, err := conn.WriteTo(packet, a)
c.wg.Done()
if err == nil { if err == nil {
// Success // Success
return return
} }
if errors.Is(err, os.ErrDeadlineExceeded) { if errors.Is(err, net.ErrClosed) {
// We intentionally closed this connection. // We intentionally closed this connection.
// It has been replaced by a new connection. Try again. // It has been replaced by a new connection. Try again.
continue continue
@ -429,14 +421,12 @@ func (c *fwdConn) read(out []byte) int {
} }
c.mu.Unlock() c.mu.Unlock()
c.wg.Add(1)
n, _, err := conn.ReadFrom(out) n, _, err := conn.ReadFrom(out)
c.wg.Done()
if err == nil { if err == nil {
// Success. // Success.
return n return n
} }
if errors.Is(err, os.ErrDeadlineExceeded) { if errors.Is(err, net.ErrClosed) {
// We intentionally closed this connection. // We intentionally closed this connection.
// It has been replaced by a new connection. Try again. // It has been replaced by a new connection. Try again.
continue continue
@ -468,10 +458,7 @@ func (c *fwdConn) closeConnLocked() {
if c.conn == nil { if c.conn == nil {
return return
} }
// Unblock all readers/writers, wait for them, close the connection. c.conn.Close() // unblocks all readers/writers, they'll pick up the next connection.
c.conn.SetDeadline(aLongTimeAgo)
c.wg.Wait()
c.conn.Close()
c.conn = nil c.conn = nil
} }