feat: add a global sigint/sigterm handler as a fallback to ctx cancellation

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
This commit is contained in:
Alano Terblanche 2024-06-18 15:42:34 +02:00
parent 70b53a0c15
commit a4bfd8c744
No known key found for this signature in database
GPG Key ID: 0E8FACD1BA98DE27
1 changed files with 24 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
@ -325,6 +326,25 @@ func tryPluginRun(ctx context.Context, dockerCli command.Cli, cmd *cobra.Command
return nil
}
// registerForceExitGoroutine registers a goroutine that will force exit the
// process after 3 SIGTERM/SIGINT signals.
func registerForceExitGoroutine(ctx context.Context, w io.Writer) {
// setup a signal handler to force exit after 3 SIGTERM/SIGINT
go func() {
<-ctx.Done()
sig := make(chan os.Signal, 2)
signal.Notify(sig, platformsignals.TerminationSignals...)
count := 0
for range sig {
count++
if count >= 2 {
_, _ = fmt.Fprint(w, "\ngot 3 SIGTERM/SIGINTs, forcefully exiting\n")
os.Exit(1)
}
}
}()
}
//nolint:gocyclo
func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
tcmd := newDockerCommand(dockerCli)
@ -384,6 +404,10 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
}
}
// This is a fallback for the case where the command does not exit
// based on context cancellation.
registerForceExitGoroutine(ctx, dockerCli.Err())
// We've parsed global args already, so reset args to those
// which remain.
cmd.SetArgs(args)