handle sigterm on running a plugin

While running a plugin and canceling with SIGTERM, main process will
close right away without letting the plugin close itself down and handle
the exit code properly. Add appcontext that is useful for handling
sigterm, as well as supporting sigkill when things go wrong.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi 2020-10-16 22:40:02 -07:00
parent 9b3eef5218
commit 857f5856f8
1 changed files with 6 additions and 0 deletions

View File

@ -15,6 +15,7 @@ import (
"github.com/docker/cli/cli/version" "github.com/docker/cli/cli/version"
"github.com/docker/docker/api/types/versions" "github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/moby/buildkit/util/appcontext"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -198,6 +199,11 @@ func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string)
return err return err
} }
go func() {
// override SIGTERM handler so we let the plugin shut down first
<-appcontext.Context().Done()
}()
if err := plugincmd.Run(); err != nil { if err := plugincmd.Run(); err != nil {
statusCode := 1 statusCode := 1
exitErr, ok := err.(*exec.ExitError) exitErr, ok := err.(*exec.ExitError)