From 90f3ecd559b21cd61a5fd6e7627a7a1a880f92e1 Mon Sep 17 00:00:00 2001 From: denis-tingaikin Date: Fri, 11 Feb 2022 17:11:23 +0300 Subject: [PATCH 1/2] fix memory leaks Signed-off-by: denis-tingaikin --- pkg/registry/common/dial/ns_client.go | 28 ++++++++++++++------------ pkg/registry/common/dial/nse_client.go | 28 ++++++++++++++------------ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/pkg/registry/common/dial/ns_client.go b/pkg/registry/common/dial/ns_client.go index c7e763973..cd4756218 100644 --- a/pkg/registry/common/dial/ns_client.go +++ b/pkg/registry/common/dial/ns_client.go @@ -111,13 +111,13 @@ func (c *dialNSClient) Unregister(ctx context.Context, in *registry.NetworkServi type dialNSFindClient struct { registry.NetworkServiceRegistry_FindClient - closeFn func() + cleanupFn func() } func (c *dialNSFindClient) Recv() (*registry.NetworkServiceResponse, error) { resp, err := c.NetworkServiceRegistry_FindClient.Recv() if err != nil { - c.closeFn() + c.cleanupFn() } return resp, err } @@ -130,31 +130,33 @@ func (c *dialNSClient) Find(ctx context.Context, in *registry.NetworkServiceQuer di := newDialer(c.chainCtx, c.dialTimeout, c.dialOptions...) - findCtx, cancel := context.WithCancel(ctx) - - err := di.Dial(findCtx, clientURL) + err := di.Dial(ctx, clientURL) if err != nil { log.FromContext(ctx).Errorf("can not dial to %v, err %v. Deleting clientconn...", grpcutils.URLToTarget(clientURL), err) - cancel() return nil, err } clientconn.Store(ctx, di) - defer clientconn.Delete(ctx) + + cleanupFn := func() { + clientconn.Delete(ctx) + di.Close() + } resp, err := next.NetworkServiceRegistryClient(ctx).Find(ctx, in, opts...) if err != nil { - _ = di.Close() - cancel() + cleanupFn() return nil, err } + go func() { + <-resp.Context().Done() + cleanupFn() + }() + return &dialNSFindClient{ NetworkServiceRegistry_FindClient: resp, - closeFn: func() { - cancel() - _ = di.Close() - }, + cleanupFn: cleanupFn, }, nil } diff --git a/pkg/registry/common/dial/nse_client.go b/pkg/registry/common/dial/nse_client.go index 4236d5a18..fab6683ff 100644 --- a/pkg/registry/common/dial/nse_client.go +++ b/pkg/registry/common/dial/nse_client.go @@ -111,13 +111,13 @@ func (c *dialNSEClient) Unregister(ctx context.Context, in *registry.NetworkServ type dialNSEFindClient struct { registry.NetworkServiceEndpointRegistry_FindClient - closeFn func() + cleanupFn func() } func (c *dialNSEFindClient) Recv() (*registry.NetworkServiceEndpointResponse, error) { resp, err := c.NetworkServiceEndpointRegistry_FindClient.Recv() if err != nil { - c.closeFn() + c.cleanupFn() } return resp, err } @@ -130,31 +130,33 @@ func (c *dialNSEClient) Find(ctx context.Context, in *registry.NetworkServiceEnd di := newDialer(c.chainCtx, c.dialTimeout, c.dialOptions...) - findCtx, cancel := context.WithCancel(ctx) - - err := di.Dial(findCtx, clientURL) + err := di.Dial(ctx, clientURL) if err != nil { log.FromContext(ctx).Errorf("can not dial to %v, err %v. Deleting clientconn...", grpcutils.URLToTarget(clientURL), err) - cancel() return nil, err } clientconn.Store(ctx, di) - defer clientconn.Delete(ctx) + + cleanupFn := func() { + clientconn.Delete(ctx) + di.Close() + } resp, err := next.NetworkServiceEndpointRegistryClient(ctx).Find(ctx, in, opts...) if err != nil { - _ = di.Close() - cancel() + cleanupFn() return nil, err } + go func() { + <-resp.Context().Done() + cleanupFn() + }() + return &dialNSEFindClient{ NetworkServiceEndpointRegistry_FindClient: resp, - closeFn: func() { - cancel() - _ = di.Close() - }, + cleanupFn: cleanupFn, }, nil } From 8852ae5fbde3a8a66fd3e09ea8b04091b2dffb8f Mon Sep 17 00:00:00 2001 From: denis-tingaikin Date: Fri, 11 Feb 2022 17:28:40 +0300 Subject: [PATCH 2/2] fix linter issue Signed-off-by: denis-tingaikin --- pkg/registry/common/dial/ns_client.go | 2 +- pkg/registry/common/dial/nse_client.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/registry/common/dial/ns_client.go b/pkg/registry/common/dial/ns_client.go index cd4756218..6c0bf9ab9 100644 --- a/pkg/registry/common/dial/ns_client.go +++ b/pkg/registry/common/dial/ns_client.go @@ -140,7 +140,7 @@ func (c *dialNSClient) Find(ctx context.Context, in *registry.NetworkServiceQuer cleanupFn := func() { clientconn.Delete(ctx) - di.Close() + _ = di.Close() } resp, err := next.NetworkServiceRegistryClient(ctx).Find(ctx, in, opts...) diff --git a/pkg/registry/common/dial/nse_client.go b/pkg/registry/common/dial/nse_client.go index fab6683ff..c2adfc489 100644 --- a/pkg/registry/common/dial/nse_client.go +++ b/pkg/registry/common/dial/nse_client.go @@ -140,7 +140,7 @@ func (c *dialNSEClient) Find(ctx context.Context, in *registry.NetworkServiceEnd cleanupFn := func() { clientconn.Delete(ctx) - di.Close() + _ = di.Close() } resp, err := next.NetworkServiceEndpointRegistryClient(ctx).Find(ctx, in, opts...)