From 033743cbee180d9a4149f574375cc1201e573d06 Mon Sep 17 00:00:00 2001 From: Qi Wang Date: Wed, 10 Jun 2020 14:14:01 -0400 Subject: Fix -f logs follow with stopped container Fix -f logs follow with stopped container. Close #6531 Signed-off-by: Qi Wang --- libpod/container_log.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'libpod') diff --git a/libpod/container_log.go b/libpod/container_log.go index c3a84d048..36b89f1ae 100644 --- a/libpod/container_log.go +++ b/libpod/container_log.go @@ -30,6 +30,13 @@ func (c *Container) ReadLog(options *logs.LogOptions, logChannel chan *logs.LogL } func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *logs.LogLine) error { + state, err := c.State() + if err != nil { + return err + } + if state != define.ContainerStateRunning && state != define.ContainerStatePaused { + options.Follow = false + } t, tailLog, err := logs.GetLogFile(c.LogPath(), options) if err != nil { // If the log file does not exist, this is not fatal. @@ -69,6 +76,14 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l if nll.Since(options.Since) { logChannel <- nll } + state, err := c.State() + if err != nil { + logrus.Error(err) + break + } + if options.Follow && state != define.ContainerStateRunning && state != define.ContainerStatePaused { + t.Kill(err) + } } options.WaitGroup.Done() }() -- cgit v1.2.3-54-g00ecf From 9ac115e691d45ec565d0152c630e97c8081e1a71 Mon Sep 17 00:00:00 2001 From: jgallucci32 Date: Thu, 11 Jun 2020 22:03:02 -0700 Subject: Fix -f logs to stop when a container exits Fixes an issue with the previous PR where a container would exit while following logs and the log tail continued to follow. This creates a subroutine which checks the state of the container and instructs the tailLog to stop when it reaches EOF. Tested the following conditions: * Tail and follow logs of running container * Tail and follow logs of stopped container * Tail and follow logs of running container which exits after some time Signed-off-by: jgallucci32 --- libpod/container_log.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'libpod') diff --git a/libpod/container_log.go b/libpod/container_log.go index 36b89f1ae..f73a5ffbf 100644 --- a/libpod/container_log.go +++ b/libpod/container_log.go @@ -2,6 +2,7 @@ package libpod import ( "os" + "time" "github.com/containers/libpod/libpod/define" "github.com/containers/libpod/libpod/logs" @@ -30,13 +31,6 @@ func (c *Container) ReadLog(options *logs.LogOptions, logChannel chan *logs.LogL } func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *logs.LogLine) error { - state, err := c.State() - if err != nil { - return err - } - if state != define.ContainerStateRunning && state != define.ContainerStatePaused { - options.Follow = false - } t, tailLog, err := logs.GetLogFile(c.LogPath(), options) if err != nil { // If the log file does not exist, this is not fatal. @@ -76,16 +70,25 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l if nll.Since(options.Since) { logChannel <- nll } - state, err := c.State() - if err != nil { - logrus.Error(err) - break - } - if options.Follow && state != define.ContainerStateRunning && state != define.ContainerStatePaused { - t.Kill(err) - } } options.WaitGroup.Done() }() + // Check if container is still running or paused + go func() { + if options.Follow { + for { + state, err := c.State() + if err != nil { + logrus.Error(err) + break + } + if state != define.ContainerStateRunning && state != define.ContainerStatePaused { + t.StopAtEOF() + break + } + time.Sleep(1 * time.Second) + } + } + }() return nil } -- cgit v1.2.3-54-g00ecf From e7143525011610af8e487e3d9ae63e533b27e4fe Mon Sep 17 00:00:00 2001 From: jgallucci32 Date: Fri, 12 Jun 2020 11:04:36 -0700 Subject: Changed from t.StopAtEOF() to t.Stop() and added error check Signed-off-by: jgallucci32 --- libpod/container_log.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'libpod') diff --git a/libpod/container_log.go b/libpod/container_log.go index f73a5ffbf..da3ed3113 100644 --- a/libpod/container_log.go +++ b/libpod/container_log.go @@ -83,7 +83,11 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l break } if state != define.ContainerStateRunning && state != define.ContainerStatePaused { - t.StopAtEOF() + err := t.Stop() + if err != nil { + logrus.Error(err) + break + } break } time.Sleep(1 * time.Second) -- cgit v1.2.3-54-g00ecf From d514e3c09705c64b6cbcef70216a6edea9c8515a Mon Sep 17 00:00:00 2001 From: jgallucci32 Date: Fri, 12 Jun 2020 12:59:58 -0700 Subject: Do not print error message when container does not exist This fixes a condition when a container is removed while following the logs and prints an error when the container is removed forcefully. Signed-off-by: jgallucci32 --- libpod/container_log.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'libpod') diff --git a/libpod/container_log.go b/libpod/container_log.go index da3ed3113..769f776d0 100644 --- a/libpod/container_log.go +++ b/libpod/container_log.go @@ -78,9 +78,11 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l if options.Follow { for { state, err := c.State() - if err != nil { + if err != nil && errors.Cause(err) != define.ErrNoSuchCtr { logrus.Error(err) break + } else if err != nil { + break } if state != define.ContainerStateRunning && state != define.ContainerStatePaused { err := t.Stop() -- cgit v1.2.3-54-g00ecf From 6d9863e7738719a100ac56c1e646c169e4c0bd11 Mon Sep 17 00:00:00 2001 From: jgallucci32 Date: Mon, 15 Jun 2020 07:16:12 -0700 Subject: Remove redundant break in for loop. Remove redundant `break` call in for loop. Co-authored-by: Qi Wang Signed-off-by: jgallucci32 --- libpod/container_log.go | 1 - 1 file changed, 1 deletion(-) (limited to 'libpod') diff --git a/libpod/container_log.go b/libpod/container_log.go index 769f776d0..39c395fe6 100644 --- a/libpod/container_log.go +++ b/libpod/container_log.go @@ -88,7 +88,6 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l err := t.Stop() if err != nil { logrus.Error(err) - break } break } -- cgit v1.2.3-54-g00ecf