Skip to content

Commit

Permalink
Merge pull request #5062 from laurazard/cherry-pick-run-hang
Browse files Browse the repository at this point in the history
[26.1 backport] Fix hang when container fails to start
  • Loading branch information
vvoland authored May 7, 2024
2 parents 4cf5afa + 4add46d commit 53a3f0b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cli/command/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ func runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOption
defer closeFn()
}

statusChan := waitExitOrRemoved(ctx, apiClient, containerID, copts.autoRemove)
// New context here because we don't to cancel waiting on container exit/remove
// when we cancel attach, etc.
statusCtx, cancelStatusCtx := context.WithCancel(context.WithoutCancel(ctx))
defer cancelStatusCtx()
statusChan := waitExitOrRemoved(statusCtx, apiClient, containerID, copts.autoRemove)

// start the container
if err := apiClient.ContainerStart(ctx, containerID, container.StartOptions{}); err != nil {
Expand Down
1 change: 1 addition & 0 deletions cli/command/container/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func waitExitOrRemoved(ctx context.Context, apiClient client.APIClient, containe

statusC := make(chan int)
go func() {
defer close(statusC)
select {
case <-ctx.Done():
return
Expand Down
17 changes: 17 additions & 0 deletions e2e/container/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/docker/cli/e2e/internal/fixtures"
"github.com/docker/cli/internal/test/environment"
Expand Down Expand Up @@ -34,6 +35,22 @@ func TestRunAttachedFromRemoteImageAndRemove(t *testing.T) {
golden.Assert(t, result.Stderr(), "run-attached-from-remote-and-remove.golden")
}

// Regression test for https://github.com/docker/cli/issues/5053
func TestRunInvalidEntrypointWithAutoremove(t *testing.T) {
environment.SkipIfDaemonNotLinux(t)

result := make(chan *icmd.Result)
go func() {
result <- icmd.RunCommand("docker", "run", "--rm", fixtures.AlpineImage, "invalidcommand")
}()
select {
case r := <-result:
r.Assert(t, icmd.Expected{ExitCode: 127})
case <-time.After(4 * time.Second):
t.Fatal("test took too long, shouldn't hang")
}
}

func TestRunWithContentTrust(t *testing.T) {
skip.If(t, environment.RemoteDaemon())

Expand Down

0 comments on commit 53a3f0b

Please sign in to comment.