Merge pull request #5060 from laurazard/fix-hang-ctx

Fix hang when container fails to start
This commit is contained in:
Sebastiaan van Stijn 2024-05-07 12:51:30 +02:00 committed by GitHub
commit 0bdc20ecbe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 1 deletions

View File

@ -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 {

View File

@ -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

View File

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