From c3319445aa0d891921f0236ff5bd7a8d879be4f8 Mon Sep 17 00:00:00 2001 From: Stephen Drake Date: Mon, 12 Sep 2016 19:24:07 +0200 Subject: [PATCH] Prevent stdout / stderr race condition in limitedBuffer. Signed-off-by: Stephen Drake --- daemon/health.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/daemon/health.go b/daemon/health.go index 9f7c76ee14..87d6a6a4e3 100644 --- a/daemon/health.go +++ b/daemon/health.go @@ -5,6 +5,7 @@ import ( "fmt" "runtime" "strings" + "sync" "time" "golang.org/x/net/context" @@ -271,11 +272,15 @@ func (d *Daemon) stopHealthchecks(c *container.Container) { // Buffer up to maxOutputLen bytes. Further data is discarded. type limitedBuffer struct { buf bytes.Buffer + mu sync.Mutex truncated bool // indicates that data has been lost } // Append to limitedBuffer while there is room. func (b *limitedBuffer) Write(data []byte) (int, error) { + b.mu.Lock() + defer b.mu.Unlock() + bufLen := b.buf.Len() dataLen := len(data) keep := min(maxOutputLen-bufLen, dataLen) @@ -290,6 +295,9 @@ func (b *limitedBuffer) Write(data []byte) (int, error) { // The contents of the buffer, with "..." appended if it overflowed. func (b *limitedBuffer) String() string { + b.mu.Lock() + defer b.mu.Unlock() + out := b.buf.String() if b.truncated { out = out + "..."