From 96acea6330e9c8c660f0312cf08061f427c589d0 Mon Sep 17 00:00:00 2001 From: Alex Barganier Date: Tue, 22 Aug 2023 14:38:29 -0400 Subject: [PATCH] pkg/util/log: clean up some unsafe lock usage in bufferedSink There are multiple undeferred lock usages in the bufferedSink beyond just the one that surrounds the call to `appendMsg`. This small patch cleans these usages up to use `defer` instead. Release note: none --- pkg/util/log/buffered_sink.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/util/log/buffered_sink.go b/pkg/util/log/buffered_sink.go index b8f527ddb796..84079b8fbb45 100644 --- a/pkg/util/log/buffered_sink.go +++ b/pkg/util/log/buffered_sink.go @@ -271,8 +271,8 @@ func (bs *bufferedSink) output(b []byte, opts sinkOutputOptions) error { if bs.mu.timer == nil && bs.maxStaleness > 0 { bs.mu.timer = time.AfterFunc(bs.maxStaleness, func() { bs.mu.Lock() + defer bs.mu.Unlock() bs.flushAsyncLocked() - bs.mu.Unlock() }) } } @@ -330,9 +330,11 @@ func (bs *bufferedSink) runFlusher(stopC <-chan struct{}) { // We'll return after flushing everything. done = true } - bs.mu.Lock() - msg, errC := buf.flush(bs.format.prefix, bs.format.suffix, bs.format.delimiter) - bs.mu.Unlock() + msg, errC := func() (*buffer, chan<- error) { + bs.mu.Lock() + defer bs.mu.Unlock() + return buf.flush(bs.format.prefix, bs.format.suffix, bs.format.delimiter) + }() if msg == nil { // Nothing to flush. // NOTE: This can happen in the done case, or if we get two flushC signals @@ -350,9 +352,11 @@ func (bs *bufferedSink) runFlusher(stopC <-chan struct{}) { } else if err != nil { Ops.Errorf(context.Background(), "logging error from %T: %v", bs.child, err) if bs.crashOnAsyncFlushFailure { - logging.mu.Lock() - f := logging.mu.exitOverride.f - logging.mu.Unlock() + f := func() func(exit.Code, error) { + logging.mu.Lock() + defer logging.mu.Unlock() + return logging.mu.exitOverride.f + }() code := bs.exitCode() if f != nil { f(code, err)