Skip to content

fix: correct context cancellation in waitForState to fix flaky test#7547

Merged
rickbrouwer merged 7 commits into
kedacore:mainfrom
RokeshVS:fix/flaky-testwait-for-state-7542
Mar 19, 2026
Merged

fix: correct context cancellation in waitForState to fix flaky test#7547
rickbrouwer merged 7 commits into
kedacore:mainfrom
RokeshVS:fix/flaky-testwait-for-state-7542

Conversation

@RokeshVS

@RokeshVS RokeshVS commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes the flaky TestWaitForState test in pkg/scalers/external_scaler_test.go.

Change 1 — external_scaler.go (bug fix)

waitForState had continue instead of return when the context was cancelled.
This caused the goroutine to spin forever, blocking the test indefinitely.

// Before
if !changeState {
    // ctx is done, return
    continue  // goroutine loops forever when ctx is cancelled
}

// After
if !changeState {
    // ctx is done, return
    return  // goroutine exits cleanly when ctx is cancelled
}

Change 2 — external_scaler_test.go (timeout fix)

The select timeout of 1s was too tight. After GracefulStop(), the gRPC
client occasionally takes slightly over 1s to transition to SHUTDOWN state.
Increased to 5s to give sufficient headroom.

// Before
case <-time.After(time.Second * 1):

// After
case <-time.After(time.Second * 5):

Result

Ran the test 10 times consecutively — all pass consistently:

go test ./pkg/scalers/ -run TestWaitForState -v -count=10
=== RUN   TestWaitForState
    external_scaler_test.go:268: close state: SHUTDOWN
--- PASS: TestWaitForState (5.01s)
=== RUN   TestWaitForState
    external_scaler_test.go:268: close state: SHUTDOWN
--- PASS: TestWaitForState (5.03s)
=== RUN   TestWaitForState
    external_scaler_test.go:268: close state: SHUTDOWN
--- PASS: TestWaitForState (5.01s)
=== RUN   TestWaitForState
    external_scaler_test.go:268: close state: SHUTDOWN
--- PASS: TestWaitForState (5.01s)
=== RUN   TestWaitForState
    external_scaler_test.go:268: close state: SHUTDOWN
--- PASS: TestWaitForState (5.01s)
=== RUN   TestWaitForState
    external_scaler_test.go:268: close state: SHUTDOWN
--- PASS: TestWaitForState (5.01s)
=== RUN   TestWaitForState
    external_scaler_test.go:268: close state: SHUTDOWN
--- PASS: TestWaitForState (5.01s)
=== RUN   TestWaitForState
    external_scaler_test.go:268: close state: SHUTDOWN
--- PASS: TestWaitForState (5.01s)
=== RUN   TestWaitForState
    external_scaler_test.go:268: close state: SHUTDOWN
--- PASS: TestWaitForState (5.01s)
=== RUN   TestWaitForState
    external_scaler_test.go:268: close state: SHUTDOWN
--- PASS: TestWaitForState (5.01s)
PASS
ok      github.com/kedacore/keda/v2/pkg/scalers 50.208s

Checklist

  • Changelog has been updated and is aligned with our changelog requirements, only when the change impacts end users
  • Commits are signed with Developer Certificate of Origin (DCO - learn more)

Fixes #7542

@RokeshVS RokeshVS requested a review from a team as a code owner March 15, 2026 21:08
@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 March 15, 2026 21:08
@snyk-io

snyk-io Bot commented Mar 15, 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.

Comment thread CHANGELOG.md Outdated
@keda-automation keda-automation requested a review from a team March 16, 2026 09:05
Comment thread CHANGELOG.md Outdated
Comment thread CHANGELOG.md Outdated
Co-authored-by: Rick Brouwer <[email protected]>
Signed-off-by: RokeshVS <[email protected]>
@keda-automation keda-automation requested a review from a team March 16, 2026 09:49
Comment thread CHANGELOG.md Outdated
Co-authored-by: Rick Brouwer <[email protected]>
Signed-off-by: RokeshVS <[email protected]>
@keda-automation keda-automation requested a review from a team March 16, 2026 09:57
Comment thread CHANGELOG.md Outdated
Signed-off-by: Rick Brouwer <[email protected]>
@rickbrouwer

rickbrouwer commented Mar 16, 2026

Copy link
Copy Markdown
Member

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

@keda-automation keda-automation requested a review from a team March 16, 2026 09:59
Comment thread CHANGELOG.md Outdated
Signed-off-by: Rick Brouwer <[email protected]>
@keda-automation keda-automation requested a review from a team March 16, 2026 10:05
@rickbrouwer

rickbrouwer commented Mar 16, 2026

Copy link
Copy Markdown
Member

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

@rickbrouwer rickbrouwer added the Awaiting/2nd-approval This PR needs one more approval review label Mar 16, 2026
@rickbrouwer rickbrouwer enabled auto-merge (squash) March 16, 2026 12:41
@rickbrouwer rickbrouwer merged commit 0286c9c into kedacore:main Mar 19, 2026
26 checks passed
@rickbrouwer

Copy link
Copy Markdown
Member

Hi @RokeshVS

The issue still exists, so unfortunately it has not yet been resolved. Do you see a chance to take another look at this? See: https://github.com/kedacore/keda/actions/runs/23997664144/job/69988405523?pr=7606

@RokeshVS

RokeshVS commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

Hi @rickbrouwer ,

Thank you for flagging that the issue still existed after #7547 was merged. I have investigated the root cause further and raised a new PR to address the remaining race condition in TestWaitForState.

The previous fix corrected the continuereturn in waitForState and increased the timeout, but the flakiness persisted under -race due to:

  • grpcClient starting in Idle after grpc.NewClient(), causing waitForState to miss state transitions
  • context.TODO() providing no cancellation, causing t.Log to be called after the test completed → panic

The new PR fixes this by calling grpcClient.Connect() to force an active connection, introducing a cancellable waitCtx so the goroutine exits cleanly, and calling grpcClient.Close() explicitly after GracefulStop() for a deterministic Shutdown transition.

Validated with go test ./pkg/scalers/ -run TestWaitForState -v -count=20 -race — all 20 runs pass consistently.

PR - #7635

Would appreciate your review when you get a chance. Thank you!

jansworld pushed a commit to jansworld/keda that referenced this pull request Apr 10, 2026
… external scaler (kedacore#7547)

Signed-off-by: RokeshVS <[email protected]>
Signed-off-by: Rick Brouwer <[email protected]>
Co-authored-by: Rick Brouwer <[email protected]>
Signed-off-by: jansworld <[email protected]>
@rickbrouwer rickbrouwer removed the Awaiting/2nd-approval This PR needs one more approval review label Apr 13, 2026
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.

Flaky external_scaler_test test (TestWaitForState)

3 participants