Skip to content

Commit

Permalink
fix deadlock #57
Browse files Browse the repository at this point in the history
  • Loading branch information
ykalay7 authored and jolestar committed Apr 20, 2022
1 parent 1d53859 commit 3f25f85
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
25 changes: 17 additions & 8 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,7 @@ func (pool *ObjectPool) ensureIdle(ctx context.Context, idleCount int, always bo

// IsClosed return this pool is closed
func (pool *ObjectPool) IsClosed() bool {
pool.closeLock.Lock()
defer pool.closeLock.Unlock()
// in java commons pool, closed is volatile, golang has not volatile, so use mutex to avoid data race
return pool.closed
return pool.getClose()
}

// ReturnObject return an instance to the pool. By contract, object
Expand Down Expand Up @@ -483,17 +480,16 @@ func (pool *ObjectPool) Close(ctx context.Context) {
if pool.IsClosed() {
return
}
pool.closeLock.Lock()
defer pool.closeLock.Unlock()
if pool.closed {

if pool.getClose() {
return
}

// Stop the evictor before the pool is closed since evict() calls
// assertOpen()
pool.startEvictor(-1)

pool.closed = true
pool.setClose(true)
// This clear removes any idle objects
pool.Clear(ctx)

Expand Down Expand Up @@ -668,6 +664,19 @@ func (pool *ObjectPool) evict(ctx context.Context) {
}
}

func (pool *ObjectPool) getClose() bool {
pool.closeLock.Lock()
defer pool.closeLock.Unlock()
// in java commons pool, closed is volatile, golang has not volatile, so use mutex to avoid data race
return pool.closed
}

func (pool *ObjectPool) setClose(closed bool) {
pool.closeLock.Lock()
defer pool.closeLock.Unlock()
pool.closed = closed
}

func (pool *ObjectPool) ensureMinIdle(ctx context.Context) {
pool.ensureIdle(ctx, pool.getMinIdle(), true)
}
Expand Down
26 changes: 26 additions & 0 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,32 @@ func (suit *PoolTestSuite) TestPreparePool() {
suit.Equal(1, suit.pool.GetNumIdle())
}

func (suit *PoolTestSuite) TestConcurrentCloseAndEvict() {
ctx := context.Background()
suit.pool.Config.MinIdle = 1
suit.pool.Config.SoftMinEvictableIdleTime = time.Millisecond * 100
suit.pool.Config.TimeBetweenEvictionRuns = time.Millisecond * 500
suit.factory.destroyLatency = time.Millisecond * 1000 // Destroy takes 1000 ms
suit.pool.PreparePool(ctx)
suit.pool.StartEvictor()
suit.Equal(1, suit.pool.GetNumIdle())
ticker := time.NewTicker(time.Millisecond * 1000)
testTimeoutTicker := time.NewTicker(time.Millisecond * 5000) // if time exceeds test fails
defer ticker.Stop()
defer testTimeoutTicker.Stop()
go func() {
select {
case <-testTimeoutTicker.C:
// Time-out
suit.FailNow("Time is exceeds on pool close")
}
}()
select {
case <-ticker.C:
suit.pool.Close(ctx)
}
}

func (suit *PoolTestSuite) TestValueFactory() {
suit.pool.factory = NewPooledObjectFactorySimple(func(context.Context) (interface{}, error) {
return "string value", nil
Expand Down

0 comments on commit 3f25f85

Please sign in to comment.