Skip to content

Commit 45bc87f

Browse files
authored
Merge pull request #4184 from fahedouch/refacto-and-fix-stats-showAll
fix: enhance event monitoring by adding close event channel
2 parents 63119fd + 37ca1f4 commit 45bc87f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pkg/cmd/container/stats.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
111111
waitFirst := &sync.WaitGroup{}
112112
cStats := stats{}
113113

114-
monitorContainerEvents := func(started chan<- struct{}, c chan *events.Envelope) {
114+
monitorContainerEvents := func(started chan<- struct{}, c chan *events.Envelope, closeEventChan chan struct{}) {
115115
eventsClient := client.EventService()
116116
eventsCh, errCh := eventsClient.Subscribe(ctx)
117117

@@ -121,6 +121,9 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
121121

122122
for {
123123
select {
124+
case <-closeEventChan:
125+
// c is closed, so we can stop monitoring events
126+
return
124127
case event := <-eventsCh:
125128
c <- event
126129
case err = <-errCh:
@@ -207,11 +210,16 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
207210
})
208211

209212
eventChan := make(chan *events.Envelope)
213+
closeEventChan := make(chan struct{})
210214

211215
go eh.Watch(eventChan)
212-
go monitorContainerEvents(started, eventChan)
216+
go monitorContainerEvents(started, eventChan, closeEventChan)
217+
218+
defer func() {
219+
closeEventChan <- struct{}{}
220+
close(eventChan)
221+
}()
213222

214-
defer close(eventChan)
215223
<-started
216224

217225
// Start a goroutine to retrieve the initial list of containers stats.

0 commit comments

Comments
 (0)