Skip to content

Fix bug: bad critical section for multiple values #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions rpc/rpcutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ func recordRPCCost(startTime time.Time, method string, err error) {
// Optimistically, val will not be nil except the first Call of method
// expvar uses sync.Map
// So, we try it first without lock
if val = expvar.Get(name); val == nil {
val = expvar.Get(name)
valC = expvar.Get(nameC)
if val == nil || valC == nil {
callRPCExpvarLock.Lock()
val = expvar.Get(name)
if val == nil {
Expand All @@ -191,9 +193,9 @@ func recordRPCCost(startTime time.Time, method string, err error) {
}
callRPCExpvarLock.Unlock()
val = expvar.Get(name)
valC = expvar.Get(nameC)
}
val.(mw.Metric).Add(costTime.Seconds())
valC = expvar.Get(nameC)
valC.(mw.Metric).Add(1)
return
}
Expand Down
26 changes: 26 additions & 0 deletions rpc/rpcutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package rpc

import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -443,3 +444,28 @@ func BenchmarkPersistentCaller_Call(b *testing.B) {

server.Stop()
}

func TestRecordRPCCost(t *testing.T) {
Convey("Bug: bad critical section for multiple values", t, func(c C) {
var (
start = time.Now()
rounds = 1000
concurrent = 10
wg = &sync.WaitGroup{}
body = func(i int) {
defer func() {
c.So(recover(), ShouldBeNil)
wg.Done()
}()
recordRPCCost(start, fmt.Sprintf("M%d", i), nil)
}
)
defer wg.Wait()
for i := 0; i < rounds; i++ {
for j := 0; j < concurrent; j++ {
wg.Add(1)
go body(i)
}
}
})
}