From bb11536480f91612e3d42a304794b3131486f4cf Mon Sep 17 00:00:00 2001 From: NikitaSkrynnik Date: Mon, 14 Oct 2024 21:28:34 +0700 Subject: [PATCH] fix memory leak Signed-off-by: NikitaSkrynnik --- pkg/networkservice/nsmonitor/client.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/networkservice/nsmonitor/client.go b/pkg/networkservice/nsmonitor/client.go index f9c69a59..54c5dcd1 100644 --- a/pkg/networkservice/nsmonitor/client.go +++ b/pkg/networkservice/nsmonitor/client.go @@ -73,12 +73,14 @@ func (r *netNSMonitorClient) Request(ctx context.Context, request *networkservic r.monitor = r.supplyMonitor(r.chainCtx) }) - cancelCtx, cancel := context.WithCancel(r.chainCtx) - if _, ok := metadata.Map(ctx, metadata.IsClient(r)).LoadOrStore(key{}, cancel); !ok { + if _, ok := metadata.Map(ctx, metadata.IsClient(r)).Load(key{}); !ok { + cancelCtx, cancel := context.WithCancel(r.chainCtx) + metadata.Map(ctx, metadata.IsClient(r)).Store(key{}, cancel) if inodeURL, ok := conn.GetMechanism().GetParameters()[common.InodeURL]; ok { deleteCh := r.monitor.Watch(cancelCtx, inodeURL) factory := begin.FromContext(ctx) go func() { + defer cancel() select { case <-r.chainCtx.Done(): return @@ -86,7 +88,7 @@ func (r *netNSMonitorClient) Request(ctx context.Context, request *networkservic return case _, ok := <-deleteCh: if ok && cancelCtx.Err() == nil { - factory.Close(begin.CancelContext(cancelCtx)) + <-factory.Close(begin.CancelContext(cancelCtx)) } return }