-
Notifications
You must be signed in to change notification settings - Fork 2.1k
cmd/docker: add cause to user-terminated context.Context
#5760
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,16 +28,57 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "go.opentelemetry.io/otel" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type errCtxSignalTerminated struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| signal os.Signal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (e errCtxSignalTerminated) Error() string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+37
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a small nit/suggestion, not necessary. You could do:
Suggested change
Then
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 on the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The suggestion looks alright to me, although we only use the exit code inside the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func main() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err := dockerMain(context.Background()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx := context.Background() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err := dockerMain(ctx) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if errors.As(err, &errCtxSignalTerminated{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Benehiko marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| os.Exit(getExitCode(err)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil && !errdefs.IsCancelled(err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _, _ = fmt.Fprintln(os.Stderr, err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| os.Exit(getExitCode(err)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func notifyContext(ctx context.Context, signals ...os.Signal) (context.Context, context.CancelFunc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ch := make(chan os.Signal, 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| signal.Notify(ch, signals...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctxCause, cancel := context.WithCancelCause(ctx) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| go func() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| select { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case <-ctx.Done(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| signal.Stop(ch) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case sig := <-ch: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cancel(errCtxSignalTerminated{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| signal: sig, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| signal.Stop(ch) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ctxCause, func() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| signal.Stop(ch) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cancel(nil) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func dockerMain(ctx context.Context) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx, cancelNotify := signal.NotifyContext(ctx, platformsignals.TerminationSignals...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx, cancelNotify := notifyContext(ctx, platformsignals.TerminationSignals...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer cancelNotify() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -57,6 +98,16 @@ func getExitCode(err error) int { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var userTerminatedErr errCtxSignalTerminated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if errors.As(err, &userTerminatedErr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s, ok := userTerminatedErr.signal.(syscall.Signal) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 128 + int(s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var stErr cli.StatusError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if errors.As(err, &stErr) && stErr.StatusCode != 0 { // FIXME(thaJeztah): StatusCode should never be used with a zero status-code. Check if we do this anywhere. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return stErr.StatusCode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.