From ee0423e79a669e36cb3e3eeb65347724f71897ce Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 16 Aug 2026 14:32:31 +0000 Subject: [PATCH] fix: harden non-blocking console logger shutdown Co-authored-by: jkroepke <1560587+jkroepke@users.noreply.github.com> --- internal/log/nonblocking_writer.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/internal/log/nonblocking_writer.go b/internal/log/nonblocking_writer.go index 89f5dfe6..269a9766 100644 --- a/internal/log/nonblocking_writer.go +++ b/internal/log/nonblocking_writer.go @@ -28,7 +28,8 @@ type nonBlockingWriter struct { queue chan []byte stop chan struct{} workerDone chan struct{} - once sync.Once + mu sync.RWMutex + closed bool } func newNonBlockingWriter(writer io.Writer, queueSize int) *nonBlockingWriter { @@ -69,9 +70,14 @@ func newNonBlockingWriter(writer io.Writer, queueSize int) *nonBlockingWriter { func (w *nonBlockingWriter) Write(p []byte) (int, error) { msg := bytes.Clone(p) - select { - case <-w.stop: + w.mu.RLock() + defer w.mu.RUnlock() + + if w.closed { return 0, io.ErrClosedPipe + } + + select { case w.queue <- msg: default: } @@ -80,9 +86,12 @@ func (w *nonBlockingWriter) Write(p []byte) (int, error) { } func (w *nonBlockingWriter) Close() error { - w.once.Do(func() { + w.mu.Lock() + if !w.closed { + w.closed = true close(w.stop) - }) + } + w.mu.Unlock() <-w.workerDone