From d8c5b27d89489f218a1faa88985892dca11177bf Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Fri, 24 Mar 2023 08:59:32 +0100 Subject: [PATCH] Fix write to closed channel --- log/writer.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/log/writer.go b/log/writer.go index 0727989e..8063dac1 100644 --- a/log/writer.go +++ b/log/writer.go @@ -325,7 +325,9 @@ type ChannelWriter interface { } type channelWriter struct { - publisher chan Event + publisher chan Event + publisherClosed bool + publisherLock sync.Mutex ctx context.Context cancel context.CancelFunc @@ -336,8 +338,9 @@ type channelWriter struct { func NewChannelWriter() ChannelWriter { w := &channelWriter{ - publisher: make(chan Event, 1024), - subscriber: make(map[string]chan Event), + publisher: make(chan Event, 1024), + publisherClosed: false, + subscriber: make(map[string]chan Event), } w.ctx, w.cancel = context.WithCancel(context.Background()) @@ -351,6 +354,13 @@ func (w *channelWriter) Write(e *Event) error { event := e.clone() event.logger = nil + w.publisherLock.Lock() + defer w.publisherLock.Unlock() + + if w.publisherClosed { + return fmt.Errorf("writer is closed") + } + select { case w.publisher <- *e: default: @@ -363,7 +373,10 @@ func (w *channelWriter) Write(e *Event) error { func (w *channelWriter) Close() { w.cancel() + w.publisherLock.Lock() close(w.publisher) + w.publisherClosed = true + w.publisherLock.Unlock() w.subscriberLock.Lock() for _, c := range w.subscriber {