Skip to content

Commit 4166a88

Browse files
committed
Cancel summarizer context on Stop to ensure goroutine exits
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
1 parent f10a875 commit 4166a88

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

pkg/summary/client/simple.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func (c *summaryResourceClient) Watch(ctx context.Context, opts metav1.ListOptio
9494
err error
9595
)
9696

97+
ctx, cancel := context.WithCancel(ctx)
9798
eventChan := make(chan watch.Event)
9899

99100
if c.namespace == "" {
@@ -102,12 +103,13 @@ func (c *summaryResourceClient) Watch(ctx context.Context, opts metav1.ListOptio
102103
resp, err = c.client.Resource(c.resource).Namespace(c.namespace).Watch(ctx, opts)
103104
}
104105
if err != nil {
106+
cancel()
105107
return nil, err
106108
}
107109

108110
go func() {
109111
defer close(eventChan)
110-
defer resp.Stop()
112+
defer cancel()
111113
for event := range resp.ResultChan() {
112114
// don't encode status objects
113115
if _, ok := event.Object.(*metav1.Status); !ok {
@@ -123,19 +125,26 @@ func (c *summaryResourceClient) Watch(ctx context.Context, opts metav1.ListOptio
123125

124126
return &watcher{
125127
Interface: resp,
128+
cancel: cancel,
126129
eventChan: eventChan,
127130
}, nil
128131
}
129132

130133
type watcher struct {
131134
watch.Interface
135+
cancel context.CancelFunc
132136
eventChan chan watch.Event
133137
}
134138

135139
func (w watcher) ResultChan() <-chan watch.Event {
136140
return w.eventChan
137141
}
138142

143+
func (w watcher) Stop() {
144+
w.cancel()
145+
w.Interface.Stop()
146+
}
147+
139148
func generateSummarizeOpts(schema *schemas.Schema) *summary.SummarizeOptions {
140149
hasObservedGeneration := false
141150
if schema != nil && schema.Attributes != nil {

0 commit comments

Comments
 (0)