Codebase list golang-github-go-kit-kit / e10c4c8
log: SyncLogger: defer mutex unlocks for panic safety (#974) Peter Bourgon authored 4 years ago GitHub committed 4 years ago
2 changed file(s) with 24 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
6464 // progress, the calling goroutine blocks until the syncWriter is available.
6565 func (w *syncWriter) Write(p []byte) (n int, err error) {
6666 w.Lock()
67 n, err = w.Writer.Write(p)
68 w.Unlock()
69 return n, err
67 defer w.Unlock()
68 return w.Writer.Write(p)
7069 }
7170
7271 // fdWriter is an io.Writer that also has an Fd method. The most common
8685 // progress, the calling goroutine blocks until the fdSyncWriter is available.
8786 func (w *fdSyncWriter) Write(p []byte) (n int, err error) {
8887 w.Lock()
89 n, err = w.fdWriter.Write(p)
90 w.Unlock()
91 return n, err
88 defer w.Unlock()
89 return w.fdWriter.Write(p)
9290 }
9391
9492 // syncLogger provides concurrent safe logging for another Logger.
109107 // progress, the calling goroutine blocks until the syncLogger is available.
110108 func (l *syncLogger) Log(keyvals ...interface{}) error {
111109 l.mu.Lock()
112 err := l.logger.Log(keyvals...)
113 l.mu.Unlock()
114 return err
110 defer l.mu.Unlock()
111 return l.logger.Log(keyvals...)
115112 }
8080 t.Error("NewSyncWriter does not pass through Fd method")
8181 }
8282 }
83
84 func TestSyncLoggerPanic(t *testing.T) {
85 var logger log.Logger
86 logger = log.LoggerFunc(func(...interface{}) error { panic("!") })
87 logger = log.NewSyncLogger(logger)
88
89 f := func() {
90 defer func() {
91 if x := recover(); x != nil {
92 t.Log(x)
93 }
94 }()
95 logger.Log("hello", "world")
96 }
97
98 f()
99 f() // without defer Unlock, this one can deadlock
100 }