From 31644d5ea7d7afa41d51cfe4e9b01f51dc112a02 Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Sun, 5 May 2024 22:45:37 +0100 Subject: [PATCH 1/2] Fix hang when container fails to start Signed-off-by: Laura Brehm --- cli/command/container/run.go | 6 +++++- cli/command/container/utils.go | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 2c4e8d41ea..0da127c463 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -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 { diff --git a/cli/command/container/utils.go b/cli/command/container/utils.go index 9662ac027f..794c0db609 100644 --- a/cli/command/container/utils.go +++ b/cli/command/container/utils.go @@ -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 From 8d6e571c03f35e93fbef4946fab553e25d8f92d9 Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Mon, 6 May 2024 12:14:41 +0100 Subject: [PATCH 2/2] Add e2e tests for run w/ bad entrypoint Signed-off-by: Laura Brehm --- e2e/container/run_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/e2e/container/run_test.go b/e2e/container/run_test.go index cffd1aeb8d..fa8ea72b03 100644 --- a/e2e/container/run_test.go +++ b/e2e/container/run_test.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" "testing" + "time" "github.com/docker/cli/e2e/internal/fixtures" "github.com/docker/cli/internal/test/environment" @@ -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())