Merge pull request #6406 from vrothberg/stats-race

container stats: fix --no-stream race
This commit is contained in:
OpenShift Merge Robot 2020-05-27 09:26:39 -04:00 committed by GitHub
commit 9cb5c87284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strings"
"sync"
"text/tabwriter"
"text/template"
@ -111,14 +112,20 @@ func stats(cmd *cobra.Command, args []string) error {
}
}
statsOptions.StatChan = make(chan []*define.ContainerStats, 1)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
for reports := range statsOptions.StatChan {
if err := outputStats(reports); err != nil {
logrus.Error(err)
}
}
wg.Done()
}()
return registry.ContainerEngine().ContainerStats(registry.Context(), args, statsOptions)
err := registry.ContainerEngine().ContainerStats(registry.Context(), args, statsOptions)
wg.Wait()
return err
}
func outputStats(reports []*define.ContainerStats) error {

View File

@ -1087,6 +1087,7 @@ func (ic *ContainerEngine) Shutdown(_ context.Context) {
}
func (ic *ContainerEngine) ContainerStats(ctx context.Context, namesOrIds []string, options entities.ContainerStatsOptions) error {
defer close(options.StatChan)
containerFunc := ic.Libpod.GetRunningContainers
switch {
case len(namesOrIds) > 0: