From eddf468aef34e14ba1fac0c911b2fc9b47789d88 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 21 Nov 2018 07:55:54 -0800 Subject: [PATCH] Lock the targetList properly in go-routines (#6838) Fixes #6483 --- pkg/event/targetlist.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/event/targetlist.go b/pkg/event/targetlist.go index 96a29b828..81d824d54 100644 --- a/pkg/event/targetlist.go +++ b/pkg/event/targetlist.go @@ -66,9 +66,6 @@ type TargetIDErr struct { // Remove - closes and removes targets by given target IDs. func (list *TargetList) Remove(targetids ...TargetID) <-chan TargetIDErr { - list.Lock() - defer list.Unlock() - errCh := make(chan TargetIDErr) go func() { @@ -76,7 +73,10 @@ func (list *TargetList) Remove(targetids ...TargetID) <-chan TargetIDErr { var wg sync.WaitGroup for _, id := range targetids { - if target, ok := list.targets[id]; ok { + list.RLock() + target, ok := list.targets[id] + list.RUnlock() + if ok { wg.Add(1) go func(id TargetID, target Target) { defer wg.Done() @@ -91,9 +91,11 @@ func (list *TargetList) Remove(targetids ...TargetID) <-chan TargetIDErr { } wg.Wait() + list.Lock() for _, id := range targetids { delete(list.targets, id) } + list.Unlock() }() return errCh @@ -114,9 +116,6 @@ func (list *TargetList) List() []TargetID { // Send - sends events to targets identified by target IDs. func (list *TargetList) Send(event Event, targetIDs ...TargetID) <-chan TargetIDErr { - list.Lock() - defer list.Unlock() - errCh := make(chan TargetIDErr) go func() { @@ -124,7 +123,10 @@ func (list *TargetList) Send(event Event, targetIDs ...TargetID) <-chan TargetID var wg sync.WaitGroup for _, id := range targetIDs { - if target, ok := list.targets[id]; ok { + list.RLock() + target, ok := list.targets[id] + list.RUnlock() + if ok { wg.Add(1) go func(id TargetID, target Target) { defer wg.Done()