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

storage: fix resource leak in Watch #16817

Merged
merged 1 commit into from
Mar 31, 2023
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
1 change: 1 addition & 0 deletions agent/grpc-external/services/resource/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (s *Server) WatchList(req *pbresource.WatchListRequest, stream pbresource.R
if err != nil {
return err
}
defer watch.Close()

for {
event, err := watch.Next(stream.Context())
Expand Down
2 changes: 2 additions & 0 deletions internal/storage/conformance/conformance.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ func testListWatch(t *testing.T, opts TestOptions) {

watch, err := backend.WatchList(ctx, tc.resourceType, tc.tenancy, tc.namePrefix)
require.NoError(t, err)
t.Cleanup(watch.Close)

for i := 0; i < len(tc.results); i++ {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
Expand All @@ -471,6 +472,7 @@ func testListWatch(t *testing.T, opts TestOptions) {

watch, err := backend.WatchList(ctx, tc.resourceType, tc.tenancy, tc.namePrefix)
require.NoError(t, err)
t.Cleanup(watch.Close)

// Write the seed data after the watch has been established.
for _, r := range seedData {
Expand Down
3 changes: 3 additions & 0 deletions internal/storage/inmem/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func (w *Watch) nextEvent(ctx context.Context) (*stream.Event, error) {
}
}

// Close the watch and free its associated resources.
func (w *Watch) Close() { w.sub.Unsubscribe() }

var eventTopic = stream.StringTopic("resources")

type eventPayload struct {
Expand Down
5 changes: 4 additions & 1 deletion internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,13 @@ type Backend interface {
}

// Watch represents a watch on a given set of resources. Call Next to get the
// next event (i.e. upsert or deletion).
// next event (i.e. upsert or deletion) and Close when you're done watching.
type Watch interface {
// Next returns the next event (i.e. upsert or deletion)
Next(ctx context.Context) (*pbresource.WatchEvent, error)

// Close the watch and free its associated resources.
Close()
}

// UnversionedType represents a pbresource.Type as it is stored without the
Expand Down