Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid double lock in DockerProvider.DaemonHost() #2900

Merged
merged 11 commits into from
Dec 20, 2024
12 changes: 11 additions & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,11 @@ func (p *DockerProvider) daemonHostLocked(ctx context.Context) (string, error) {
p.hostCache = daemonURL.Hostname()
case "unix", "npipe":
if core.InAContainer() {
ip, err := p.GetGatewayIP(ctx)
defaultNetwork, err := p.ensureDefaultNetworkLocked(ctx)
if err != nil {
return "", fmt.Errorf("ensure default network: %w", err)
}
ip, err := p.getGatewayIP(ctx, defaultNetwork)
if err != nil {
ip, err = core.DefaultGatewayIP()
if err != nil {
Expand Down Expand Up @@ -1598,7 +1602,10 @@ func (p *DockerProvider) GetGatewayIP(ctx context.Context) (string, error) {
if err != nil {
return "", fmt.Errorf("ensure default network: %w", err)
}
return p.getGatewayIP(ctx, defaultNetwork)
}

func (p *DockerProvider) getGatewayIP(ctx context.Context, defaultNetwork string) (string, error) {
stevenh marked this conversation as resolved.
Show resolved Hide resolved
nw, err := p.GetNetwork(ctx, NetworkRequest{Name: defaultNetwork})
if err != nil {
return "", err
Expand All @@ -1624,7 +1631,10 @@ func (p *DockerProvider) GetGatewayIP(ctx context.Context) (string, error) {
func (p *DockerProvider) ensureDefaultNetwork(ctx context.Context) (string, error) {
p.mtx.Lock()
defer p.mtx.Unlock()
return p.ensureDefaultNetworkLocked(ctx)
}

func (p *DockerProvider) ensureDefaultNetworkLocked(ctx context.Context) (string, error) {
if p.defaultNetwork != "" {
// Already set.
return p.defaultNetwork, nil
Expand Down
63 changes: 63 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
"log"
Expand All @@ -19,12 +20,14 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand All @@ -35,6 +38,7 @@ const (
nginxAlpineImage = "nginx:alpine"
nginxDefaultPort = "80/tcp"
nginxHighPort = "8080/tcp"
golangImage = "golang"
daemonMaxVersion = "1.41"
)

Expand All @@ -46,6 +50,65 @@ func init() {
}
}

func TestWithinContainerStage1(t *testing.T) {
// TODO: remove this skip check when context rework is merged.
if providerType != ProviderDocker {
vikstrous marked this conversation as resolved.
Show resolved Hide resolved
t.Skip("This is a docker-specific test.")
}
dir, err := os.Getwd()
require.NoError(t, err)
req := ContainerRequest{
Image: golangImage,
WorkingDir: dir,
HostConfigModifier: func(hc *container.HostConfig) {
hc.Mounts = append(hc.Mounts, mount.Mount{
Type: "bind",
Source: dir,
Target: dir,
}, mount.Mount{
Type: "bind",
Source: "/var/run",
Target: "/var/run",
})
},
Entrypoint: []string{"sleep", "100"},
}
stevenh marked this conversation as resolved.
Show resolved Hide resolved
container, err := GenericContainer(context.Background(), GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.NoError(t, err)
// The timeout is necessary because when TestWithinContainersStage2 fails, it hangs forever instead of erroring
exitCode, outputReader, err := container.Exec(context.Background(), []string{"go", "test", "-v", "-run", "^TestWithinContainerStage2$", "-timeout", "20s", "-stage2"}, exec.Multiplexed())
require.NoError(t, err)

outputBytes, err := io.ReadAll(outputReader)
require.NoError(t, err, "failed to read output")
t.Logf("%s", outputBytes)
stevenh marked this conversation as resolved.
Show resolved Hide resolved

require.Zero(t, exitCode, "non-zero exit code, output: %s", outputBytes)
}

var stage2 = flag.Bool("stage2", false, "Run TestWithinContainerStage2")

// Regression test for deadlock #2897
func TestWithinContainerStage2(t *testing.T) {
vikstrous marked this conversation as resolved.
Show resolved Hide resolved
if !*stage2 {
t.Skip("Skipping test that requires stage2")
}

req := ContainerRequest{
Image: nginxImage,
}
container, err := GenericContainer(context.Background(), GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.NoError(t, err)
_, err = container.Host(context.Background())
require.NoError(t, err)
}

func TestContainerWithHostNetworkOptions(t *testing.T) {
if os.Getenv("XDG_RUNTIME_DIR") != "" {
t.Skip("Skipping test that requires host network access when running in a container")
Expand Down