1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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 <james.johnston@thumbtack.com>
                  Nathan Wong <nathanw@thumbtack.com>

Signed-off-by: Anuj Varma <anujvarma@thumbtack.com>
This commit is contained in:
Anuj Varma 2021-04-24 18:29:28 -07:00
parent e0170da0dc
commit cf259eb8a0

View file

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