From bdb616caa5d4197fe801e0063f4234d900fa3a0e Mon Sep 17 00:00:00 2001 From: Corentin Clabaut Date: Tue, 11 Jun 2024 08:41:29 +0200 Subject: [PATCH] Fix race condition --- pond.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pond.go b/pond.go index 90fa70d..6bd1a6f 100644 --- a/pond.go +++ b/pond.go @@ -353,11 +353,6 @@ func (p *WorkerPool) stop(waitForQueuedTasksToComplete bool) { // Mark pool as stopped atomic.StoreInt32(&p.stopped, 1) - // close tasks channel (only once, in case multiple concurrent calls to StopAndWait are made) - p.tasksCloseOnce.Do(func() { - close(p.tasks) - }) - if waitForQueuedTasksToComplete { // Wait for all queued tasks to complete p.tasksWaitGroup.Wait() @@ -369,6 +364,11 @@ func (p *WorkerPool) stop(waitForQueuedTasksToComplete bool) { // Terminate all workers & purger goroutine p.contextCancel() + // close tasks channel (only once, in case multiple concurrent calls to StopAndWait are made) + p.tasksCloseOnce.Do(func() { + close(p.tasks) + }) + // Wait for all workers & purger goroutine to exit p.workersWaitGroup.Wait() }