From cf259eb8a04c44844b46897949dad7fdfc71230c Mon Sep 17 00:00:00 2001 From: Anuj Varma Date: Sat, 24 Apr 2021 18:29:28 -0700 Subject: [PATCH] Wait for `run` goroutine to exit before `Close` The underlying Loggers Close() function can be called with the the run() goroutine still writing to the driver. This is causing the fluentd-golang-logger to panic cause it doesn't defensively check for the closing of the channel before writing to it. It relies on the docker daemon to keep the contract of not calling Log() if Close() has already been called. Contributions by: James Johnston Nathan Wong Signed-off-by: Anuj Varma --- daemon/logger/ring.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/daemon/logger/ring.go b/daemon/logger/ring.go index b6432aed36..658f0fcfd0 100644 --- a/daemon/logger/ring.go +++ b/daemon/logger/ring.go @@ -19,6 +19,7 @@ type RingLogger struct { l Logger logInfo Info closeFlag int32 + wg sync.WaitGroup } var _ SizedLogger = &RingLogger{} @@ -42,6 +43,7 @@ func newRingLogger(driver Logger, logInfo Info, maxSize int64) *RingLogger { l: driver, logInfo: logInfo, } + l.wg.Add(1) go l.run() return l } @@ -93,6 +95,7 @@ func (r *RingLogger) setClosed() { func (r *RingLogger) Close() error { r.setClosed() r.buffer.Close() + r.wg.Wait() // empty out the queue var logErr bool for _, msg := range r.buffer.Drain() { @@ -118,6 +121,7 @@ func (r *RingLogger) Close() error { // logger. // This is run in a goroutine when the RingLogger is created func (r *RingLogger) run() { + defer r.wg.Done() for { if r.closed() { return