Skip to content

Commit

Permalink
attach: wait for exit code from ContainerWait
Browse files Browse the repository at this point in the history
Such as with `docker run`, if a user CTRL-Cs while attached to a
container, we should forward the signal and wait for the exit from
`ContainerWait`, instead of just returning.

Signed-off-by: Laura Brehm <[email protected]>
  • Loading branch information
laurazard committed Jul 25, 2024
1 parent 788e996 commit 8310aec
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
6 changes: 2 additions & 4 deletions cli/command/container/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
15 changes: 5 additions & 10 deletions cli/command/container/attach_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package container

import (
"context"
"io"
"testing"

Expand Down Expand Up @@ -86,11 +85,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
Expand Down Expand Up @@ -118,20 +113,20 @@ func TestGetExitStatus(t *testing.T) {
},
expectedError: cli.StatusError{StatusCode: 15},
},
{
err: context.Canceled,
expectedError: nil,
},
}

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 {
Expand Down
11 changes: 8 additions & 3 deletions e2e/container/attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/docker/cli/e2e/internal/fixtures"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
"gotest.tools/v3/skip"
)

func TestAttachExitCode(t *testing.T) {
Expand All @@ -32,7 +33,11 @@ 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")
// this is a new test, it already did not work (inside dind) when over ssh
// todo(laurazard): make this test work w/ dind over ssh
skip.If(t, strings.Contains(os.Getenv("DOCKER_HOST"), "ssh://"))

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())

Expand All @@ -49,6 +54,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")
}

0 comments on commit 8310aec

Please sign in to comment.