Skip to content

Commit

Permalink
🐛 Add cancel context to NewGRPCClient (#720)
Browse files Browse the repository at this point in the history
### 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 <[email protected]>
  • Loading branch information
kthatipally authored Dec 16, 2024
1 parent 4aa811c commit 6e059e8
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions provider/grpc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6e059e8

Please sign in to comment.