Skip to content

Comments

fix(discovery): prevent duplicate GVKs and avoid stop channel leaks#2869

Open
mrueg wants to merge 1 commit intokubernetes:mainfrom
mrueg:discovery-fix
Open

fix(discovery): prevent duplicate GVKs and avoid stop channel leaks#2869
mrueg wants to merge 1 commit intokubernetes:mainfrom
mrueg:discovery-fix

Conversation

@mrueg
Copy link
Member

@mrueg mrueg commented Feb 7, 2026

What this PR does / why we need it:

In CRDiscoverer.AppendToMap, GVKs were unconditionally appended to the internal map and new stop channels were created on every call. This could lead to duplicate metric registration and leaked channels/goroutines when existing stop channels were overwritten.

This change:

  • Only creates a new stop channel if one does not already exist for the GVK.
  • Checks for existing Kinds in the discovery map before appending to prevent duplicates.

How does this change affect the cardinality of KSM: (increases, decreases or does not change cardinality)

Which issue(s) this PR fixes: (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged)
Fixes #

@mrueg mrueg requested a review from rexagod February 7, 2026 22:37
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Feb 7, 2026
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mrueg

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

The pull request process is described 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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 7, 2026
@github-project-automation github-project-automation bot moved this to Needs Triage in SIG Instrumentation Feb 7, 2026
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Feb 7, 2026
@mrueg mrueg force-pushed the discovery-fix branch 2 times, most recently from f72ec93 to 1d0d173 Compare February 7, 2026 22:45
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 7, 2026
@mrueg mrueg force-pushed the discovery-fix branch 2 times, most recently from 5d62710 to fb00999 Compare February 7, 2026 22:47
In CRDiscoverer.AppendToMap, GVKs were unconditionally appended to the internal map and new stop channels were created on every call. This could lead to duplicate metric registration and leaked channels/goroutines when existing stop channels were overwritten.

This change:

* Only creates a new stop channel if one does not already exist for the GVK.
* Checks for existing Kinds in the discovery map before appending to prevent duplicates.
@alexandernorth
Copy link

Hi @mrueg , I wanted to quickly jump in as the work I did in #2854 should also address this issue (#2870 would still be needed, as the stopCh still exist although I think it might be more appropriate to use child contexts instead of these floating channels). Happy to support or discuss my PR further, as it changes how discovery is handled.

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

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

3 participants