Skip to content

Commit b6ad4fd

Browse files
committed
fix data race
1 parent ce13fa8 commit b6ad4fd

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

internal/pool/pool.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ type wantConn struct {
122122
result chan wantConnResult // channel to deliver connection or error
123123
}
124124

125+
// getCtxForDial returns context for dial or nil if connection was delivered or canceled.
126+
func (w *wantConn) getCtxForDial() context.Context {
127+
w.mu.Lock()
128+
defer w.mu.Unlock()
129+
130+
return w.ctx
131+
}
132+
125133
func (w *wantConn) tryDeliver(cn *Conn, err error) bool {
126134
w.mu.Lock()
127135
defer w.mu.Unlock()
@@ -576,12 +584,13 @@ func (p *ConnPool) asyncNewConn(ctx context.Context) (*Conn, error) {
576584
defer w.cancelCtx()
577585
defer func() { <-p.dialsInProgress }() // Release connection creation permission
578586

579-
cn, cnErr := p.newConn(w.ctx, true)
587+
dialCtx := w.getCtxForDial()
588+
cn, cnErr := p.newConn(dialCtx, true)
580589
delivered := w.tryDeliver(cn, cnErr)
581590
if cnErr == nil && delivered {
582591
return
583592
} else if cnErr == nil && !delivered {
584-
p.Put(w.ctx, cn)
593+
p.Put(dialCtx, cn)
585594
} else { // freeTurn after error
586595
p.freeTurn()
587596
}

0 commit comments

Comments
 (0)