mirror of https://github.com/docker/cli.git
attach: don't return context cancelled error
In3f0d90a2a9
we introduced a global signal handler and made sure all the contexts passed into command execution get (appropriately) cancelled when we get a SIGINT. Due to that change, and how we use this context during `docker attach`, we started to return the context cancelation error when a user signals the running `docker attach`. Since this is the intended behavior, we shouldn't return an error, so this commit adds checks to ignore this specific error in this case. Also adds a regression test. Signed-off-by: Laura Brehm <laurabrehm@hey.com> (cherry picked from commit66aa0f672c
) Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
parent
2e506cbb10
commit
dfb8f2155a
|
@ -146,7 +146,8 @@ func RunAttach(ctx context.Context, dockerCLI command.Cli, containerID string, o
|
||||||
detachKeys: options.DetachKeys,
|
detachKeys: options.DetachKeys,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := streamer.stream(ctx); err != nil {
|
// if the context was canceled, this was likely intentional and we shouldn't return an error
|
||||||
|
if err := streamer.stream(ctx); err != nil && !errors.Is(err, context.Canceled) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,6 +164,9 @@ func getExitStatus(errC <-chan error, resultC <-chan container.WaitResponse) err
|
||||||
return cli.StatusError{StatusCode: int(result.StatusCode)}
|
return cli.StatusError{StatusCode: int(result.StatusCode)}
|
||||||
}
|
}
|
||||||
case err := <-errC:
|
case err := <-errC:
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -113,6 +114,10 @@ func TestGetExitStatus(t *testing.T) {
|
||||||
},
|
},
|
||||||
expectedError: cli.StatusError{StatusCode: 15},
|
expectedError: cli.StatusError{StatusCode: 15},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
err: context.Canceled,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testcase := range testcases {
|
for _, testcase := range testcases {
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/creack/pty"
|
||||||
"github.com/docker/cli/e2e/internal/fixtures"
|
"github.com/docker/cli/e2e/internal/fixtures"
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
"gotest.tools/v3/icmd"
|
"gotest.tools/v3/icmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -23,3 +29,26 @@ func TestAttachExitCode(t *testing.T) {
|
||||||
func withStdinNewline(cmd *icmd.Cmd) {
|
func withStdinNewline(cmd *icmd.Cmd) {
|
||||||
cmd.Stdin = strings.NewReader("\n")
|
cmd.Stdin = strings.NewReader("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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.Assert(t, icmd.Success)
|
||||||
|
containerID := strings.TrimSpace(result.Stdout())
|
||||||
|
|
||||||
|
// run it as such so we can signal it later
|
||||||
|
c := exec.Command("docker", "attach", containerID)
|
||||||
|
d := bytes.Buffer{}
|
||||||
|
c.Stdout = &d
|
||||||
|
c.Stderr = &d
|
||||||
|
_, err := pty.Start(c)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
|
||||||
|
// have to wait a bit to give time for the command to execute/print
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
c.Process.Signal(os.Interrupt)
|
||||||
|
|
||||||
|
_ = c.Wait()
|
||||||
|
assert.Equal(t, c.ProcessState.ExitCode(), 0)
|
||||||
|
assert.Equal(t, d.String(), "")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue