From 49bbf7c26817f98c166e953bbded6a0d8d9def95 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Fri, 27 Jan 2023 18:08:15 +0100 Subject: [PATCH] scheduler_perf: fix race condition collector.collect got called without ensuring that collector.run had terminated, so it could have happened that collector.run adds another sample while collector.collect is reading them. --- test/integration/scheduler_perf/scheduler_perf_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/integration/scheduler_perf/scheduler_perf_test.go b/test/integration/scheduler_perf/scheduler_perf_test.go index 9177fc6bb89..ecf9bc236cd 100644 --- a/test/integration/scheduler_perf/scheduler_perf_test.go +++ b/test/integration/scheduler_perf/scheduler_perf_test.go @@ -832,12 +832,19 @@ func runWorkload(ctx context.Context, b *testing.B, tc *testCase, w *workload) [ var collectors []testDataCollector var collectorCtx context.Context var collectorCancel func() + var collectorWG sync.WaitGroup if concreteOp.CollectMetrics { collectorCtx, collectorCancel = context.WithCancel(ctx) defer collectorCancel() collectors = getTestDataCollectors(podInformer, fmt.Sprintf("%s/%s", b.Name(), namespace), namespace, tc.MetricsCollectorConfig) for _, collector := range collectors { - go collector.run(collectorCtx) + // Need loop-local variable for function below. + collector := collector + collectorWG.Add(1) + go func() { + defer collectorWG.Done() + collector.run(collectorCtx) + }() } } if err := createPods(ctx, b, namespace, concreteOp, client); err != nil { @@ -861,6 +868,7 @@ func runWorkload(ctx context.Context, b *testing.B, tc *testCase, w *workload) [ // same time, so if we're here, it means that all pods have been // scheduled. collectorCancel() + collectorWG.Wait() mu.Lock() for _, collector := range collectors { dataItems = append(dataItems, collector.collect()...)