Merge pull request #5171 from Benehiko/feat-global-force-exit

fix: force cli to exit after third sigint/sigterm
This commit is contained in:
Paweł Gronowski 2024-06-20 17:49:07 +02:00 committed by GitHub
commit 0d415ad0e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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
}
// forceExitAfter3TerminationSignals waits for the first termination signal
// to be caught and the context to be marked as done, then registers a new
// signal handler for subsequent signals. It forces the process to exit
// after 3 SIGTERM/SIGINT signals.
func forceExitAfter3TerminationSignals(ctx context.Context, w io.Writer) {
// wait for the first signal to be caught and the context to be marked as done
<-ctx.Done()
// register a new signal handler for subsequent signals
sig := make(chan os.Signal, 2)
signal.Notify(sig, platformsignals.TerminationSignals...)
// once we have received a total of 3 signals we force exit the cli
for i := 0; i < 2; i++ {
<-sig
}
_, _ = 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.
go forceExitAfter3TerminationSignals(ctx, dockerCli.Err())
// We've parsed global args already, so reset args to those
// which remain.
cmd.SetArgs(args)