Skip to content

Commit

Permalink
Merge pull request #21804 from a-robinson/cherrypick_21768
Browse files Browse the repository at this point in the history
cherrypick-1.1: util/log: Avoid infinite recursion out of disk errors cause an exit
  • Loading branch information
a-robinson committed Jan 26, 2018
2 parents 5f163c7 + ba3b353 commit d0e5e6b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/util/log/clog.go
Expand Up @@ -889,10 +889,20 @@ var logExitFunc func(error)
// point in hanging around. l.mu is held.
func (l *loggingT) exitLocked(err error) {
l.mu.AssertHeld()

// Either stderr or our log file is broken. Try writing the error to both
// streams in the hope that one still works or else the user will have no idea
// why we crashed.
for _, w := range []io.Writer{OrigStderr, l.file} {
outputs := make([]io.Writer, 2)
outputs[0] = OrigStderr
if f, ok := l.file.(*syncBuffer); ok {
// Don't call syncBuffer's Write method, because it can call back into
// exitLocked. Go directly to syncBuffer's underlying writer.
outputs[1] = f.Writer
} else {
outputs[1] = l.file
}
for _, w := range outputs {
if w == nil {
continue
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/util/log/clog_test.go
Expand Up @@ -28,6 +28,7 @@ import (
"reflect"
"regexp"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -775,6 +776,36 @@ func TestFileSeverityFilter(t *testing.T) {
}
}

type outOfSpaceWriter struct{}

func (w *outOfSpaceWriter) Write([]byte) (int, error) {
return 0, fmt.Errorf("no space left on device")
}

func TestExitOnFullDisk(t *testing.T) {
oldLogExitFunc := logExitFunc
logExitFunc = nil
defer func() { logExitFunc = oldLogExitFunc }()

var exited sync.WaitGroup
exited.Add(1)
l := &loggingT{
exitFunc: func(int) {
exited.Done()
},
}
l.file = &syncBuffer{
logger: l,
Writer: bufio.NewWriterSize(&outOfSpaceWriter{}, 1),
}

l.mu.Lock()
l.exitLocked(fmt.Errorf("out of space"))
l.mu.Unlock()

exited.Wait()
}

func BenchmarkHeader(b *testing.B) {
for i := 0; i < b.N; i++ {
buf := formatHeader(Severity_INFO, timeutil.Now(), 200, "file.go", 100, nil)
Expand Down

0 comments on commit d0e5e6b

Please sign in to comment.