From 16983d2cdd5fd5edf1f66187d93ebe7f98abecba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Tu=C5=BCnik?= Date: Wed, 4 Dec 2024 20:18:50 +0100 Subject: [PATCH] CA: Fix a data race in framework.NewHandle 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. --- cluster-autoscaler/simulator/framework/handle.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cluster-autoscaler/simulator/framework/handle.go b/cluster-autoscaler/simulator/framework/handle.go index f8b2250648af..f56c3c94b0b1 100644 --- a/cluster-autoscaler/simulator/framework/handle.go +++ b/cluster-autoscaler/simulator/framework/handle.go @@ -19,6 +19,7 @@ package framework import ( "context" "fmt" + "sync" "k8s.io/client-go/informers" schedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config" @@ -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 @@ -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(),