fix: Memory leak (#114)

Fixes #113
This commit is contained in:
Z 2020-10-15 11:35:55 +08:00 committed by GitHub
parent ef60172172
commit 94a7a7f1cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -259,6 +259,13 @@ func (p *Pool) revertWorker(worker *goWorker) bool {
worker.recycleTime = time.Now() worker.recycleTime = time.Now()
p.lock.Lock() 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) err := p.workers.insert(worker)
if err != nil { if err != nil {
p.lock.Unlock() p.lock.Unlock()

View File

@ -277,6 +277,14 @@ func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool {
} }
worker.recycleTime = time.Now() worker.recycleTime = time.Now()
p.lock.Lock() 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) p.workers = append(p.workers, worker)
// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.