Browse code

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 <swernli@microsoft.com>

Stefan J. Wernli authored on 2016/06/22 10:36:19
Showing 2 changed files
... ...
@@ -77,6 +77,9 @@ func (l *JSONFileLogger) readLogs(logWatcher *logger.LogWatcher, config logger.R
77 77
 	}
78 78
 
79 79
 	if !config.Follow {
80
+		if err := latestFile.Close(); err != nil {
81
+			logrus.Errorf("Error closing file: %v", err)
82
+		}
80 83
 		return
81 84
 	}
82 85
 
... ...
@@ -87,6 +87,13 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c
87 87
 			if !ok {
88 88
 				logrus.Debug("logs: end stream")
89 89
 				logs.Close()
90
+				if cLog != container.LogDriver {
91
+					// Since the logger isn't cached in the container, which occurs if it is running, it
92
+					// must get explicitly closed here to avoid leaking it and any file handles it has.
93
+					if err := cLog.Close(); err != nil {
94
+						logrus.Errorf("Error closing logger: %v", err)
95
+					}
96
+				}
90 97
 				return nil
91 98
 			}
92 99
 			logLine := msg.Line