]> git.feebdaed.xyz Git - 0xmirror/grpc-go.git/commitdiff
benchmark: Hold read+write lock while updating server state (#8601)
authorArjan Singh Bal <46515553+arjan-bal@users.noreply.github.com>
Tue, 23 Sep 2025 19:37:31 +0000 (01:07 +0530)
committerGitHub <noreply@github.com>
Tue, 23 Sep 2025 19:37:31 +0000 (01:07 +0530)
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

benchmark/worker/benchmark_server.go

index 01b3a5ce8a9f899e7e59eebe3e18d591dcd3866e..fcf5027047acd7af643ad5d0d1b130eee52877d5 100644 (file)
@@ -44,10 +44,11 @@ var (
 )
 
 type benchmarkServer struct {
-       port            int
-       cores           int
-       closeFunc       func()
-       mu              sync.RWMutex
+       port      int
+       cores     int
+       closeFunc func()
+
+       mu              sync.Mutex
        lastResetTime   time.Time
        rusageLastReset *syscall.Rusage
 }
@@ -168,8 +169,8 @@ func startBenchmarkServer(config *testpb.ServerConfig, serverPort int) (*benchma
 // getStats returns the stats for benchmark server.
 // It resets lastResetTime if argument reset is true.
 func (bs *benchmarkServer) getStats(reset bool) *testpb.ServerStats {
-       bs.mu.RLock()
-       defer bs.mu.RUnlock()
+       bs.mu.Lock()
+       defer bs.mu.Unlock()
        wallTimeElapsed := time.Since(bs.lastResetTime).Seconds()
        rusageLatest := syscall.GetRusage()
        uTimeElapsed, sTimeElapsed := syscall.CPUTimeDiff(bs.rusageLastReset, rusageLatest)