From 8691607ade23f9d2d282e0edc0d88d481451b5fb Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Tue, 21 Jun 2016 18:36:19 -0700 Subject: [PATCH] Fixing file handle leak for "docker logs" If "docker logs" was used on an offline container, the logger is leaked, leaving it up to the finalizer to close the file handle, which could block removal of the container. Further, the json file logger could leak an open handle if the logs are read without follow due to an early return without a close. This change addresses both cases. Signed-off-by: Stefan J. Wernli (cherry picked from commit 54f11b84d218c1a7ff4ab4e2148819265da213bc) --- daemon/logger/jsonfilelog/read.go | 3 +++ daemon/logs.go | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/daemon/logger/jsonfilelog/read.go b/daemon/logger/jsonfilelog/read.go index 1e197f3e84..bea83ddf14 100644 --- a/daemon/logger/jsonfilelog/read.go +++ b/daemon/logger/jsonfilelog/read.go @@ -77,6 +77,9 @@ func (l *JSONFileLogger) readLogs(logWatcher *logger.LogWatcher, config logger.R } if !config.Follow { + if err := latestFile.Close(); err != nil { + logrus.Errorf("Error closing file: %v", err) + } return } diff --git a/daemon/logs.go b/daemon/logs.go index 1ad654e101..e0da46d9e4 100644 --- a/daemon/logs.go +++ b/daemon/logs.go @@ -87,6 +87,13 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c if !ok { logrus.Debug("logs: end stream") logs.Close() + if cLog != container.LogDriver { + // Since the logger isn't cached in the container, which occurs if it is running, it + // must get explicitly closed here to avoid leaking it and any file handles it has. + if err := cLog.Close(); err != nil { + logrus.Errorf("Error closing logger: %v", err) + } + } return nil } logLine := msg.Line