Skip to content

Commit

Permalink
Merge #97305
Browse files Browse the repository at this point in the history
97305: sqlstats: fix data race on get percentile values r=maryliag a=maryliag

The function `GetPercentileValues` was using a read lock, since it was only reading the values from `latencySummary`, but the `Stream.flush` used by the `latencySummary` modifies the value, so a read and write lock is necessary.

Issue was reproduced and then tested using:
`./dev test pkg/sql -f=TestQueryCache --race --stress -v --stream-output -- cpus=1`

Fixes #97273
Release note: None

Co-authored-by: maryliag <[email protected]>
  • Loading branch information
craig[bot] and maryliag committed Feb 19, 2023
2 parents 2177d95 + c8e2cf7 commit 3d054f3
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/sql/sqlstats/insights/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ func (d *anomalyDetector) isSlow(stmt *Statement) (decision bool) {
}

func (d *anomalyDetector) GetPercentileValues(id appstatspb.StmtFingerprintID) PercentileValues {
d.mu.RLock()
defer d.mu.RUnlock()
// latencySummary.Query might modify its own state (Stream.flush), so a read-write lock is necessary.
d.mu.Lock()
defer d.mu.Unlock()
latencies := PercentileValues{}
if entry, ok := d.mu.index[id]; ok {
latencySummary := entry.Value.(latencySummaryEntry).value
Expand Down

0 comments on commit 3d054f3

Please sign in to comment.