Skip to content

Commit

Permalink
grpc: hold ac.mu while calling resetTransport to prevent concurrent c…
Browse files Browse the repository at this point in the history
…onnection attempts (#7390)
  • Loading branch information
arjan-bal authored Jul 9, 2024
1 parent f64a6a3 commit 45d44a7
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,8 @@ func (ac *addrConn) connect() error {
ac.mu.Unlock()
return nil
}
ac.mu.Unlock()

ac.resetTransport()
ac.resetTransportAndUnlock()
return nil
}

Expand Down Expand Up @@ -992,11 +991,9 @@ func (ac *addrConn) updateAddrs(addrs []resolver.Address) {
ac.updateConnectivityState(connectivity.Idle, nil)
}

ac.mu.Unlock()

// Since we were connecting/connected, we should start a new connection
// attempt.
go ac.resetTransport()
go ac.resetTransportAndUnlock()
}

// getServerName determines the serverName to be used in the connection
Expand Down Expand Up @@ -1231,8 +1228,10 @@ func (ac *addrConn) adjustParams(r transport.GoAwayReason) {
}
}

func (ac *addrConn) resetTransport() {
ac.mu.Lock()
// resetTransportAndUnlock unconditionally connects the addrConn.
//
// ac.mu must be held by the caller, and this function will guarantee it is released.
func (ac *addrConn) resetTransportAndUnlock() {
acCtx := ac.ctx
if acCtx.Err() != nil {
ac.mu.Unlock()
Expand Down

0 comments on commit 45d44a7

Please sign in to comment.