mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-10-29 17:52:32 +00:00
fix race condition on GetWorkerCounts by cloning map (#24616)
This commit is contained in:
3
changelog/24616.txt
Normal file
3
changelog/24616.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
```release-note:bug
|
||||
fairshare: fix a race condition in JobManager.GetWorkerCounts
|
||||
```
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user