Skip to content

Commit 36bab9c

Browse files
OlegStotskyos-ocofekshenawa
authored
fix ConnPool race in newConn (#2885)
Co-authored-by: Oleg Stotskiy <[email protected]> Co-authored-by: ofekshenawa <[email protected]>
1 parent a157737 commit 36bab9c

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

internal/pool/pool.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,12 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
168168
return nil, ErrClosed
169169
}
170170

171+
p.connsMu.Lock()
171172
if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns {
173+
p.connsMu.Unlock()
172174
return nil, ErrPoolExhausted
173175
}
176+
p.connsMu.Unlock()
174177

175178
cn, err := p.dialConn(ctx, pooled)
176179
if err != nil {
@@ -180,6 +183,11 @@ func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
180183
p.connsMu.Lock()
181184
defer p.connsMu.Unlock()
182185

186+
if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns {
187+
_ = cn.Close()
188+
return nil, ErrPoolExhausted
189+
}
190+
183191
p.conns = append(p.conns, cn)
184192
if pooled {
185193
// If pool is full remove the cn on next Put.

0 commit comments

Comments
 (0)