diff --git a/cli/command/container/attach.go b/cli/command/container/attach.go index d5303485f011..a4dbe0ec0d11 100644 --- a/cli/command/container/attach.go +++ b/cli/command/container/attach.go @@ -72,7 +72,8 @@ func RunAttach(ctx context.Context, dockerCLI command.Cli, containerID string, o apiClient := dockerCLI.Client() // request channel to wait for client - resultC, errC := apiClient.ContainerWait(ctx, containerID, "") + waitCtx := context.WithoutCancel(ctx) + resultC, errC := apiClient.ContainerWait(waitCtx, containerID, "") c, err := inspectContainerAndCheckState(ctx, apiClient, containerID) if err != nil { @@ -163,9 +164,6 @@ func getExitStatus(errC <-chan error, resultC <-chan container.WaitResponse) err return cli.StatusError{StatusCode: int(result.StatusCode)} } case err := <-errC: - if errors.Is(err, context.Canceled) { - return nil - } return err } diff --git a/cli/command/container/attach_test.go b/cli/command/container/attach_test.go index 6ffa9ff3d77c..5530cca36f44 100644 --- a/cli/command/container/attach_test.go +++ b/cli/command/container/attach_test.go @@ -86,11 +86,7 @@ func TestNewAttachCommandErrors(t *testing.T) { } func TestGetExitStatus(t *testing.T) { - var ( - expectedErr = errors.New("unexpected error") - errC = make(chan error, 1) - resultC = make(chan container.WaitResponse, 1) - ) + expectedErr := errors.New("unexpected error") testcases := []struct { result *container.WaitResponse @@ -119,19 +115,26 @@ func TestGetExitStatus(t *testing.T) { expectedError: cli.StatusError{StatusCode: 15}, }, { + result: &container.WaitResponse{ + StatusCode: 130, + }, err: context.Canceled, - expectedError: nil, + expectedError: cli.StatusError{StatusCode: 130}, }, } for _, testcase := range testcases { + errC := make(chan error, 1) + resultC := make(chan container.WaitResponse, 1) if testcase.err != nil { errC <- testcase.err } if testcase.result != nil { resultC <- *testcase.result } + err := getExitStatus(errC, resultC) + if testcase.expectedError == nil { assert.NilError(t, err) } else { diff --git a/e2e/container/attach_test.go b/e2e/container/attach_test.go index 6822ca60f776..e0fcbba829ee 100644 --- a/e2e/container/attach_test.go +++ b/e2e/container/attach_test.go @@ -32,7 +32,7 @@ func withStdinNewline(cmd *icmd.Cmd) { // Regression test for https://github.com/docker/cli/issues/5294 func TestAttachInterrupt(t *testing.T) { - result := icmd.RunCommand("docker", "run", "-d", fixtures.AlpineImage, "sh", "-c", "sleep 5") + result := icmd.RunCommand("docker", "run", "-d", fixtures.AlpineImage, "sh", "-c", "while true; do sleep 1; done") result.Assert(t, icmd.Success) containerID := strings.TrimSpace(result.Stdout()) @@ -49,6 +49,6 @@ func TestAttachInterrupt(t *testing.T) { c.Process.Signal(os.Interrupt) _ = c.Wait() - assert.Equal(t, c.ProcessState.ExitCode(), 0) - assert.Equal(t, d.String(), "") + assert.Equal(t, c.ProcessState.ExitCode(), 130) + assert.Equal(t, d.String(), "exit status 130\n") }