fix race condition on GetWorkerCounts by cloning map (#24616)

This commit is contained in:
Raymond Ho
2023-12-21 10:28:36 -08:00
committed by GitHub
parent 84bc8b1743
commit 0ed86eb1a8
3 changed files with 29 additions and 1 deletions

3
changelog/24616.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
fairshare: fix a race condition in JobManager.GetWorkerCounts
```

View File

@@ -143,7 +143,12 @@ func (j *JobManager) GetPendingJobCount() int {
func (j *JobManager) GetWorkerCounts() map[string]int {
j.l.RLock()
defer j.l.RUnlock()
return j.workerCount
workerCounts := make(map[string]int, len(j.workerCount))
for k, v := range j.workerCount {
workerCounts[k] = v
}
return workerCounts
}
// GetWorkQueueLengths() returns a map of queue ID to number of jobs in the queue

View File

@@ -747,3 +747,23 @@ func TestFairshare_queueWorkersSaturated(t *testing.T) {
j.l.RUnlock()
}
}
func TestJobManager_GetWorkerCounts_RaceCondition(t *testing.T) {
j := NewJobManager("test-job-mgr", 20, nil, nil)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
j.incrementWorkerCount("a")
}
}()
wcs := j.GetWorkerCounts()
wcs["foo"] = 10
for worker, count := range wcs {
_ = worker
_ = count
}
wg.Wait()
}