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

Added mutex to sync access to the SidecarEgressMap #220

Merged

Conversation

shriramsharma
Copy link
Collaborator

Fixes #219
Added mutex to sync access to the SidecarEgressMap

@@ -177,7 +177,10 @@ func (s *SidecarEgressMap) Put(identity string, namespace string, fqdn string, c
}

func (s *SidecarEgressMap) Get(key string) map[string]SidecarEgress {
return s.cache[key]
s.mutex.Lock()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s.mutex.Lock()
s.mutex.Lock()
defer s.mutex.Unlock()


// Range is a thread safe iterator to iterate through the SidecarEgress map
func (s *SidecarEgressMap) Range(fn func(k string, v map[string]SidecarEgress)) {
s.mutex.Lock()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer s.mutex.Unlock

func (s *SidecarEgressMap) Map() map[string]map[string]SidecarEgress {
return s.cache
}

// Range is a thread safe iterator to iterate through the SidecarEgress map
func (s *SidecarEgressMap) Range(fn func(k string, v map[string]SidecarEgress)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where will this be used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @nirvanagit , I've added this func to provide a thread-safe way to iterate through the SidecarEgress map. Basically discouraging using the .Map() func to get access to the underlying map and then performing a for range on it. I've added comments to make it clear.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this already being used ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's not being used right now

func (s *SidecarEgressMap) Map() map[string]map[string]SidecarEgress {
return s.cache
}

// Range is a thread safe iterator to iterate through the SidecarEgress map
func (s *SidecarEgressMap) Range(fn func(k string, v map[string]SidecarEgress)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@codecov-commenter
Copy link

Codecov Report

Merging #220 (b487b17) into master (059a0d5) will increase coverage by 0.07%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #220      +/-   ##
==========================================
+ Coverage   74.42%   74.49%   +0.07%     
==========================================
  Files          27       27              
  Lines        2545     2552       +7     
==========================================
+ Hits         1894     1901       +7     
  Misses        528      528              
  Partials      123      123              
Impacted Files Coverage Δ
admiral/pkg/controller/common/types.go 89.58% <100.00%> (+0.81%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 059a0d5...b487b17. Read the comment docs.

ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(3*time.Second))
defer cancel()

go func(ctx context.Context) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
go func(ctx context.Context) {
wg := synced.WaitGroup()
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()

Understand it is test code, but to avoid possible memory leak, waitgroup can help in this case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ccfishk . I'm sorry I'm trying to understand where could this lead to a memory leak. As per my understanding, as the context's deadline expires, ctx.Done() would get called and the go routine in that case should return. Please correct me if I'm wrong.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shriramsharma Sorry for the late response.
I mean goroutine leak, which is memory leak, coz goroutine is not garbage cleaned by runtime object, only if the parent process specifically terminate the goroutine before it exit.
One example is that parent process stops for error while goroutine is left as it is. The clean way includes

  • signal (like channle close make(chan interface{})) between parent and the routine,
  • waitgroup synchronize

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ccfishk , I think I understand where you're getting at. Thanks. The issue is with the unpredictable order in which the Done() method will be called. There could be a scenario where the parent go routine exits prior to the child.
I added Waitgroup as you suggested.

Copy link

@ccfishk ccfishk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindly address comments

Added mutex to sync access to the SidecarEgressMap

Signed-off-by: Shriram Sharma <shriram_sharma@intuit.com>
Signed-off-by: Shriram Sharma <shriram_sharma@intuit.com>
Signed-off-by: Shriram Sharma <shriram_sharma@intuit.com>
Signed-off-by: Shriram Sharma <shriram_sharma@intuit.com>
Signed-off-by: Shriram Sharma <shriram_sharma@intuit.com>
@shriramsharma shriramsharma merged commit 7ab4d47 into istio-ecosystem:master May 27, 2022
@shriramsharma shriramsharma deleted the concurrent_map_219 branch May 27, 2022 16:10
psikka1 pushed a commit to psikka1/admiral that referenced this pull request Jun 15, 2022
Signed-off-by: psikka1 <pankaj_sikka@intuit.com>
asushanthk pushed a commit that referenced this pull request Jul 21, 2022
Signed-off-by: sa <sushanth_a@intuit.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: SidecarEgressMap.Get() throws concurrent map access error intermittently
4 participants