Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: global signal handling to cancel ctx for graceful exits#4993
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -28,12 +28,20 @@ import ( | ||
| ) | ||
| func main() { | ||
| ctx := context.Background() | ||
| statusCode := dockerMain() | ||
| if statusCode != 0 { | ||
| os.Exit(statusCode) | ||
| } | ||
| } | ||
| func dockerMain() int { | ||
| ctx, cancelNotify := signal.NotifyContext(context.Background(), platformsignals.TerminationSignals...) | ||
| defer cancelNotify() | ||
| dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx)) | ||
| if err != nil { | ||
| fmt.Fprintln(os.Stderr, err) | ||
| os.Exit(1) | ||
| return 1 | ||
| } | ||
| logrus.SetOutput(dockerCli.Err()) | ||
| otel.SetErrorHandler(debug.OTELErrorHandler) | ||
| @@ -46,16 +54,17 @@ func main() { | ||
| // StatusError should only be used for errors, and all errors should | ||
| // have a non-zero exit status, so never exit with 0 | ||
| if sterr.StatusCode == 0 { | ||
| os.Exit(1) | ||
| return 1 | ||
| } | ||
| os.Exit(sterr.StatusCode) | ||
| return sterr.StatusCode | ||
| } | ||
| if errdefs.IsCancelled(err) { | ||
| os.Exit(0) | ||
| return 0 | ||
| } | ||
| fmt.Fprintln(dockerCli.Err(), err) | ||
| os.Exit(1) | ||
| return 1 | ||
| } | ||
| return 0 | ||
| } | ||
| func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand { | ||
| @@ -224,7 +233,7 @@ func setValidateArgs(dockerCli command.Cli, cmd *cobra.Command) { | ||
| }) | ||
| } | ||
| func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string, envs []string) error { | ||
| func tryPluginRun(ctx context.Context, dockerCli command.Cli, cmd *cobra.Command, subcommand string, envs []string) error { | ||
| plugincmd, err := pluginmanager.PluginRunCommand(dockerCli, subcommand, cmd) | ||
| if err != nil { | ||
| return err | ||
| @@ -242,40 +251,56 @@ func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string, | ||
| // Background signal handling logic: block on the signals channel, and | ||
| // notify the plugin via the PluginServer (or signal) as appropriate. | ||
| const exitLimit = 3 | ||
| signals := make(chan os.Signal, exitLimit) | ||
| signal.Notify(signals, platformsignals.TerminationSignals...) | ||
| const exitLimit = 2 | ||
Benehiko marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| tryTerminatePlugin := func(force bool) { | ||
| // If stdin is a TTY, the kernel will forward | ||
| // signals to the subprocess because the shared | ||
| // pgid makes the TTY a controlling terminal. | ||
| // | ||
| // The plugin should have it's own copy of this | ||
| // termination logic, and exit after 3 retries | ||
| // on it's own. | ||
| if dockerCli.Out().IsTerminal() { | ||
| return | ||
| } | ||
| // Terminate the plugin server, which will | ||
| // close all connections with plugin | ||
| // subprocesses, and signal them to exit. | ||
| // | ||
| // Repeated invocations will result in EINVAL, | ||
| // or EBADF; but that is fine for our purposes. | ||
| _ = srv.Close() | ||
| // force the process to terminate if it hasn't already | ||
| if force { | ||
| _ = plugincmd.Process.Kill() | ||
| _, _ = fmt.Fprint(dockerCli.Err(), "got 3 SIGTERM/SIGINTs, forcefully exiting\n") | ||
| os.Exit(1) | ||
| } | ||
| } | ||
| go func() { | ||
| retries := 0 | ||
| for range signals { | ||
| // If stdin is a TTY, the kernel will forward | ||
| // signals to the subprocess because the shared | ||
| // pgid makes the TTY a controlling terminal. | ||
| // | ||
| // The plugin should have it's own copy of this | ||
| // termination logic, and exit after 3 retries | ||
| // on it's own. | ||
| if dockerCli.Out().IsTerminal() { | ||
| continue | ||
| } | ||
| force := false | ||
| // catch the first signal through context cancellation | ||
| <-ctx.Done() | ||
| tryTerminatePlugin(force) | ||
| // Terminate the plugin server, which will | ||
| // close all connections with plugin | ||
| // subprocesses, and signal them to exit. | ||
| // | ||
| // Repeated invocations will result in EINVAL, | ||
| // or EBADF; but that is fine for our purposes. | ||
| _ = srv.Close() | ||
| // register subsequent signals | ||
| signals := make(chan os.Signal, exitLimit) | ||
| signal.Notify(signals, platformsignals.TerminationSignals...) | ||
| for range signals { | ||
| retries++ | ||
| // If we're still running after 3 interruptions | ||
| // (SIGINT/SIGTERM), send a SIGKILL to the plugin as a | ||
| // final attempt to terminate, and exit. | ||
| retries++ | ||
| if retries >= exitLimit { | ||
| _, _ = fmt.Fprintf(dockerCli.Err(), "got %d SIGTERM/SIGINTs, forcefully exiting\n", retries) | ||
| _ = plugincmd.Process.Kill() | ||
| os.Exit(1) | ||
| force = true | ||
| } | ||
| tryTerminatePlugin(force) | ||
| } | ||
| }() | ||
| @@ -338,7 +363,7 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error { | ||
| ccmd, _, err := cmd.Find(args) | ||
| subCommand = ccmd | ||
| if err != nil || pluginmanager.IsPluginCommand(ccmd) { | ||
| err := tryPluginRun(dockerCli, cmd, args[0], envs) | ||
| err := tryPluginRun(ctx, dockerCli, cmd, args[0], envs) | ||
| if err == nil { | ||
| if dockerCli.HooksEnabled() && dockerCli.Out().IsTerminal() && ccmd != nil { | ||
| pluginmanager.RunPluginHooks(ctx, dockerCli, cmd, ccmd, args) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.