Skip to content

Commit 94a7a7f

Browse files
authored
fix: Memory leak (panjf2000#114)
Fixes panjf2000#113
1 parent ef60172 commit 94a7a7f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pool.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ func (p *Pool) revertWorker(worker *goWorker) bool {
259259
worker.recycleTime = time.Now()
260260
p.lock.Lock()
261261

262+
// To avoid memory leaks, add a double check in the lock scope.
263+
// Issue: https://github.com/panjf2000/ants/issues/113
264+
if atomic.LoadInt32(&p.state) == CLOSED {
265+
p.lock.Unlock()
266+
return false
267+
}
268+
262269
err := p.workers.insert(worker)
263270
if err != nil {
264271
p.lock.Unlock()

pool_func.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool {
277277
}
278278
worker.recycleTime = time.Now()
279279
p.lock.Lock()
280+
281+
// To avoid memory leaks, add a double check in the lock scope.
282+
// Issue: https://github.com/panjf2000/ants/issues/113
283+
if atomic.LoadInt32(&p.state) == CLOSED {
284+
p.lock.Unlock()
285+
return false
286+
}
287+
280288
p.workers = append(p.workers, worker)
281289

282290
// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.

0 commit comments

Comments
 (0)