From e89927de2b86fa298c80cb115033f74e39ad4eaf Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 14 Oct 2023 18:55:07 -0700 Subject: [PATCH] tsnet: fix data race in TestFallbackTCPHandler Fixes #9805 Signed-off-by: Brad Fitzpatrick --- tsnet/tsnet_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tsnet/tsnet_test.go b/tsnet/tsnet_test.go index 0d1689683..9d98db2fd 100644 --- a/tsnet/tsnet_test.go +++ b/tsnet/tsnet_test.go @@ -26,6 +26,7 @@ import ( "reflect" "strings" "sync" + "sync/atomic" "testing" "time" @@ -652,23 +653,23 @@ func TestFallbackTCPHandler(t *testing.T) { } t.Logf("ping success: %#+v", res) - s1TcpConnCount := 0 + var s1TcpConnCount atomic.Int32 deregister := s1.RegisterFallbackTCPHandler(func(src, dst netip.AddrPort) (handler func(net.Conn), intercept bool) { - s1TcpConnCount++ + s1TcpConnCount.Add(1) return nil, false }) - if _, err = s2.Dial(ctx, "tcp", fmt.Sprintf("%s:8081", s1ip)); err == nil { + if _, err := s2.Dial(ctx, "tcp", fmt.Sprintf("%s:8081", s1ip)); err == nil { t.Fatal("Expected dial error because fallback handler did not intercept") } - if s1TcpConnCount != 1 { - t.Errorf("s1TcpConnCount = %d, want %d", s1TcpConnCount, 1) + if got := s1TcpConnCount.Load(); got != 1 { + t.Errorf("s1TcpConnCount = %d, want %d", got, 1) } deregister() - if _, err = s2.Dial(ctx, "tcp", fmt.Sprintf("%s:8081", s1ip)); err == nil { + if _, err := s2.Dial(ctx, "tcp", fmt.Sprintf("%s:8081", s1ip)); err == nil { t.Fatal("Expected dial error because nothing would intercept") } - if s1TcpConnCount != 1 { - t.Errorf("s1TcpConnCount = %d, want %d", s1TcpConnCount, 1) + if got := s1TcpConnCount.Load(); got != 1 { + t.Errorf("s1TcpConnCount = %d, want %d", got, 1) } }