mirror of https://github.com/containers/podman.git
container stats: fix --no-stream race
Fix a race in `podman container stats` by waiting for the client to consume the data in the channel. This requires a `sync.WaitGroup` (or semaphore) in the client and to also close the channel the backend. Fixes: #6405 Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
parent
119e13d4bc
commit
3cef598fc2
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
|
@ -111,14 +112,20 @@ func stats(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
statsOptions.StatChan = make(chan []*define.ContainerStats, 1)
|
statsOptions.StatChan = make(chan []*define.ContainerStats, 1)
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
for reports := range statsOptions.StatChan {
|
for reports := range statsOptions.StatChan {
|
||||||
if err := outputStats(reports); err != nil {
|
if err := outputStats(reports); err != nil {
|
||||||
logrus.Error(err)
|
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 {
|
func outputStats(reports []*define.ContainerStats) error {
|
||||||
|
|
|
||||||
|
|
@ -1087,6 +1087,7 @@ func (ic *ContainerEngine) Shutdown(_ context.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ic *ContainerEngine) ContainerStats(ctx context.Context, namesOrIds []string, options entities.ContainerStatsOptions) error {
|
func (ic *ContainerEngine) ContainerStats(ctx context.Context, namesOrIds []string, options entities.ContainerStatsOptions) error {
|
||||||
|
defer close(options.StatChan)
|
||||||
containerFunc := ic.Libpod.GetRunningContainers
|
containerFunc := ic.Libpod.GetRunningContainers
|
||||||
switch {
|
switch {
|
||||||
case len(namesOrIds) > 0:
|
case len(namesOrIds) > 0:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue