Skip to content

Commit

Permalink
plugin(language, converter): Respond to SIGINT
Browse files Browse the repository at this point in the history
Configures the language plugin binary to shut down the plugin RPC server
when SIGINT is received.
This will help the binary exit cleanly when it's time to shut down.

This functionality was added in pulumi/pulumi#13795.
pulumi/pulumi#13809 contains a similar change for Go, Python, and Node.
  • Loading branch information
abhinav committed Aug 29, 2023
1 parent 2760742 commit 48cab9c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

- Publish pulumi-converter-yaml.

### Bug Fixes
- Plugins: clean up resources and exit cleanly on receiving SIGINT or CTRL_BREAK.

### Bug Fixes
11 changes: 11 additions & 0 deletions cmd/pulumi-converter-yaml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"errors"
"fmt"
"log"
"os"
"os/signal"

yamlgen "github.com/pulumi/pulumi-yaml/pkg/pulumiyaml/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
Expand Down Expand Up @@ -94,8 +96,17 @@ func (*yamlConverter) ConvertProgram(ctx context.Context,

// Launches the converter RPC endpoint
func main() {
cancelch := make(chan bool)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
go func() {
<-ctx.Done()
cancel() // deregister the interrupt handler
close(cancelch)
}()

// Fire up a gRPC server, letting the kernel choose a free port for us.
handle, err := rpcutil.ServeWithOptions(rpcutil.ServeOptions{
Cancel: cancelch,
Init: func(srv *grpc.Server) error {
pulumirpc.RegisterConverterServer(srv, plugin.NewConverterServer(&yamlConverter{}))
return nil
Expand Down
11 changes: 9 additions & 2 deletions cmd/pulumi-language-yaml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -81,13 +83,18 @@ func main() {
}

func setupHealthChecks(engineAddress string) (chan bool, error) {
// If we have a host cancel our cancellation context if it fails the healthcheck
ctx, cancel := context.WithCancel(context.Background())
// If the health check begins failing or we receive a SIGINT,
// we'll cancel the context.
//
// The returned channel is used to notify the server that it should
// stop serving and exit.
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)

// map the context Done channel to the rpcutil boolean cancel channel
cancelChannel := make(chan bool)
go func() {
<-ctx.Done()
cancel() // deregister the signal handler
close(cancelChannel)
}()
err := rpcutil.Healthcheck(ctx, engineAddress, 5*time.Minute, cancel)
Expand Down

0 comments on commit 48cab9c

Please sign in to comment.