mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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:
parent
4122443aa7
commit
2e440580bc
2 changed files with 18 additions and 10 deletions
|
@ -137,7 +137,11 @@ func tailFile(f io.ReadSeeker, logWatcher *logger.LogWatcher, tail int, since ti
|
||||||
if !since.IsZero() && msg.Timestamp.Before(since) {
|
if !since.IsZero() && msg.Timestamp.Before(since) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
logWatcher.Msg <- msg
|
select {
|
||||||
|
case <-logWatcher.WatchClose():
|
||||||
|
return
|
||||||
|
case logWatcher.Msg <- msg:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,18 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c
|
||||||
Follow: follow,
|
Follow: follow,
|
||||||
}
|
}
|
||||||
logs := logReader.ReadLogs(readConfig)
|
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)
|
wf := ioutils.NewWriteFlusher(config.OutStream)
|
||||||
defer wf.Close()
|
defer wf.Close()
|
||||||
|
@ -81,19 +93,11 @@ func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, c
|
||||||
logrus.Errorf("Error streaming logs: %v", err)
|
logrus.Errorf("Error streaming logs: %v", err)
|
||||||
return nil
|
return nil
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
logs.Close()
|
logrus.Debugf("logs: end stream, ctx is done: %v", ctx.Err())
|
||||||
return nil
|
return nil
|
||||||
case msg, ok := <-logs.Msg:
|
case msg, ok := <-logs.Msg:
|
||||||
if !ok {
|
if !ok {
|
||||||
logrus.Debug("logs: end stream")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
logLine := msg.Line
|
logLine := msg.Line
|
||||||
|
|
Loading…
Add table
Reference in a new issue