From 94a7a7f1cb3a11fcaaf9608c94fe1dc0bcd98ab0 Mon Sep 17 00:00:00 2001 From: Z Date: Thu, 15 Oct 2020 11:35:55 +0800 Subject: [PATCH] fix: Memory leak (#114) Fixes #113 --- pool.go | 7 +++++++ pool_func.go | 8 ++++++++ 2 files changed, 15 insertions(+) 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.