From a35f4960a8ad1cbf1f0f7d2fbb723dbcc4d25f5f Mon Sep 17 00:00:00 2001 From: dferstay Date: Thu, 14 Nov 2019 19:53:34 -0800 Subject: [PATCH] Make Worker.Shutdown() synchronous (#58) Previously, a WaitGroup was used to track executing ShardConsumers and prevent Worker.Shutdown() from returning until all ShardConsumers had completed. Unfortunately, it was possible for Shutdown() to race with the eventLoop(), leading to a situation where Worker.Shutdown() returns while a ShardConsumer is still executing. Now, we increment the WaitGroup to keep track the eventLoop() as well as the ShardConsumers. This prevents shutdown from returning until all background go-routines have completed. Signed-off-by: Daniel Ferstay --- clientlibrary/worker/worker.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/clientlibrary/worker/worker.go b/clientlibrary/worker/worker.go index 132c123..f14eec0 100644 --- a/clientlibrary/worker/worker.go +++ b/clientlibrary/worker/worker.go @@ -115,8 +115,12 @@ func (w *Worker) Start() error { } log.Infof("Starting worker event loop.") - // entering event loop - go w.eventLoop() + w.waitGroup.Add(1) + go func() { + defer w.waitGroup.Done() + // entering event loop + w.eventLoop() + }() return nil }