Skip to content
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

cmd/docker: add cause to user-terminated context.Context #5760

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,57 @@ import (
"go.opentelemetry.io/otel"
)

type errCtxSignalTerminated struct {
signal os.Signal
}

func (e errCtxSignalTerminated) Error() string {
return ""
Benehiko marked this conversation as resolved.
Show resolved Hide resolved
}
Comment on lines +31 to +37
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small nit/suggestion, not necessary.

You could do:

Suggested change
type errCtxSignalTerminated struct {
signal os.Signal
}
func (e errCtxSignalTerminated) Error() string {
return ""
}
type errCtxSignalTerminated struct {
signal os.Signal
}
func (e *errCtxSignalTerminated) Error() string {
return ""
}
func (e *errCtxSignalTerminated) exitCode() int {
if e == nil {
return 1
}
s, ok := e.signal.(syscall.Signal)
if !ok {
return 1
}
return 128 + int(s)
}

Then errCtxSignalTerminated implements error (instead of *errCtxSignalTerminated implementing error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on the exitCode

Copy link
Member Author

Choose a reason for hiding this comment

The 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 getExitCode function and this error type is only used inside cmd/docker/docker.go. At least to me, it just seems like extra refactoring with little benefit.


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))
Expand All @@ -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
Expand Down
33 changes: 33 additions & 0 deletions cmd/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (
"context"
"io"
"os"
"syscall"
"testing"
"time"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/debug"
platformsignals "github.com/docker/cli/cmd/docker/internal/signals"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
Expand Down Expand Up @@ -75,3 +79,32 @@ func TestVersion(t *testing.T) {
assert.NilError(t, err)
assert.Check(t, is.Contains(b.String(), "Docker version"))
}

func TestUserTerminatedError(t *testing.T) {
ctx, cancel := context.WithTimeoutCause(context.Background(), time.Second*1, errors.New("test timeout"))
t.Cleanup(cancel)

notifyCtx, cancelNotify := notifyContext(ctx, platformsignals.TerminationSignals...)
t.Cleanup(cancelNotify)

syscall.Kill(syscall.Getpid(), syscall.SIGINT)

<-notifyCtx.Done()
assert.ErrorIs(t, context.Cause(notifyCtx), errCtxSignalTerminated{
signal: syscall.SIGINT,
Benehiko marked this conversation as resolved.
Show resolved Hide resolved
})

assert.Equal(t, getExitCode(context.Cause(notifyCtx)), 130)

notifyCtx, cancelNotify = notifyContext(ctx, platformsignals.TerminationSignals...)
t.Cleanup(cancelNotify)

syscall.Kill(syscall.Getpid(), syscall.SIGTERM)

<-notifyCtx.Done()
assert.ErrorIs(t, context.Cause(notifyCtx), errCtxSignalTerminated{
signal: syscall.SIGTERM,
})

assert.Equal(t, getExitCode(context.Cause(notifyCtx)), 143)
}
Loading