Skip to content

Commit

Permalink
CA: Fix a data race in framework.NewHandle
Browse files Browse the repository at this point in the history
Multiple tests can call NewHandle() concurrently, because of
t.Parallel(). NewHandle calls schedulermetrics.InitMetrics()
which modifies global variables, so there's a race.

Wrapped the schedulermetrics.InitMetrics() call in a sync.Once.Do()
so that it's only done once, in a thread-safe manner.
  • Loading branch information
towca committed Dec 4, 2024
1 parent 0fc5c40 commit 16983d2
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cluster-autoscaler/simulator/framework/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package framework
import (
"context"
"fmt"
"sync"

"k8s.io/client-go/informers"
schedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
Expand All @@ -29,6 +30,10 @@ import (
schedulermetrics "k8s.io/kubernetes/pkg/scheduler/metrics"
)

var (
initMetricsOnce sync.Once
)

// Handle is meant for interacting with the scheduler framework.
type Handle struct {
Framework schedulerframework.Framework
Expand All @@ -50,7 +55,9 @@ func NewHandle(informerFactory informers.SharedInformerFactory, schedConfig *sch
}
sharedLister := NewDelegatingSchedulerSharedLister()

schedulermetrics.InitMetrics()
initMetricsOnce.Do(func() {
schedulermetrics.InitMetrics()
})
framework, err := schedulerframeworkruntime.NewFramework(
context.TODO(),
schedulerplugins.NewInTreeRegistry(),
Expand Down

0 comments on commit 16983d2

Please sign in to comment.