From 2806c4af7c5e856b843c292e0ae9be5ec53162c2 Mon Sep 17 00:00:00 2001 From: E_L <54755533+ELchem@users.noreply.github.com> Date: Thu, 6 Jul 2023 02:51:22 -0400 Subject: [PATCH] opt: fix the potential goroutine leak after calling `Release()` (#287) Co-authored-by: EL --- pool.go | 15 ++++++++------- pool_func.go | 15 ++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/pool.go b/pool.go index d4750a0..b0481bb 100644 --- a/pool.go +++ b/pool.go @@ -276,6 +276,14 @@ func (p *Pool) Release() { if !atomic.CompareAndSwapInt32(&p.state, OPENED, CLOSED) { return } + + if p.stopPurge != nil { + p.stopPurge() + p.stopPurge = nil + } + p.stopTicktock() + p.stopTicktock = nil + p.lock.Lock() p.workers.reset() p.lock.Unlock() @@ -289,13 +297,6 @@ func (p *Pool) ReleaseTimeout(timeout time.Duration) error { if p.IsClosed() || (!p.options.DisablePurge && p.stopPurge == nil) || p.stopTicktock == nil { return ErrPoolClosed } - - if p.stopPurge != nil { - p.stopPurge() - p.stopPurge = nil - } - p.stopTicktock() - p.stopTicktock = nil p.Release() endTime := time.Now().Add(timeout) diff --git a/pool_func.go b/pool_func.go index 96c8831..69d3c86 100644 --- a/pool_func.go +++ b/pool_func.go @@ -282,6 +282,14 @@ func (p *PoolWithFunc) Release() { if !atomic.CompareAndSwapInt32(&p.state, OPENED, CLOSED) { return } + + if p.stopPurge != nil { + p.stopPurge() + p.stopPurge = nil + } + p.stopTicktock() + p.stopTicktock = nil + p.lock.Lock() p.workers.reset() p.lock.Unlock() @@ -295,13 +303,6 @@ func (p *PoolWithFunc) ReleaseTimeout(timeout time.Duration) error { if p.IsClosed() || (!p.options.DisablePurge && p.stopPurge == nil) || p.stopTicktock == nil { return ErrPoolClosed } - - if p.stopPurge != nil { - p.stopPurge() - p.stopPurge = nil - } - p.stopTicktock() - p.stopTicktock = nil p.Release() endTime := time.Now().Add(timeout)