2
0

Do not leave pool lock after panic from nil context

refs #13
This commit is contained in:
Jack Christensen
2021-11-13 17:39:29 -06:00
parent 69a4c02937
commit 4d33264d63
2 changed files with 17 additions and 1 deletions
+3 -1
View File
@@ -276,10 +276,10 @@ func (p *Pool) TryAcquire(ctx context.Context) (*Resource, error) {
// will return ErrNotAvailable if no resource is available.
func (p *Pool) doAcquire(ctx context.Context, block bool) (*Resource, error) {
startNano := nanotime()
p.cond.L.Lock()
if doneChan := ctx.Done(); doneChan != nil {
select {
case <-ctx.Done():
p.cond.L.Lock()
p.canceledAcquireCount += 1
p.cond.L.Unlock()
return nil, ctx.Err()
@@ -287,6 +287,8 @@ func (p *Pool) doAcquire(ctx context.Context, block bool) (*Resource, error) {
}
}
p.cond.L.Lock()
emptyAcquire := false
for {
+14
View File
@@ -192,6 +192,20 @@ func TestPoolTryAcquireDoesNotBlock(t *testing.T) {
assert.Equal(t, 1, createCounter.Value())
}
func TestPoolAcquireNilContextDoesNotLeavePoolLocked(t *testing.T) {
constructor, createCounter := createConstructor()
pool := puddle.NewPool(constructor, stubDestructor, 10)
assert.Panics(t, func() { pool.Acquire(nil) })
res, err := pool.Acquire(context.Background())
require.NoError(t, err)
assert.Equal(t, 1, res.Value())
res.Release()
assert.Equal(t, 1, createCounter.Value())
}
func TestPoolAcquireContextAlreadyCanceled(t *testing.T) {
constructor := func(ctx context.Context) (interface{}, error) {
panic("should never be called")