2018-04-05 12:39:28 -04:00
|
|
|
package loggerutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"io"
|
2018-08-27 16:53:23 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-04-05 12:39:28 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
|
|
|
"github.com/docker/docker/pkg/tailfile"
|
|
|
|
"gotest.tools/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTailFiles(t *testing.T) {
|
|
|
|
s1 := strings.NewReader("Hello.\nMy name is Inigo Montoya.\n")
|
|
|
|
s2 := strings.NewReader("I'm serious.\nDon't call me Shirley!\n")
|
|
|
|
s3 := strings.NewReader("Roads?\nWhere we're going we don't need roads.\n")
|
|
|
|
|
|
|
|
files := []SizeReaderAt{s1, s2, s3}
|
|
|
|
watcher := logger.NewLogWatcher()
|
|
|
|
createDecoder := func(r io.Reader) func() (*logger.Message, error) {
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
return func() (*logger.Message, error) {
|
|
|
|
if !scanner.Scan() {
|
|
|
|
return nil, scanner.Err()
|
|
|
|
}
|
|
|
|
// some comment
|
|
|
|
return &logger.Message{Line: scanner.Bytes(), Timestamp: time.Now()}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tailReader := func(ctx context.Context, r SizeReaderAt, lines int) (io.Reader, int, error) {
|
|
|
|
return tailfile.NewTailReader(ctx, r, lines)
|
|
|
|
}
|
|
|
|
|
|
|
|
for desc, config := range map[string]logger.ReadConfig{} {
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
|
|
|
started := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
close(started)
|
|
|
|
tailFiles(files, watcher, createDecoder, tailReader, config)
|
|
|
|
}()
|
|
|
|
<-started
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
config := logger.ReadConfig{Tail: 2}
|
|
|
|
started := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
close(started)
|
|
|
|
tailFiles(files, watcher, createDecoder, tailReader, config)
|
|
|
|
}()
|
|
|
|
<-started
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(60 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for tail line")
|
|
|
|
case err := <-watcher.Err:
|
|
|
|
assert.Assert(t, err)
|
|
|
|
case msg := <-watcher.Msg:
|
|
|
|
assert.Assert(t, msg != nil)
|
|
|
|
assert.Assert(t, string(msg.Line) == "Roads?", string(msg.Line))
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(60 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for tail line")
|
|
|
|
case err := <-watcher.Err:
|
|
|
|
assert.Assert(t, err)
|
|
|
|
case msg := <-watcher.Msg:
|
|
|
|
assert.Assert(t, msg != nil)
|
|
|
|
assert.Assert(t, string(msg.Line) == "Where we're going we don't need roads.", string(msg.Line))
|
|
|
|
}
|
|
|
|
}
|
2018-08-27 16:53:23 -04:00
|
|
|
|
daemon.ContainerLogs(): fix resource leak on follow
When daemon.ContainerLogs() is called with options.follow=true
(as in "docker logs --follow"), the "loggerutils.followLogs()"
function never returns (even then the logs consumer is gone).
As a result, all the resources associated with it (including
an opened file descriptor for the log file being read, two FDs
for a pipe, and two FDs for inotify watch) are never released.
If this is repeated (such as by running "docker logs --follow"
and pressing Ctrl-C a few times), this results in DoS caused by
either hitting the limit of inotify watches, or the limit of
opened files. The only cure is daemon restart.
Apparently, what happens is:
1. logs producer (a container) is gone, calling (*LogWatcher).Close()
for all its readers (daemon/logger/jsonfilelog/jsonfilelog.go:175).
2. WatchClose() is properly handled by a dedicated goroutine in
followLogs(), cancelling the context.
3. Upon receiving the ctx.Done(), the code in followLogs()
(daemon/logger/loggerutils/logfile.go#L626-L638) keeps to
send messages _synchronously_ (which is OK for now).
4. Logs consumer is gone (Ctrl-C is pressed on a terminal running
"docker logs --follow"). Method (*LogWatcher).Close() is properly
called (see daemon/logs.go:114). Since it was called before and
due to to once.Do(), nothing happens (which is kinda good, as
otherwise it will panic on closing a closed channel).
5. A goroutine (see item 3 above) keeps sending log messages
synchronously to the logWatcher.Msg channel. Since the
channel reader is gone, the channel send operation blocks forever,
and resource cleanup set up in defer statements at the beginning
of followLogs() never happens.
Alas, the fix is somewhat complicated:
1. Distinguish between close from logs producer and logs consumer.
To that effect,
- yet another channel is added to LogWatcher();
- {Watch,}Close() are renamed to {Watch,}ProducerGone();
- {Watch,}ConsumerGone() are added;
*NOTE* that ProducerGone()/WatchProducerGone() pair is ONLY needed
in order to stop ConsumerLogs(follow=true) when a container is stopped;
otherwise we're not interested in it. In other words, we're only
using it in followLogs().
2. Code that was doing (logWatcher*).Close() is modified to either call
ProducerGone() or ConsumerGone(), depending on the context.
3. Code that was waiting for WatchClose() is modified to wait for
either ConsumerGone() or ProducerGone(), or both, depending on the
context.
4. followLogs() are modified accordingly:
- context cancellation is happening on WatchProducerGone(),
and once it's received the FileWatcher is closed and waitRead()
returns errDone on EOF (i.e. log rotation handling logic is disabled);
- due to this, code that was writing synchronously to logWatcher.Msg
can be and is removed as the code above it handles this case;
- function returns once ConsumerGone is received, freeing all the
resources -- this is the bugfix itself.
While at it,
1. Let's also remove the ctx usage to simplify the code a bit.
It was introduced by commit a69a59ffc7e3d ("Decouple removing the
fileWatcher from reading") in order to fix a bug. The bug was actually
a deadlock in fsnotify, and the fix was just a workaround. Since then
the fsnofify bug has been fixed, and a new fsnotify was vendored in.
For more details, please see
https://github.com/moby/moby/pull/27782#issuecomment-416794490
2. Since `(*filePoller).Close()` is fixed to remove all the files
being watched, there is no need to explicitly call
fileWatcher.Remove(name) anymore, so get rid of the extra code.
Should fix https://github.com/moby/moby/issues/37391
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-08-01 00:03:55 -04:00
|
|
|
func TestFollowLogsConsumerGone(t *testing.T) {
|
2018-08-27 16:53:23 -04:00
|
|
|
lw := logger.NewLogWatcher()
|
|
|
|
|
|
|
|
f, err := ioutil.TempFile("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer func() {
|
|
|
|
f.Close()
|
|
|
|
os.Remove(f.Name())
|
|
|
|
}()
|
|
|
|
|
|
|
|
makeDecoder := func(rdr io.Reader) func() (*logger.Message, error) {
|
|
|
|
return func() (*logger.Message, error) {
|
|
|
|
return &logger.Message{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
followLogsDone := make(chan struct{})
|
|
|
|
var since, until time.Time
|
|
|
|
go func() {
|
|
|
|
followLogs(f, lw, make(chan interface{}), makeDecoder, since, until)
|
|
|
|
close(followLogsDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-lw.Msg:
|
|
|
|
case err := <-lw.Err:
|
|
|
|
assert.NilError(t, err)
|
|
|
|
case <-followLogsDone:
|
|
|
|
t.Fatal("follow logs finished unexpectedly")
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for log message")
|
|
|
|
}
|
|
|
|
|
daemon.ContainerLogs(): fix resource leak on follow
When daemon.ContainerLogs() is called with options.follow=true
(as in "docker logs --follow"), the "loggerutils.followLogs()"
function never returns (even then the logs consumer is gone).
As a result, all the resources associated with it (including
an opened file descriptor for the log file being read, two FDs
for a pipe, and two FDs for inotify watch) are never released.
If this is repeated (such as by running "docker logs --follow"
and pressing Ctrl-C a few times), this results in DoS caused by
either hitting the limit of inotify watches, or the limit of
opened files. The only cure is daemon restart.
Apparently, what happens is:
1. logs producer (a container) is gone, calling (*LogWatcher).Close()
for all its readers (daemon/logger/jsonfilelog/jsonfilelog.go:175).
2. WatchClose() is properly handled by a dedicated goroutine in
followLogs(), cancelling the context.
3. Upon receiving the ctx.Done(), the code in followLogs()
(daemon/logger/loggerutils/logfile.go#L626-L638) keeps to
send messages _synchronously_ (which is OK for now).
4. Logs consumer is gone (Ctrl-C is pressed on a terminal running
"docker logs --follow"). Method (*LogWatcher).Close() is properly
called (see daemon/logs.go:114). Since it was called before and
due to to once.Do(), nothing happens (which is kinda good, as
otherwise it will panic on closing a closed channel).
5. A goroutine (see item 3 above) keeps sending log messages
synchronously to the logWatcher.Msg channel. Since the
channel reader is gone, the channel send operation blocks forever,
and resource cleanup set up in defer statements at the beginning
of followLogs() never happens.
Alas, the fix is somewhat complicated:
1. Distinguish between close from logs producer and logs consumer.
To that effect,
- yet another channel is added to LogWatcher();
- {Watch,}Close() are renamed to {Watch,}ProducerGone();
- {Watch,}ConsumerGone() are added;
*NOTE* that ProducerGone()/WatchProducerGone() pair is ONLY needed
in order to stop ConsumerLogs(follow=true) when a container is stopped;
otherwise we're not interested in it. In other words, we're only
using it in followLogs().
2. Code that was doing (logWatcher*).Close() is modified to either call
ProducerGone() or ConsumerGone(), depending on the context.
3. Code that was waiting for WatchClose() is modified to wait for
either ConsumerGone() or ProducerGone(), or both, depending on the
context.
4. followLogs() are modified accordingly:
- context cancellation is happening on WatchProducerGone(),
and once it's received the FileWatcher is closed and waitRead()
returns errDone on EOF (i.e. log rotation handling logic is disabled);
- due to this, code that was writing synchronously to logWatcher.Msg
can be and is removed as the code above it handles this case;
- function returns once ConsumerGone is received, freeing all the
resources -- this is the bugfix itself.
While at it,
1. Let's also remove the ctx usage to simplify the code a bit.
It was introduced by commit a69a59ffc7e3d ("Decouple removing the
fileWatcher from reading") in order to fix a bug. The bug was actually
a deadlock in fsnotify, and the fix was just a workaround. Since then
the fsnofify bug has been fixed, and a new fsnotify was vendored in.
For more details, please see
https://github.com/moby/moby/pull/27782#issuecomment-416794490
2. Since `(*filePoller).Close()` is fixed to remove all the files
being watched, there is no need to explicitly call
fileWatcher.Remove(name) anymore, so get rid of the extra code.
Should fix https://github.com/moby/moby/issues/37391
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-08-01 00:03:55 -04:00
|
|
|
lw.ConsumerGone()
|
2018-08-27 16:53:23 -04:00
|
|
|
select {
|
|
|
|
case <-followLogsDone:
|
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for followLogs() to finish")
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 22:58:47 -04:00
|
|
|
|
|
|
|
func TestFollowLogsProducerGone(t *testing.T) {
|
|
|
|
lw := logger.NewLogWatcher()
|
|
|
|
|
|
|
|
f, err := ioutil.TempFile("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
|
|
|
|
var sent, received, closed int
|
|
|
|
makeDecoder := func(rdr io.Reader) func() (*logger.Message, error) {
|
|
|
|
return func() (*logger.Message, error) {
|
|
|
|
if closed == 1 {
|
|
|
|
closed++
|
|
|
|
t.Logf("logDecode() closed after sending %d messages\n", sent)
|
|
|
|
return nil, io.EOF
|
|
|
|
} else if closed > 1 {
|
|
|
|
t.Fatal("logDecode() called after closing!")
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
sent++
|
|
|
|
return &logger.Message{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var since, until time.Time
|
|
|
|
|
|
|
|
followLogsDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
followLogs(f, lw, make(chan interface{}), makeDecoder, since, until)
|
|
|
|
close(followLogsDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// read 1 message
|
|
|
|
select {
|
|
|
|
case <-lw.Msg:
|
|
|
|
received++
|
|
|
|
case err := <-lw.Err:
|
|
|
|
assert.NilError(t, err)
|
|
|
|
case <-followLogsDone:
|
|
|
|
t.Fatal("followLogs() finished unexpectedly")
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for log message")
|
|
|
|
}
|
|
|
|
|
|
|
|
// "stop" the "container"
|
|
|
|
closed = 1
|
|
|
|
lw.ProducerGone()
|
|
|
|
|
|
|
|
// should receive all the messages sent
|
|
|
|
readDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(readDone)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-lw.Msg:
|
|
|
|
received++
|
|
|
|
if received == sent {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case err := <-lw.Err:
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-readDone:
|
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
t.Fatalf("timeout waiting for log messages to be read (sent: %d, received: %d", sent, received)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("messages sent: %d, received: %d", sent, received)
|
|
|
|
|
|
|
|
// followLogs() should be done by now
|
|
|
|
select {
|
|
|
|
case <-followLogsDone:
|
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for followLogs() to finish")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-lw.WatchConsumerGone():
|
|
|
|
t.Fatal("consumer should not have exited")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|