From 9bc3f66578a08cdfe084468ea3ef998fa6bf3bb0 Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Mon, 16 Jan 2023 19:15:06 +0100 Subject: [PATCH] fix: prevent panic while refreshing a scaler cache (#4093) (#4119) Signed-off-by: Zbynek Roubalik Signed-off-by: Zbynek Roubalik Signed-off-by: Zbynek Roubalik --- CHANGELOG.md | 1 + pkg/scaling/cache/scalers_cache.go | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bab761e749..5981ceeb376 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ Here is an overview of all new **experimental** features: - **General**: Prevent a panic that might occur while refreshing a scaler cache ([#4092](https://github.com/kedacore/keda/issues/4092)) - **Azure Service Bus Scaler:** Use correct auth flows with pod identity ([#4026](https://github.com/kedacore/keda/issues/4026)) +- **Prometheus Metrics**: Fix exposed metric from `keda_scaled_errors` to `keda_scaled_object_errors` ([#4037](https://github.com/kedacore/keda/issues/4037)) ### Deprecations diff --git a/pkg/scaling/cache/scalers_cache.go b/pkg/scaling/cache/scalers_cache.go index 813ed7b4bfc..6b61d6f9aa5 100644 --- a/pkg/scaling/cache/scalers_cache.go +++ b/pkg/scaling/cache/scalers_cache.go @@ -235,17 +235,20 @@ func (c *ScalersCache) refreshScaler(ctx context.Context, id int) (scalers.Scale } sb := c.Scalers[id] + defer sb.Scaler.Close(ctx) ns, sConfig, err := sb.Factory() if err != nil { return nil, err } + if id < 0 || id >= len(c.Scalers) { + return nil, fmt.Errorf("scaler with id %d not found. Len = %d", id, len(c.Scalers)) + } c.Scalers[id] = ScalerBuilder{ Scaler: ns, ScalerConfig: *sConfig, Factory: sb.Factory, } - sb.Scaler.Close(ctx) return ns, nil }