mirror of https://github.com/docker/cli.git
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:
parent
70b53a0c15
commit
a4bfd8c744
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
@ -325,6 +326,25 @@ func tryPluginRun(ctx context.Context, dockerCli command.Cli, cmd *cobra.Command
|
||||||
return nil
|
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
|
//nolint:gocyclo
|
||||||
func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
|
func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
|
||||||
tcmd := newDockerCommand(dockerCli)
|
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
|
// We've parsed global args already, so reset args to those
|
||||||
// which remain.
|
// which remain.
|
||||||
cmd.SetArgs(args)
|
cmd.SetArgs(args)
|
||||||
|
|
Loading…
Reference in New Issue