From 4ba0891cf461849f403b542ffa43d24754e5e5f4 Mon Sep 17 00:00:00 2001 From: GrigoriyMikhalkin Date: Sat, 18 Oct 2025 15:42:51 +0200 Subject: [PATCH 1/2] Fixing data race in etcd watcher --- .../src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go index cf16606bc51..43306017215 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go @@ -24,6 +24,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" clientv3 "go.etcd.io/etcd/client/v3" @@ -55,17 +56,17 @@ const ( var defaultWatcherMaxLimit int64 = maxLimit // fatalOnDecodeError is used during testing to panic the server if watcher encounters a decoding error -var fatalOnDecodeError = false +var fatalOnDecodeError = atomic.Bool{} func init() { // check to see if we are running in a test environment - TestOnlySetFatalOnDecodeError(true) - fatalOnDecodeError, _ = strconv.ParseBool(os.Getenv("KUBE_PANIC_WATCH_DECODE_ERROR")) + b, _ := strconv.ParseBool(os.Getenv("KUBE_PANIC_WATCH_DECODE_ERROR")) + TestOnlySetFatalOnDecodeError(b) } // TestOnlySetFatalOnDecodeError should only be used for cases where decode errors are expected and need to be tested. e.g. conversion webhooks. func TestOnlySetFatalOnDecodeError(b bool) { - fatalOnDecodeError = b + fatalOnDecodeError.Store(b) } type watcher struct { @@ -758,7 +759,7 @@ func (w *watcher) transformIfCorruptObjectError(e *event, err error) error { func decodeObj(codec runtime.Codec, versioner storage.Versioner, data []byte, rev int64) (_ runtime.Object, err error) { obj, err := runtime.Decode(codec, []byte(data)) if err != nil { - if fatalOnDecodeError { + if fatalOnDecodeError.Load() { // we are running in a test environment and thus an // error here is due to a coder mistake if the defer // does not catch it From 3266f18eea230823dc8fa1a1a4362fda057c7d60 Mon Sep 17 00:00:00 2001 From: GrigoriyMikhalkin Date: Wed, 17 Dec 2025 21:28:43 +0100 Subject: [PATCH 2/2] Update staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go Co-authored-by: Patrick Ohly --- staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go index 43306017215..0559383f8da 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go @@ -56,7 +56,7 @@ const ( var defaultWatcherMaxLimit int64 = maxLimit // fatalOnDecodeError is used during testing to panic the server if watcher encounters a decoding error -var fatalOnDecodeError = atomic.Bool{} +var fatalOnDecodeError atomic.Bool func init() { // check to see if we are running in a test environment