Commit 8076f21e authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

net: fix data race in benchmark

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
parent 2e8697de
...@@ -97,6 +97,7 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) { ...@@ -97,6 +97,7 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
b.Fatalf("Listen failed: %v", err) b.Fatalf("Listen failed: %v", err)
} }
defer ln.Close() defer ln.Close()
serverSem := make(chan bool, numConcurrent)
// Acceptor. // Acceptor.
go func() { go func() {
for { for {
...@@ -104,9 +105,13 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) { ...@@ -104,9 +105,13 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
if err != nil { if err != nil {
break break
} }
serverSem <- true
// Server connection. // Server connection.
go func(c Conn) { go func(c Conn) {
defer c.Close() defer func() {
c.Close()
<-serverSem
}()
if timeout { if timeout {
c.SetDeadline(time.Now().Add(time.Hour)) // Not intended to fire. 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) { ...@@ -119,13 +124,13 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
}(c) }(c)
} }
}() }()
sem := make(chan bool, numConcurrent) clientSem := make(chan bool, numConcurrent)
for i := 0; i < conns; i++ { for i := 0; i < conns; i++ {
sem <- true clientSem <- true
// Client connection. // Client connection.
go func() { go func() {
defer func() { defer func() {
<-sem <-clientSem
}() }()
c, err := Dial("tcp", ln.Addr().String()) c, err := Dial("tcp", ln.Addr().String())
if err != nil { if err != nil {
...@@ -144,8 +149,9 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) { ...@@ -144,8 +149,9 @@ func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
} }
}() }()
} }
for i := 0; i < cap(sem); i++ { for i := 0; i < numConcurrent; i++ {
sem <- true clientSem <- true
serverSem <- true
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment