From 430d378f7d70a6386e61190050c2c781f3877124 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 17 Oct 2021 17:11:27 -0700 Subject: [PATCH] wgengine/magicsock: fix data race with sync.Pool in error+logging path Fixes #3122 Change-Id: Ib52e84f9bd5813d6cf2e80ce5b2296912a48e064 Signed-off-by: Brad Fitzpatrick (cherry picked from commit c759fcc7d3ead9ddd188e980ee3c13e865fcd185) --- wgengine/magicsock/magicsock.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index db9cfe00a..7f6fb90e3 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -1149,8 +1149,12 @@ func (c *Conn) Send(b []byte, ep conn.Endpoint) error { // See sendAddr's docs on the return value meanings. func (c *Conn) sendUDP(ipp netaddr.IPPort, b []byte) (sent bool, err error) { ua := udpAddrPool.Get().(*net.UDPAddr) - defer udpAddrPool.Put(ua) - return c.sendUDPStd(ipp.UDPAddrAt(ua), b) + sent, err = c.sendUDPStd(ipp.UDPAddrAt(ua), b) + if err == nil { + // Only return it to the pool on success; Issue 3122. + udpAddrPool.Put(ua) + } + return } // sendUDP sends UDP packet b to addr.