浏览代码

Merge pull request #26596 from drakenator/26384-healthcheck-race

Prevent stdout / stderr race condition in limitedBuffer.
Tõnis Tiigi 8 年之前
父节点
当前提交
4c82365cdb
共有 1 个文件被更改,包括 8 次插入0 次删除
  1. 8 0
      daemon/health.go

+ 8 - 0
daemon/health.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"fmt"
 	"runtime"
 	"runtime"
 	"strings"
 	"strings"
+	"sync"
 	"time"
 	"time"
 
 
 	"golang.org/x/net/context"
 	"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.
 // Buffer up to maxOutputLen bytes. Further data is discarded.
 type limitedBuffer struct {
 type limitedBuffer struct {
 	buf       bytes.Buffer
 	buf       bytes.Buffer
+	mu        sync.Mutex
 	truncated bool // indicates that data has been lost
 	truncated bool // indicates that data has been lost
 }
 }
 
 
 // Append to limitedBuffer while there is room.
 // Append to limitedBuffer while there is room.
 func (b *limitedBuffer) Write(data []byte) (int, error) {
 func (b *limitedBuffer) Write(data []byte) (int, error) {
+	b.mu.Lock()
+	defer b.mu.Unlock()
+
 	bufLen := b.buf.Len()
 	bufLen := b.buf.Len()
 	dataLen := len(data)
 	dataLen := len(data)
 	keep := min(maxOutputLen-bufLen, dataLen)
 	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.
 // The contents of the buffer, with "..." appended if it overflowed.
 func (b *limitedBuffer) String() string {
 func (b *limitedBuffer) String() string {
+	b.mu.Lock()
+	defer b.mu.Unlock()
+
 	out := b.buf.String()
 	out := b.buf.String()
 	if b.truncated {
 	if b.truncated {
 		out = out + "..."
 		out = out + "..."