diff --git a/pool.go b/pool.go index bf2c1e9..6adf751 100644 --- a/pool.go +++ b/pool.go @@ -259,6 +259,13 @@ func (p *Pool) revertWorker(worker *goWorker) bool { worker.recycleTime = time.Now() p.lock.Lock() + // To avoid memory leaks, add a double check in the lock scope. + // Issue: https://github.com/panjf2000/ants/issues/113 + if atomic.LoadInt32(&p.state) == CLOSED { + p.lock.Unlock() + return false + } + err := p.workers.insert(worker) if err != nil { p.lock.Unlock() diff --git a/pool_func.go b/pool_func.go index 7c16a98..ab0fb0f 100644 --- a/pool_func.go +++ b/pool_func.go @@ -277,6 +277,14 @@ func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool { } worker.recycleTime = time.Now() p.lock.Lock() + + // To avoid memory leaks, add a double check in the lock scope. + // Issue: https://github.com/panjf2000/ants/issues/113 + if atomic.LoadInt32(&p.state) == CLOSED { + p.lock.Unlock() + return false + } + p.workers = append(p.workers, worker) // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.