Skip to content

Commit

Permalink
opt: fix the potential goroutine leak after calling Release() (#287)
Browse files Browse the repository at this point in the history
Co-authored-by: EL <evnldn9321@gmail.com>
  • Loading branch information
ELchem and EL committed Jul 6, 2023
1 parent 45a0390 commit 2806c4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
15 changes: 8 additions & 7 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions pool_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 2806c4a

Please sign in to comment.