From d651715528af79b4f9cc8130fbb43eb6acd25bcc Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 10 Mar 2020 17:30:28 -0700 Subject: [PATCH] wgengine/magicsock: synchronize test STUN shutdown. Failure to do so triggers either a data race or a panic in the testing package, due to racey use of t.Logf. Signed-off-by: David Anderson --- wgengine/magicsock/magicsock_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/wgengine/magicsock/magicsock_test.go b/wgengine/magicsock/magicsock_test.go index 7df6d7a45..b78b58089 100644 --- a/wgengine/magicsock/magicsock_test.go +++ b/wgengine/magicsock/magicsock_test.go @@ -159,11 +159,17 @@ func serveSTUN(t *testing.T) (addr string, cleanupFn func()) { stunAddr := pc.LocalAddr().String() stunAddr = strings.Replace(stunAddr, "0.0.0.0:", "localhost:", 1) - go runSTUN(t, pc, &stats) - return stunAddr, func() { pc.Close() } + doneCh := make(chan struct{}) + go runSTUN(t, pc, &stats, doneCh) + return stunAddr, func() { + pc.Close() + <-doneCh + } } -func runSTUN(t *testing.T, pc net.PacketConn, stats *stunStats) { +func runSTUN(t *testing.T, pc net.PacketConn, stats *stunStats, done chan struct{}) { + defer func() { done <- struct{}{} }() + var buf [64 << 10]byte for { n, addr, err := pc.ReadFrom(buf[:])