Skip to content

Commit

Permalink
fix interior state mutability data race
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Lamarre <[email protected]>
  • Loading branch information
alexandreLamarre committed Jul 31, 2024
1 parent fe58712 commit 6114d3a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/objectset/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ func (c *lockableObjectSetRegisterAndCache) Lock(key relatedresource.Key) {
// nothing to lock
return
}
s.mutateMu.RLock()
defer s.mutateMu.RUnlock()
if s.ObjectSet == nil {
// nothing to lock
return
Expand Down Expand Up @@ -284,8 +286,9 @@ func (c *lockableObjectSetRegisterAndCache) deleteState(key relatedresource.Key)
c.stateMapLock.Lock()
delete(c.stateByKey, key)
c.stateMapLock.Unlock()

s.mutateMu.Lock()
s.ObjectSet = nil
s.mutateMu.Unlock()
s.Locked = false
c.stateChanges <- watch.Event{Type: watch.Deleted, Object: s}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/objectset/state.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package objectset

import (
"sync"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -38,6 +39,7 @@ func newObjectSetState(namespace, name string, obj objectSetState) *objectSetSta
obj.UID = types.UID(uuid.New().String())
obj.CreationTimestamp = metav1.NewTime(time.Now())
obj.ResourceVersion = "0"
obj.mutateMu = &sync.RWMutex{}
return &obj
}

Expand All @@ -50,6 +52,8 @@ type objectSetState struct {
// ObjectSet is a pointer to the underlying ObjectSet whose state is being tracked
ObjectSet *objectset.ObjectSet `json:"objectSet,omitempty"`

mutateMu *sync.RWMutex

// Locked represents whether the ObjectSet should be locked in the cluster or not
Locked bool `json:"locked"`
}
Expand Down

0 comments on commit 6114d3a

Please sign in to comment.