From ca47263025c823b8c39f4cc942f4a04b05a90499 Mon Sep 17 00:00:00 2001 From: Keerthi Thatipally <99229993+kthatipally@users.noreply.github.com> Date: Mon, 16 Dec 2024 12:05:22 -0600 Subject: [PATCH] :bug: Add cancel context to NewGRPCClient (#720) ### PR Summary Bug fix to address the issue of java-external-provider.exe not ending after analysis completion. Introduces a cancel context to the NewGRPCClient to ensure proper termination of the gRPC client. - provider/grpc/provider.go: Added context.WithCancel to manage the lifecycle of the gRPC client and ensure it can be properly stopped. - provider/grpc/provider.go: Updated grpcProvider struct to include cancelCmd and invoked cancelCmd() in the Stop method to terminate the context. Signed-off-by: kthatipally <99229993+kthatipally@users.noreply.github.com> Signed-off-by: Cherry Picker --- provider/grpc/provider.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/provider/grpc/provider.go b/provider/grpc/provider.go index d621924d..d725e92a 100644 --- a/provider/grpc/provider.go +++ b/provider/grpc/provider.go @@ -25,11 +25,12 @@ import ( ) type grpcProvider struct { - Client pb.ProviderServiceClient - log logr.Logger - ctx context.Context - conn *grpc.ClientConn - config provider.Config + Client pb.ProviderServiceClient + log logr.Logger + ctx context.Context + conn *grpc.ClientConn + config provider.Config + cancelCmd context.CancelFunc serviceClients []provider.ServiceClient } @@ -39,7 +40,8 @@ var _ provider.InternalProviderClient = &grpcProvider{} func NewGRPCClient(config provider.Config, log logr.Logger) (provider.InternalProviderClient, error) { log = log.WithName(config.Name) log = log.WithValues("provider", "grpc") - conn, out, err := start(context.Background(), config) + ctxCmd, cancelCmd := context.WithCancel(context.Background()) + conn, out, err := start(ctxCmd, config) if err != nil { return nil, err } @@ -71,6 +73,7 @@ func NewGRPCClient(config provider.Config, log logr.Logger) (provider.InternalPr ctx: refCltCtx, conn: conn, config: config, + cancelCmd: cancelCmd, serviceClients: []provider.ServiceClient{}, } if out != nil { @@ -239,6 +242,7 @@ func (g *grpcProvider) Stop() { c.Stop() } g.conn.Close() + g.cancelCmd() } func start(ctx context.Context, config provider.Config) (*grpc.ClientConn, io.ReadCloser, error) {