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

Fix a data race in watcher.go #2952

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ require (
github.com/zclconf/go-cty v1.13.2 // indirect
go.opencensus.io v0.24.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/atomic v1.11.0
gocloud.dev v0.36.0 // indirect
gocloud.dev/secrets/hashivault v0.27.0 // indirect
golang.org/x/net v0.21.0
Expand Down
18 changes: 11 additions & 7 deletions provider/pkg/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ func (ow *ObjectWatcher) watch(
var obj *unstructured.Unstructured
results := make(chan result)
poll := func() {
var err error
obj, err = ow.pollFunc()
results <- result{Obj: obj, Err: err}
o, err := ow.pollFunc()
results <- result{Obj: o, Err: err}
Comment on lines +117 to +118
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The race was due to this write to obj happening across goroutines. We can instead communicate that state via the results channel.

}

wait := 500 * time.Millisecond
Expand All @@ -129,6 +128,7 @@ func (ow *ObjectWatcher) watch(
case <-ow.ctx.Done():
return cancellationErr(ow.objName, obj)
case res := <-results:
obj = res.Obj
if stop, err := until(res.Obj, res.Err); err != nil {
return err
} else if stop {
Expand Down Expand Up @@ -160,13 +160,17 @@ type result struct {
// --------------------------------------------------------------------------

func timeoutErr(name string, obj *unstructured.Unstructured) error {
return &watchError{object: obj,
message: fmt.Sprintf("Timeout occurred polling for '%s'", name)}
return &watchError{
object: obj,
message: fmt.Sprintf("Timeout occurred polling for '%s'", name),
}
}

func cancellationErr(name string, obj *unstructured.Unstructured) error {
return &watchError{object: obj,
message: fmt.Sprintf("Resource operation was cancelled for '%s'", name)}
return &watchError{
object: obj,
message: fmt.Sprintf("Resource operation was cancelled for '%s'", name),
}
}

type watchError struct {
Expand Down
17 changes: 8 additions & 9 deletions provider/pkg/watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"go.uber.org/atomic"
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use sync/atomic instead?

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

Expand Down Expand Up @@ -72,27 +73,27 @@ func Test_WatchUntil_PollFuncTimeout(t *testing.T) {
testCompleted := make(chan struct{})
for _, test := range timeoutTests {
go func(test timeoutTest) {
pollFuncCalls, watchFuncCalls := 0, 0
pollFuncCalls, watchFuncCalls := atomic.NewInt32(0), atomic.NewInt32(0)
err := testObjWatcher(
context.Background(),
func() (*unstructured.Unstructured, error) {
pollFuncCalls++
pollFuncCalls.Inc()
return test.pollFunc()
}).
WatchUntil(
func(obj *unstructured.Unstructured) bool {
watchFuncCalls++
watchFuncCalls.Inc()
return test.predicate(obj)
},
test.timeout)
if err == nil || !strings.HasPrefix(err.Error(), timeoutErrPrefix) {
t.Errorf("%s: Polling should have timed out", test.name)
}
if !test.targetPollFuncCalls(pollFuncCalls) {
t.Errorf("%s: Got %d poll function calls, which did not satisfy the test predicate", test.name, pollFuncCalls)
if !test.targetPollFuncCalls(int(pollFuncCalls.Load())) {
t.Errorf("%s: Got %d poll function calls, which did not satisfy the test predicate", test.name, pollFuncCalls.Load())
}
if !test.targetWatchFuncCalls(watchFuncCalls) {
t.Errorf("%s: Got %d watch function calls, which did not satisfy the test predicate", test.name, watchFuncCalls)
if !test.targetWatchFuncCalls(int(watchFuncCalls.Load())) {
t.Errorf("%s: Got %d watch function calls, which did not satisfy the test predicate", test.name, watchFuncCalls.Load())
}
testCompleted <- struct{}{}
}(test)
Expand All @@ -119,7 +120,6 @@ func Test_WatchUntil_Success(t *testing.T) {
return true // Always true.
},
1*time.Second)

if err != nil {
t.Error("Expected watch to terminate without error")
}
Expand All @@ -137,7 +137,6 @@ func Test_RetryUntil_Success(t *testing.T) {
return nil // Always succeeds.
},
1*time.Second)

if err != nil {
t.Error("Expected watch to terminate without error")
}
Expand Down
Loading