]> Cypherpunks repositories - gostls13.git/commitdiff
net: fix data race in benchmark
authorDmitriy Vyukov <dvyukov@google.com>
Mon, 7 Apr 2014 07:00:07 +0000 (11:00 +0400)
committerDmitriy Vyukov <dvyukov@google.com>
Mon, 7 Apr 2014 07:00:07 +0000 (11:00 +0400)
If an error happens on a connection, server goroutine can call b.Logf
after benchmark finishes.
So join both client and server goroutines.
Update #7718

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/84750047

src/pkg/net/tcp_test.go

index c8c2a9c0d21debfcf761e7dd45cd0366d3f8e1c4..abd305aa4e3dbe12099d2a2aef4ac887e5d28b79 100644 (file)
@@ -97,6 +97,7 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
                b.Fatalf("Listen failed: %v", err)
        }
        defer ln.Close()
+       serverSem := make(chan bool, numConcurrent)
        // Acceptor.
        go func() {
                for {
@@ -104,9 +105,13 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
                        if err != nil {
                                break
                        }
+                       serverSem <- true
                        // Server connection.
                        go func(c Conn) {
-                               defer c.Close()
+                               defer func() {
+                                       c.Close()
+                                       <-serverSem
+                               }()
                                if timeout {
                                        c.SetDeadline(time.Now().Add(time.Hour)) // Not intended to fire.
                                }
@@ -119,13 +124,13 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
                        }(c)
                }
        }()
-       sem := make(chan bool, numConcurrent)
+       clientSem := make(chan bool, numConcurrent)
        for i := 0; i < conns; i++ {
-               sem <- true
+               clientSem <- true
                // Client connection.
                go func() {
                        defer func() {
-                               <-sem
+                               <-clientSem
                        }()
                        c, err := Dial("tcp", ln.Addr().String())
                        if err != nil {
@@ -144,8 +149,9 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
                        }
                }()
        }
-       for i := 0; i < cap(sem); i++ {
-               sem <- true
+       for i := 0; i < numConcurrent; i++ {
+               clientSem <- true
+               serverSem <- true
        }
 }