fix(NSC): add mutex to protect nodeport healthcheck maps from data race#2021
Closed
Aprazor wants to merge 1 commit intocloudnativelabs:masterfrom
Closed
fix(NSC): add mutex to protect nodeport healthcheck maps from data race#2021Aprazor wants to merge 1 commit intocloudnativelabs:masterfrom
Aprazor wants to merge 1 commit intocloudnativelabs:masterfrom
Conversation
UpdateServicesInfo writes serviceInfoMap and endpointsInfoMap from the controller sync goroutine, while the HTTP handler reads them from net/http's goroutine pool. This is a data race. Add sync.RWMutex to nphcServicesInfo and guard writes with Lock/Unlock and reads with RLock/RUnlock.
Collaborator
|
Hi @Aprazor — please see my comment on #2020 (#2020 (comment)) for feedback on how to consolidate your PRs. Thanks! |
Contributor
Author
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 type of PR is this?
bug
What this PR does / why we need it:
UpdateServicesInfo()writesserviceInfoMapandendpointsInfoMapfrom the controller's sync goroutine, while the HTTP handlerHandler()reads them fromnet/http's goroutine pool. This is a data race — any concurrent NodePort health check HTTP request during a sync cycle races with the map write.The fix adds a
sync.RWMutextonphcServicesInfoand guards writes withLock/Unlockand reads withRLock/RUnlock, allowing concurrent health check reads while blocking only during sync writes.Which issue(s) this PR is related to:
None found.
Was AI used during the creation of this PR?
What, if any, amount of integration testing was done with this change in a Kubernetes environment?
Unit tests pass. No integration testing — the race requires concurrent HTTP requests during sync to trigger.
Does this PR introduce a breaking change?
Anything else the reviewer should know that wasn't already covered?
The race would be detectable with
go test -raceunder load. RWMutex was chosen over a regular Mutex because health check reads are far more frequent than sync writes.