Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocator maps accessed outside of their mutex #1034

Closed
kristinapathak opened this issue Aug 11, 2022 · 0 comments · Fixed by #1040
Closed

Allocator maps accessed outside of their mutex #1034

kristinapathak opened this issue Aug 11, 2022 · 0 comments · Fixed by #1040
Labels
area:target-allocator Issues for target-allocator help wanted Extra attention is needed

Comments

@kristinapathak
Copy link
Contributor

type Allocator struct {
m sync.Mutex
targetsWaiting map[string]TargetItem // temp buffer to keep targets that are waiting to be processed
collectors map[string]*collector // all current collectors
TargetItems map[string]*TargetItem
log logr.Logger
}

TargetItems and collectors are normally protected by a mutex, but are being accessed in a few places without the mutex being locked first.

TargetItems:

for _, v := range s.allocator.TargetItems {

for _, v := range s.allocator.TargetItems {

for _, j := range allocator.TargetItems {

collectors:

for _, col := range allocator.collectors {

This is a data race and can cause issues with the endpoints providing the correct information.

I would like to fix this, by:

  • modifying the mutex to be a RWMutex
  • no longer exporting TargetItems
  • adding two functions for the Allocator that each provide a copy of their respective maps - names could be something like Collectors() and TargetItems() or GetCollectors() and GetTargetItems()

A follow up question: If we shallow copy the map values, they will still be pointing to the same objects. How deep do we need to copy everything? Is it enough to dereference the pointers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:target-allocator Issues for target-allocator help wanted Extra attention is needed
Projects
None yet
2 participants