From 558756d6c47ba15b0289c439815dd56a51c35a46 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 2 Apr 2020 22:11:33 -0500 Subject: [PATCH] Fix race condition when CreateResource is called concurrently with Close WaitGroup.Add(1) cannot be called after WaitGroup.Wait(). --- pool.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pool.go b/pool.go index b5003cc..5d9ea66 100644 --- a/pool.go +++ b/pool.go @@ -402,6 +402,11 @@ func (p *Pool) CreateResource(ctx context.Context) error { } p.cond.L.Lock() + // If closed while constructing resource then destroy it and return an error + if p.closed { + go p.destructResourceValue(res.value) + return ErrClosedPool + } p.allResources = append(p.allResources, res) p.idleResources = append(p.idleResources, res) p.destructWG.Add(1)