From 044bcc008a0593c3454781d146cff56ff0f88049 Mon Sep 17 00:00:00 2001 From: Geoffrey Flarity Date: Fri, 29 Sep 2023 17:08:01 -0400 Subject: [PATCH] wait for log producer to really stop inside StopLogProducer func --- docker.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/docker.go b/docker.go index 2734bfe1446..3dcb537a963 100644 --- a/docker.go +++ b/docker.go @@ -67,6 +67,7 @@ type DockerContainer struct { consumers []LogConsumer raw *types.ContainerJSON stopProducer chan bool + producerDone chan bool logger Logging lifecycleHooks []ContainerLifecycleHooks } @@ -616,8 +617,13 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error { } c.stopProducer = make(chan bool) + c.producerDone = make(chan bool) + + go func(stop <-chan bool, done chan<- bool) { + + // signal the producer is done once go routine exits, this prevents race conditions around start/stop + defer close(done) - go func(stop <-chan bool) { since := "" // if the socket is closed we will make additional logs request with updated Since timestamp BEGIN: @@ -702,7 +708,7 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error { } } } - }(c.stopProducer) + }(c.stopProducer, c.producerDone) return nil } @@ -712,7 +718,10 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error { func (c *DockerContainer) StopLogProducer() error { if c.stopProducer != nil { c.stopProducer <- true + // block until the producer is actually done in order to avoid strange races + <-c.producerDone c.stopProducer = nil + c.producerDone = nil } return nil }