Fix concurrent map iteration and write panic in AppRefTracker#1844
Open
somaz94 wants to merge 1 commit into
Open
Fix concurrent map iteration and write panic in AppRefTracker#1844somaz94 wants to merge 1 commit into
somaz94 wants to merge 1 commit into
Conversation
Signed-off-by: somaz <genius5711@gmail.com>
somaz94
added a commit
to somaz94/somaz94
that referenced
this pull request
Jul 15, 2026
somaz94
marked this pull request as ready for review
July 20, 2026 08:25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does / why we need it:
AppRefTracker.AppsForRefandRefsForAppreturned the internal map byreference. Callers such as
SecretHandler.enqueueAppsForUpdateandConfigMapHandler.enqueueAppsForUpdateiterate that map after the tracker lockis released, so a concurrent
ReconcileRefs/RemoveAppFromAllRefswriting thesame map crashes kapp-controller with
fatal error: concurrent map iteration and map write. This reproduces on large clusters with thousands ofnamespaces/secrets under continuous reconciliation.
Both methods now return a shallow copy of the RefKey set while holding the lock,
so callers iterate a private map and never race the writers. The two callers
only iterate the result (read-only), so returning a copy is behavior-preserving.
Which issue(s) this PR fixes:
Fixes #1843
Does this PR introduce a user-facing change?
Additional Notes for your reviewer:
Added two regression tests in
pkg/reftracker/ref_tracker_race_test.go(concurrent read+iterate against a concurrent writer, for both
AppsForRefandRefsForApp). Before the fix they crash with the reported fatal error; after itthey pass, including under
-race.Validation run locally:
go test ./pkg/reftracker/... -racepasses (existing + new tests)gofmt/go vetcleanReview Checklist: