Skip to content

fix(nats-jetstream): return error when configured consumer is missing#7668

Merged
rickbrouwer merged 1 commit into
kedacore:mainfrom
robert-blackman:fix/jetstream-msglag-error
Apr 26, 2026
Merged

fix(nats-jetstream): return error when configured consumer is missing#7668
rickbrouwer merged 1 commit into
kedacore:mainfrom
robert-blackman:fix/jetstream-msglag-error

Conversation

@robert-blackman

@robert-blackman robert-blackman commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes a bug in the NATS JetStream scaler where a ScaledObject referencing a consumer that does not exist in the target stream would cause the workload to scale up to maxReplicaCount.

Root cause

getMaxMsgLag() iterates the stream's consumers looking for the configured one. If it isn't found, the function previously fell back to returning s.stream.State.LastSequence - the stream's last published sequence
number. On any non-trivial stream this value is very large, exceeds any reasonable lagThreshold, and the HPA scales the workload out to its maximum.

Fix

  • getMaxMsgLag() now returns (int64, error) and surfaces an error when the configured consumer is missing, instead of returning the stream's last sequence.
  • GetMetricsAndActivity propagates that error so user-configured fallback behaviour can take over.
  • Updated an existing test that was asserting the previous (bugged) behaviour, and added a dedicated unit test covering both the consumer-present and stream-exists-but-consumer-missing cases.

Behaviour change

Before: scaler silently reported a large metric and scaled to max replicas.
After: scaler returns an error; the ScaledObject's fallback can engage, or the metric becomes unavailable until the consumer is created.

Checklist

Fixes #7657

@robert-blackman robert-blackman requested a review from a team as a code owner April 20, 2026 03:49
@github-actions

Copy link
Copy Markdown

Thank you for your contribution! 🙏

Please understand that we will do our best to review your PR and give you feedback as soon as possible, but please bear with us if it takes a little longer as expected.

While you are waiting, make sure to:

  • Add an entry in our changelog in alphabetical order and link related issue
  • Update the documentation, if needed
  • Add unit & e2e tests for your changes
  • GitHub checks are passing
  • Is the DCO check failing? Here is how you can fix DCO issues

Once the initial tests are successful, a KEDA member will ensure that the e2e tests are run. Once the e2e tests have been successfully completed, the PR may be merged at a later date. Please be patient.

Learn more about our contribution guide.

@keda-automation keda-automation requested a review from a team April 20, 2026 03:49
@snyk-io

snyk-io Bot commented Apr 20, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@rickbrouwer

rickbrouwer commented Apr 20, 2026

Copy link
Copy Markdown
Member

/run-e2e jetstream
Update: You can check the progress here

@rickbrouwer rickbrouwer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

@rickbrouwer rickbrouwer added the Awaiting/2nd-approval This PR needs one more approval review label Apr 20, 2026
@rickbrouwer rickbrouwer enabled auto-merge (squash) April 20, 2026 07:38
@rickbrouwer rickbrouwer mentioned this pull request Apr 26, 2026
22 tasks
@rickbrouwer rickbrouwer merged commit c672a4f into kedacore:main Apr 26, 2026
26 checks passed
@JorTurFer

Copy link
Copy Markdown
Member

Thanks a lot!

@rickbrouwer rickbrouwer removed the Awaiting/2nd-approval This PR needs one more approval review label Apr 26, 2026
@robert-blackman robert-blackman deleted the fix/jetstream-msglag-error branch April 28, 2026 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NATS JetStream scaler incorrectly scales to maxReplicaCount when the specified consumer does not exist

3 participants