From c3a3d1c48adcf492696029fa946d606e5e7ba40a Mon Sep 17 00:00:00 2001 From: Arjan Bal Date: Thu, 4 Jul 2024 13:16:02 +0530 Subject: [PATCH 1/3] Update conn state to prevent concurrent connection attempts --- clientconn.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clientconn.go b/clientconn.go index 423be7b43b00..e682effeca6e 100644 --- a/clientconn.go +++ b/clientconn.go @@ -918,6 +918,9 @@ func (ac *addrConn) connect() error { ac.mu.Unlock() return nil } + // Update the state to ensure no concurrent requests start once the lock + // is released. + ac.updateConnectivityState(connectivity.Connecting, nil) ac.mu.Unlock() ac.resetTransport() From 6214c9dd1cd421727852b03cafceac7c9e9eb973 Mon Sep 17 00:00:00 2001 From: Arjan Bal Date: Tue, 9 Jul 2024 00:43:36 +0530 Subject: [PATCH 2/3] Make callers of resetBackoff() lock the mutex --- clientconn.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/clientconn.go b/clientconn.go index e682effeca6e..a0a751e5134d 100644 --- a/clientconn.go +++ b/clientconn.go @@ -918,12 +918,8 @@ func (ac *addrConn) connect() error { ac.mu.Unlock() return nil } - // Update the state to ensure no concurrent requests start once the lock - // is released. - ac.updateConnectivityState(connectivity.Connecting, nil) - ac.mu.Unlock() - ac.resetTransport() + ac.resetTransportAndUnlock() return nil } @@ -995,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 @@ -1234,8 +1228,7 @@ func (ac *addrConn) adjustParams(r transport.GoAwayReason) { } } -func (ac *addrConn) resetTransport() { - ac.mu.Lock() +func (ac *addrConn) resetTransportAndUnlock() { acCtx := ac.ctx if acCtx.Err() != nil { ac.mu.Unlock() From ff977b39fc525a077d16427fad00949e179dc17b Mon Sep 17 00:00:00 2001 From: Arjan Bal Date: Tue, 9 Jul 2024 11:55:33 +0530 Subject: [PATCH 3/3] Add doc comment for resetTransportAndUnlock --- clientconn.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clientconn.go b/clientconn.go index a0a751e5134d..c3cafbc1e55c 100644 --- a/clientconn.go +++ b/clientconn.go @@ -1228,6 +1228,9 @@ func (ac *addrConn) adjustParams(r transport.GoAwayReason) { } } +// 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 {