From bd8661e8fafd5f80ce12fa966b9f8e990b7d7714 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Wed, 18 Mar 2015 12:52:42 -0700 Subject: [PATCH] Protect jsonfilelog writes with mutex Signed-off-by: Alexander Morozov --- daemon/logger/jsonfilelog/jsonfilelog.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/daemon/logger/jsonfilelog/jsonfilelog.go b/daemon/logger/jsonfilelog/jsonfilelog.go index c1d1e0f84e..faa6bf92e2 100644 --- a/daemon/logger/jsonfilelog/jsonfilelog.go +++ b/daemon/logger/jsonfilelog/jsonfilelog.go @@ -3,6 +3,7 @@ package jsonfilelog import ( "bytes" "os" + "sync" "github.com/docker/docker/daemon/logger" "github.com/docker/docker/pkg/jsonlog" @@ -12,7 +13,8 @@ import ( // JSON objects to file type JSONFileLogger struct { buf *bytes.Buffer - f *os.File // store for closing + f *os.File // store for closing + mu sync.Mutex // protects buffer } // New creates new JSONFileLogger which writes to filename @@ -29,13 +31,20 @@ func New(filename string) (logger.Logger, error) { // Log converts logger.Message to jsonlog.JSONLog and serializes it to file func (l *JSONFileLogger) Log(msg *logger.Message) error { + l.mu.Lock() + defer l.mu.Unlock() err := (&jsonlog.JSONLog{Log: string(msg.Line) + "\n", Stream: msg.Source, Created: msg.Timestamp}).MarshalJSONBuf(l.buf) if err != nil { return err } l.buf.WriteByte('\n') _, err = l.buf.WriteTo(l.f) - return err + if err != nil { + // this buffer is screwed, replace it with another to avoid races + l.buf = bytes.NewBuffer(nil) + return err + } + return nil } // Close closes underlying file