From a45126ac5dff337792a3f3023ec3c48544b70819 Mon Sep 17 00:00:00 2001 From: Oleg Stotskiy <ostotsky@ordercapital.com> Date: Mon, 5 Feb 2024 17:55:52 +0400 Subject: [PATCH] fix ConnPool race in newConn --- internal/pool/pool.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/pool/pool.go b/internal/pool/pool.go index 986c05d0a..2125f3e13 100644 --- a/internal/pool/pool.go +++ b/internal/pool/pool.go @@ -168,9 +168,12 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) { return nil, ErrClosed } + p.connsMu.Lock() if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns { + p.connsMu.Unlock() return nil, ErrPoolExhausted } + p.connsMu.Unlock() cn, err := p.dialConn(ctx, pooled) if err != nil { @@ -180,6 +183,11 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) { p.connsMu.Lock() defer p.connsMu.Unlock() + if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns { + _ = cn.Close() + return nil, ErrPoolExhausted + } + p.conns = append(p.conns, cn) if pooled { // If pool is full remove the cn on next Put.