mirror of https://github.com/grpc/grpc-go.git
Avoid goroutine leak in clientconn
Prior to this change, it was possible for `DialContext` to return `(nil, err)` without properly closing the `ClientConn`, resulting in an unavoidable leak of the `resetAddrConn` goroutine.
This commit is contained in:
parent
d8f4ebe77f
commit
dd5645bebf
|
@ -234,13 +234,13 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
|
||||||
defer func() {
|
defer func() {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
if conn != nil {
|
conn, err = nil, ctx.Err()
|
||||||
conn.Close()
|
|
||||||
}
|
|
||||||
conn = nil
|
|
||||||
err = ctx.Err()
|
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
cc.Close()
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
|
@ -296,11 +296,9 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
|
||||||
return nil, ctx.Err()
|
return nil, ctx.Err()
|
||||||
case err := <-waitC:
|
case err := <-waitC:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cc.Close()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case <-timeoutCh:
|
case <-timeoutCh:
|
||||||
cc.Close()
|
|
||||||
return nil, ErrClientConnTimeout
|
return nil, ErrClientConnTimeout
|
||||||
}
|
}
|
||||||
// If balancer is nil or balancer.Notify() is nil, ok will be false here.
|
// If balancer is nil or balancer.Notify() is nil, ok will be false here.
|
||||||
|
|
Loading…
Reference in New Issue