Skip to content

fix: capture Run() error in metrics controller goroutine#326

Closed
abhaygoudannavar wants to merge 0 commit into
openkruise:masterfrom
abhaygoudannavar:fix/stale-err-metrics-goroutine
Closed

fix: capture Run() error in metrics controller goroutine#326
abhaygoudannavar wants to merge 0 commit into
openkruise:masterfrom
abhaygoudannavar:fix/stale-err-metrics-goroutine

Conversation

@abhaygoudannavar
Copy link
Copy Markdown
Contributor

What this PR does

Fixes a closure bug where the metrics controller goroutine logged the wrong error on failure.

Problem

In main.go (lines 280-285), the goroutine that runs the metrics controller never captures the return value of metricsController.Run(). Instead, it references the outer err variable from the metrics.NewController() call:

metricsController, err := metrics.NewController(kruisegameInformerFactory)
if err != nil {
    setupLog.Error(err, "unable to create metrics controller")
    os.Exit(1)
}
kruisegameInformerFactory.Start(signal.Done())
go func() {
    if metricsController.Run(signal) != nil {
        setupLog.Error(err, "unable to setup metrics controller")  // wrong err
        os.Exit(1)
    }
}()

Since NewController succeeded (otherwise we'd have exited), err is nil by the time the goroutine runs. So if Run() fails, the log prints a nil error — making the failure impossible to diagnose.

Fix
Capture the return value of Run() into its own variable:

    if runErr := metricsController.Run(signal); runErr != nil {
        setupLog.Error(runErr, "unable to setup metrics controller")
        os.Exit(1)
    }
}()

fixes #325

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 40.53%. Comparing base (0974487) to head (f4c1adf).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #326   +/-   ##
=======================================
  Coverage   40.53%   40.53%           
=======================================
  Files         112      112           
  Lines       12547    12547           
=======================================
  Hits         5086     5086           
  Misses       7051     7051           
  Partials      410      410           
Flag Coverage Δ
unittests 40.53% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@abhaygoudannavar abhaygoudannavar force-pushed the fix/stale-err-metrics-goroutine branch from f4c1adf to 206e756 Compare April 13, 2026 05:48
@kruise-bot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: metrics controller goroutine logs wrong error due to stale variable capture

2 participants