Skip to content

fix(race): fix race #6170

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

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ lint-fix: bin/golangci-lint

.PHONY: unit
unit:
cd $(VALIDATION_TEST_PATH) && go test ./...
go test $$(go list -e ./... | grep -v cmd | grep -v tools | grep -v tests/e2e | grep -v third_party) \
cd $(VALIDATION_TEST_PATH) && go test -race ./...
go test -race $$(go list -e ./... | grep -v cmd | grep -v tools | grep -v tests/e2e | grep -v third_party) \
-cover -coverprofile=coverage.txt -covermode=atomic
sed -i.bak '/generated/d;/fake.go/d' coverage.txt && rm coverage.txt.bak

Expand Down
2 changes: 1 addition & 1 deletion pkg/timanager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func TestClientManagerSource(t *testing.T) {
})
assert.True(tt, synced)

lister.L.Items = c.updated
lister.UpdateItems(c.updated)

select {
case <-ctx.Done():
Expand Down
24 changes: 19 additions & 5 deletions pkg/timanager/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"cmp"
"context"
"slices"
"sync"
"testing"
"time"

Expand All @@ -33,17 +34,23 @@ import (
)

type FakeLister[T any, PT Object[T]] struct {
L List[T, PT]
lock sync.Mutex
L List[T, PT]
}

func (l *FakeLister[T, PT]) List(_ context.Context) (*List[T, PT], error) {
l.lock.Lock()
defer l.lock.Unlock()
return &l.L, nil
}

func (l *FakeLister[T, PT]) GetItems(_ *List[T, PT]) []PT {
objs := make([]PT, 0, len(l.L.Items))
for i := range l.L.Items {
objs = append(objs, &l.L.Items[i])
func (l *FakeLister[T, PT]) GetItems(list *List[T, PT]) []PT {
l.lock.Lock()
defer l.lock.Unlock()

objs := make([]PT, 0, len(list.Items))
for i := range list.Items {
objs = append(objs, &list.Items[i])
}
return objs
}
Expand All @@ -52,6 +59,13 @@ func (*FakeLister[T, PT]) MarkAsInvalid(PT) bool {
return false
}

func (l *FakeLister[T, PT]) UpdateItems(items []T) {
l.lock.Lock()
defer l.lock.Unlock()

l.L.Items = items
}

func TestPoller(t *testing.T) {
cases := []struct {
desc string
Expand Down
24 changes: 20 additions & 4 deletions pkg/timanager/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ type cached[Client, UnderlayClient any] struct {
c Client
f SharedInformerFactory[UnderlayClient]

cancel context.CancelFunc

cacheKeys []string

started bool
cancel context.CancelFunc
lock sync.Mutex
}

func NewCache[Client, UnderlayClient any](keys []string, c Client, f SharedInformerFactory[UnderlayClient]) Cache[Client, UnderlayClient] {
Expand All @@ -157,14 +159,28 @@ func (c *cached[Client, UnderlayClient]) Keys() []string {
}

func (c *cached[Client, UnderlayClient]) Start(ctx context.Context) {
c.lock.Lock()
defer c.lock.Unlock()

if c.started {
return
}

nctx, cancel := context.WithCancel(ctx)
c.cancel = cancel
c.started = true

c.f.Start(nctx.Done())
}

func (c *cached[Client, UnderlayClient]) Stop() {
if c.cancel != nil {
c.cancel()
c.lock.Lock()
defer c.lock.Unlock()

if !c.started {
return
}

c.cancel()
c.f.Shutdown()
}