From bb154abca4badc198fb7d4c87d4565e49a04edf5 Mon Sep 17 00:00:00 2001 From: Wei Fu Date: Wed, 12 Oct 2022 22:17:23 +0800 Subject: [PATCH] clientconn: go idle if conn closed after preface received The select branch will be selected randomly, if there are several ready branches. If the preface has been received and then connection is closed, the `createTransport` might hit the `connClosed.Done()` branch. Ideally, the subConn should go idle and reconnect. Fixes: #5688 Signed-off-by: Wei Fu --- clientconn.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/clientconn.go b/clientconn.go index 779b03bca1c..e82b1250b5c 100644 --- a/clientconn.go +++ b/clientconn.go @@ -1325,9 +1325,15 @@ func (ac *addrConn) createTransport(addr resolver.Address, copts transport.Conne return nil case <-connClosed.Done(): // The transport has already closed. If we received the preface, too, - // this is not an error. + // this is not an error and go idle. select { case <-prefaceReceived.Done(): + ac.mu.Lock() + defer ac.mu.Unlock() + + if ac.state != connectivity.Shutdown { + ac.updateConnectivityState(connectivity.Idle, nil) + } return nil default: return errors.New("connection closed before server preface received")