fix: harden non-blocking console logger shutdown

Co-authored-by: jkroepke <1560587+jkroepke@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-08-16 14:32:31 +00:00
committed by GitHub
parent d9b8ff9dc0
commit ee0423e79a

View File

@@ -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