Skip to content

Commit c4f881c

Browse files
authored
benchmark: Hold read+write lock while updating server state (#8601)
The `lastResetTime` and `rusageLastReset ` fields in the `benchmarkServer` are written while holding a read lock. This can result in concurrent modifications. This change replaces the `RWMutex` with a regular `Mutex` to avoid such problems. This lock is acquired a couple of times during the entire test run, so contention is not a major concern. RELEASE NOTES: N/A
1 parent 7235bb7 commit c4f881c

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

benchmark/worker/benchmark_server.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ var (
4444
)
4545

4646
type benchmarkServer struct {
47-
port int
48-
cores int
49-
closeFunc func()
50-
mu sync.RWMutex
47+
port int
48+
cores int
49+
closeFunc func()
50+
51+
mu sync.Mutex
5152
lastResetTime time.Time
5253
rusageLastReset *syscall.Rusage
5354
}
@@ -168,8 +169,8 @@ func startBenchmarkServer(config *testpb.ServerConfig, serverPort int) (*benchma
168169
// getStats returns the stats for benchmark server.
169170
// It resets lastResetTime if argument reset is true.
170171
func (bs *benchmarkServer) getStats(reset bool) *testpb.ServerStats {
171-
bs.mu.RLock()
172-
defer bs.mu.RUnlock()
172+
bs.mu.Lock()
173+
defer bs.mu.Unlock()
173174
wallTimeElapsed := time.Since(bs.lastResetTime).Seconds()
174175
rusageLatest := syscall.GetRusage()
175176
uTimeElapsed, sTimeElapsed := syscall.CPUTimeDiff(bs.rusageLastReset, rusageLatest)

0 commit comments

Comments
 (0)