From d344a18fdb50b641927efb43b25a24f360b26944 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 27 Oct 2022 20:46:47 -0500 Subject: [PATCH] Add to the destructWG wait group while cond.L is locked Otherwise, if many constructors are underway while the pool is closed it is possible destructWG will get to 0 while the Wait in Close is underway and then Add will be called again which is disallowed. From the docs: Note that calls with a positive delta that occur when the counter is zero must happen before a Wait. https://github.com/jackc/pgx/issues/1356 --- pool.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pool.go b/pool.go index 9c87f58..3708ebe 100644 --- a/pool.go +++ b/pool.go @@ -496,10 +496,12 @@ func (p *Pool[T]) CreateResource(ctx context.Context) error { p.cond.L.Unlock() return ErrClosedPool } + p.destructWG.Add(1) p.cond.L.Unlock() value, err := p.constructResourceValue(ctx) if err != nil { + p.destructWG.Done() return err } @@ -511,7 +513,6 @@ func (p *Pool[T]) CreateResource(ctx context.Context) error { lastUsedNano: nanotime(), poolResetCount: p.resetCount, } - p.destructWG.Add(1) p.cond.L.Lock() // If closed while constructing resource then destroy it and return an error