From 99c5c583650992659175bce9df256b4398c35ebe Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 10 Oct 2024 14:00:54 +0300 Subject: [PATCH] [#282] client: Close connection on non-nil error in Dial A particular status code does not imply that a connection has not been established. However, `Dial()` requires user to call `Close()` only if the error was nil. Thus, it is `Dial()` responsibility to close everything if it returns an error. Introduced after the gRPC update in #270 (6009d089fc69). Signed-off-by: Evgenii Stratonikov --- client/client.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/client/client.go b/client/client.go index 4dd5059..0894f0e 100644 --- a/client/client.go +++ b/client/client.go @@ -106,16 +106,21 @@ func (c *Client) Dial(ctx context.Context, prm PrmDial) error { client.WithContext(ctx), ) if err != nil { + var ctxErr error + // return context errors since they signal about dial problem if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { - return err + ctxErr = err + } else if st, ok := status.FromError(err); ok && st.Code() == codes.Canceled { + ctxErr = context.Canceled + } else if ok && st.Code() == codes.DeadlineExceeded { + ctxErr = context.DeadlineExceeded } - st, ok := status.FromError(err) - if ok && st.Code() == codes.Canceled { - return context.Canceled - } - if ok && st.Code() == codes.DeadlineExceeded { - return context.DeadlineExceeded + if ctxErr != nil { + if conn := c.c.Conn(); conn != nil { + _ = conn.Close() + } + return ctxErr } } -- 2.45.2