Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin(language, converter): Respond to SIGINT #501

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need a defer'd cancel() // dregister the interrupt handler here like in the converter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derp, we do. Good catch. Fixed


// 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
Loading