From 7826c06eee6014cc1ec5b9079f94ed86c53e1dee Mon Sep 17 00:00:00 2001 From: Matthew Jaffee Date: Sat, 18 Dec 2021 08:58:19 -0600 Subject: [PATCH] use atomics for currentWorker to avoid race --- executor.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/executor.go b/executor.go index fb5ef68ce..5e03e1a72 100644 --- a/executor.go +++ b/executor.go @@ -64,7 +64,7 @@ type executor struct { workMu sync.RWMutex workersWG sync.WaitGroup workerPoolSize int - currentWorkers int + currentWorkers int64 work chan job // Maximum per-request memory usage (Extract() only) @@ -154,7 +154,7 @@ func newExecutor(opts ...executorOption) *executor { } if len(e.work) == 0 { idle++ - if idle > 10 && e.currentWorkers > (e.workerPoolSize*2) { + if idle > 10 && atomic.LoadInt64(&e.currentWorkers) > int64(e.workerPoolSize*2) { select { case e.work <- job{idleHands: true}: // we closed an excess worker @@ -179,11 +179,11 @@ func newExecutor(opts ...executorOption) *executor { func (e *executor) addWorker() { e.workersWG.Add(1) - e.currentWorkers++ + atomic.AddInt64(&e.currentWorkers, 1) go func() { defer e.workersWG.Done() e.worker(e.work) - e.currentWorkers-- + atomic.AddInt64(&e.currentWorkers, -1) }() }