Skip to content

Commit

Permalink
Adding ability for provider and service client to shutdown gracefully…
Browse files Browse the repository at this point in the history
… by catching signals and closing the pipes

Signed-off-by: Shawn Hurley <[email protected]>
  • Loading branch information
shawn-hurley committed Dec 10, 2024
1 parent bea5c15 commit 2707d32
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide
return nil, additionalBuiltinConfig, err
}

waitErrorChannel := make(chan error)
go func() {
err := cmd.Start()
if err != nil {
Expand All @@ -378,6 +379,23 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide
log.Error(err, "unable to start lsp command")
return
}
// Here we need to wait for the command to finish or if the ctx is cancelled,
// To close the pipes.
select {
case err := <-waitErrorChannel:
if err != nil {
log.Error(err, "language server stopped with error")
}
log.V(5).Info("language server stopped")
case <-ctx.Done():
log.Info("language server context cancelled closing pipes")
stdin.Close()
stdout.Close()
}
}()
// This will close the go routine above when wait has completed.
go func() {
waitErrorChannel <- cmd.Wait()
}()
rpc := jsonrpc2.NewConn(jsonrpc2.NewHeaderStream(stdout, stdin), log)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ func (p *javaServiceClient) GetAllReferences(ctx context.Context, symbol protoco

func (p *javaServiceClient) Stop() {
p.cancelFunc()
p.cmd.Wait()
err := p.cmd.Wait()
if err != nil {
p.log.Info("stopping java provider", "error", err)
}
}

func (p *javaServiceClient) initialization(ctx context.Context) {
Expand Down

0 comments on commit 2707d32

Please sign in to comment.