From 5e354ed5337b0187ae72013a05d3567d6a2dc548 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Fri, 4 May 2018 12:41:42 -0400 Subject: [PATCH] Fix race conditions in logs API Closing the log driver was in a defer meanwhile logs are collected asyncronously, so the log driver was being closed before reads were actually finished. Signed-off-by: Brian Goff Upstream-commit: 2c252a48c252749d41079cf8c450d00a5c18296e Component: engine --- components/engine/daemon/logs.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/components/engine/daemon/logs.go b/components/engine/daemon/logs.go index 8ef8d00d23..37ca4caf63 100644 --- a/components/engine/daemon/logs.go +++ b/components/engine/daemon/logs.go @@ -22,7 +22,7 @@ import ( // // if it returns nil, the config channel will be active and return log // messages until it runs out or the context is canceled. -func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, config *types.ContainerLogsOptions) (<-chan *backend.LogMessage, bool, error) { +func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, config *types.ContainerLogsOptions) (messages <-chan *backend.LogMessage, isTTY bool, retErr error) { lg := logrus.WithFields(logrus.Fields{ "module": "daemon", "method": "(*Daemon).ContainerLogs", @@ -51,8 +51,10 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c } if cLogCreated { defer func() { - if err = cLog.Close(); err != nil { - logrus.Errorf("Error closing logger: %v", err) + if retErr != nil { + if err = cLog.Close(); err != nil { + logrus.Errorf("Error closing logger: %v", err) + } } }() } @@ -101,6 +103,13 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c // this goroutine functions as a shim between the logger and the caller. messageChan := make(chan *backend.LogMessage, 1) go func() { + if cLogCreated { + defer func() { + if err = cLog.Close(); err != nil { + logrus.Errorf("Error closing logger: %v", err) + } + }() + } // set up some defers defer logs.Close()