Skip to content

Commit e1348a0

Browse files
committed
bugfix: fix lock competition between Stats and Start interface when container is not running
Signed-off-by: Michael Wan <[email protected]>
1 parent 9b47561 commit e1348a0

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

daemon/mgr/container_stats.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,15 @@ func (mgr *ContainerManager) StreamStats(ctx context.Context, name string, confi
5959
if err != nil {
6060
return err
6161
}
62-
containerStat := toContainerStats(metrics.Timestamp, stats)
6362

64-
if err := enc.Encode(containerStat); err != nil {
65-
return err
63+
// metrics may be nil if the container is not running,
64+
// so just ignore it and try get the metrics next time.
65+
if metrics != nil {
66+
containerStat := toContainerStats(metrics.Timestamp, stats)
67+
68+
if err := enc.Encode(containerStat); err != nil {
69+
return err
70+
}
6671
}
6772

6873
time.Sleep(DefaultStatsInterval)
@@ -81,6 +86,11 @@ func (mgr *ContainerManager) Stats(ctx context.Context, name string) (*container
8186
ID := c.ID
8287
c.Unlock()
8388

89+
// only get metrics when the container is running
90+
if !(c.IsRunning() || c.IsPaused()) {
91+
return nil, nil, nil
92+
}
93+
8494
metric, err := mgr.Client.ContainerStats(ctx, ID)
8595
if err != nil {
8696
return nil, nil, err

0 commit comments

Comments
 (0)