Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cherrypick-1.1: util/log: Avoid infinite recursion out of disk errors cause an exit #21804

Merged
merged 1 commit into from Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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