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/container_log.go') 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