From cd426eaf4cb41d1ff65ff2b9488b945275b5a265 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 18 Aug 2021 14:39:12 -0700 Subject: [PATCH] net/portmapper: fix t.Log-after-test-done race in tests Signed-off-by: Brad Fitzpatrick --- net/portmapper/igd_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/net/portmapper/igd_test.go b/net/portmapper/igd_test.go index 7b0ba260f..0af633dd1 100644 --- a/net/portmapper/igd_test.go +++ b/net/portmapper/igd_test.go @@ -14,6 +14,7 @@ import ( "testing" "inet.af/netaddr" + "tailscale.com/syncs" "tailscale.com/types/logger" ) @@ -24,6 +25,7 @@ type TestIGD struct { pxpConn net.PacketConn // for NAT-PMP and/or PCP ts *httptest.Server logf logger.Logf + closed syncs.AtomicBool // do* will log which packets are sent, but will not reply to unexpected packets. @@ -97,6 +99,7 @@ func testIPAndGateway() (gw, ip netaddr.IP, ok bool) { } func (d *TestIGD) Close() error { + d.closed.Set(true) d.ts.Close() d.upnpConn.Close() d.pxpConn.Close() @@ -124,7 +127,9 @@ func (d *TestIGD) serveUPnPDiscovery() { for { n, src, err := d.upnpConn.ReadFrom(buf) if err != nil { - d.logf("serveUPnP failed: %v", err) + if !d.closed.Get() { + d.logf("serveUPnP failed: %v", err) + } return } pkt := buf[:n] @@ -149,7 +154,9 @@ func (d *TestIGD) servePxP() { for { n, a, err := d.pxpConn.ReadFrom(buf) if err != nil { - d.logf("servePxP failed: %v", err) + if !d.closed.Get() { + d.logf("servePxP failed: %v", err) + } return } ua := a.(*net.UDPAddr)