From 406841b9a19c039f67ade21af3777dc452f9c11b Mon Sep 17 00:00:00 2001 From: Aris Tzoumas Date: Mon, 9 Nov 2026 14:03:43 +0200 Subject: [PATCH] Fix concurrent map read and write in the Helm operator's watched secrets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getWatchedSecretsForNamespace added a namespace's entry under watchedSecretsMutex but read the map again after unlocking it. A reconcile could then read the map while another reconcile added a namespace, and the Go runtime aborted the operator with "fatal error: concurrent map read and map write". Return the entry that was read or created under the lock. Signed-off-by: Aris Tzoumas 🔒 Scanned for secrets using gitleaks 8.30.1 --- .../helm-operator-watched-secrets-race.yaml | 7 +++ internal/helm/client/actionconfig.go | 12 +++-- internal/helm/client/actionconfig_test.go | 47 +++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 changelog/fragments/helm-operator-watched-secrets-race.yaml create mode 100644 internal/helm/client/actionconfig_test.go diff --git a/changelog/fragments/helm-operator-watched-secrets-race.yaml b/changelog/fragments/helm-operator-watched-secrets-race.yaml new file mode 100644 index 00000000000..1d06abe1240 --- /dev/null +++ b/changelog/fragments/helm-operator-watched-secrets-race.yaml @@ -0,0 +1,7 @@ +entries: + - description: > + For Helm-based operators, fixed a concurrent map read and write that could crash the + operator with "fatal error: concurrent map read and map write" when reconciles of + different namespaces started at the same time. + kind: bugfix + breaking: false diff --git a/internal/helm/client/actionconfig.go b/internal/helm/client/actionconfig.go index a8cca3e0c74..b7832af22d8 100644 --- a/internal/helm/client/actionconfig.go +++ b/internal/helm/client/actionconfig.go @@ -81,12 +81,14 @@ type actionConfigGetter struct { // Creates a new watcher for each namespace to not require cluster-wide secret access func (acg *actionConfigGetter) getWatchedSecretsForNamespace(namespace string) *WatchedSecrets { acg.watchedSecretsMutex.Lock() - if _, found := acg.watchedSecrets[namespace]; !found { - acg.watchedSecrets[namespace] = NewWatchedSecrets(acg.kubeClientSet, namespace) - acg.watchedSecrets[namespace].Run() + defer acg.watchedSecretsMutex.Unlock() + ws, found := acg.watchedSecrets[namespace] + if !found { + ws = NewWatchedSecrets(acg.kubeClientSet, namespace) + ws.Run() + acg.watchedSecrets[namespace] = ws } - acg.watchedSecretsMutex.Unlock() - return acg.watchedSecrets[namespace] + return ws } func (acg *actionConfigGetter) ActionConfigFor(obj client.Object) (*action.Configuration, error) { diff --git a/internal/helm/client/actionconfig_test.go b/internal/helm/client/actionconfig_test.go new file mode 100644 index 00000000000..6b9f1aa74ac --- /dev/null +++ b/internal/helm/client/actionconfig_test.go @@ -0,0 +1,47 @@ +// Copyright 2026 The Operator-SDK Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package client + +import ( + "fmt" + "sync" + "testing" + + "github.com/stretchr/testify/assert" + "k8s.io/client-go/kubernetes/fake" +) + +// Run with -race: reconciles of different namespaces look up and add watched secrets concurrently. +func TestGetWatchedSecretsForNamespaceConcurrently(t *testing.T) { + acg := &actionConfigGetter{ + kubeClientSet: fake.NewClientset(), + watchedSecrets: map[string]*WatchedSecrets{}, + watchedSecretsMutex: &sync.Mutex{}, + } + const workers, namespaces = 8, 50 + var wg sync.WaitGroup + for w := range workers { + wg.Add(1) + go func() { + defer wg.Done() + for n := range namespaces { + ns := fmt.Sprintf("ns-%d", (n+w)%namespaces) + assert.NotNil(t, acg.getWatchedSecretsForNamespace(ns)) + } + }() + } + wg.Wait() + assert.Len(t, acg.watchedSecrets, namespaces) +}