Merge pull request #30223 from mlaventure/fix-logwatcher-deadlock

Close logwatcher on context cancellation
(cherry picked from commit 0566f3ffc4)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Tõnis Tiigi 2017-01-20 10:14:37 -08:00 committed by Sebastiaan van Stijn
parent 4122443aa7
commit 2e440580bc
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 18 additions and 10 deletions

View File

@ -137,7 +137,11 @@ func tailFile(f io.ReadSeeker, logWatcher *logger.LogWatcher, tail int, since ti
if !since.IsZero() && msg.Timestamp.Before(since) {
continue
}
logWatcher.Msg <- msg
select {
case <-logWatcher.WatchClose():
return
case logWatcher.Msg <- msg:
}
}
}

View File

@ -61,6 +61,18 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c
Follow: follow,
}
logs := logReader.ReadLogs(readConfig)
// Close logWatcher on exit
defer func() {
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)
}
}
}()
wf := ioutils.NewWriteFlusher(config.OutStream)
defer wf.Close()
@ -81,19 +93,11 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c
logrus.Errorf("Error streaming logs: %v", err)
return nil
case <-ctx.Done():
logs.Close()
logrus.Debugf("logs: end stream, ctx is done: %v", ctx.Err())
return nil
case msg, ok := <-logs.Msg:
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