Skip to content

fix: data race in docker client Info() #1779

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commons-test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test-%:
--packages="./..." \
--junitfile TEST-unit.xml \
-- \
-coverprofile=coverage.out \
-coverprofile=coverage.out \
-timeout=30m

.PHONY: tools
Expand Down
51 changes: 27 additions & 24 deletions docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testcontainers

import (
"context"
"fmt"
"sync"

"github.com/docker/docker/api/types"
Expand All @@ -20,8 +21,10 @@ type DockerClient struct {
}

var (
dockerInfo types.Info // dockerInfo stores the docker info to be reused in the Info method
dockerInfoOnce sync.Once
// dockerInfo stores the docker info to be reused in the Info method
dockerInfo types.Info
dockerInfoSet bool
dockerInfoLock sync.Mutex
)

// implements SystemAPIClient interface
Expand All @@ -36,14 +39,20 @@ func (c *DockerClient) Events(ctx context.Context, options types.EventsOptions)
// and reused every time Info is called.
// It will also print out the docker server info, and the resolved Docker paths, to the default logger.
func (c *DockerClient) Info(ctx context.Context) (types.Info, error) {
var err error
dockerInfoOnce.Do(func() {
dockerInfo, err = c.Client.Info(ctx)
if err != nil {
return
}
dockerInfoLock.Lock()
defer dockerInfoLock.Unlock()
if dockerInfoSet {
return dockerInfo, nil
}

infoMessage := `%v - Connected to docker:
info, err := c.Client.Info(ctx)
if err != nil {
return info, fmt.Errorf("failed to retrieve docker info: %w", err)
}
dockerInfo = info
dockerInfoSet = true

infoMessage := `%v - Connected to docker:
Server Version: %v
API Version: %v
Operating System: %v
Expand All @@ -54,22 +63,16 @@ func (c *DockerClient) Info(ctx context.Context) (types.Info, error) {
Test ProcessID: %s
`

Logger.Printf(infoMessage, packagePath,
dockerInfo.ServerVersion, c.Client.ClientVersion(),
dockerInfo.OperatingSystem, dockerInfo.MemTotal/1024/1024,
testcontainersdocker.ExtractDockerHost(ctx),
testcontainersdocker.ExtractDockerSocket(ctx),
testcontainerssession.SessionID(),
testcontainerssession.ProcessID(),
)
})

if err != nil {
// reset the state of the sync.Once so that the next call to Info will try again
dockerInfoOnce = sync.Once{}
}
Logger.Printf(infoMessage, packagePath,
dockerInfo.ServerVersion, c.Client.ClientVersion(),
dockerInfo.OperatingSystem, dockerInfo.MemTotal/1024/1024,
testcontainersdocker.ExtractDockerHost(ctx),
testcontainersdocker.ExtractDockerSocket(ctx),
testcontainerssession.SessionID(),
testcontainerssession.ProcessID(),
)

return dockerInfo, err
return dockerInfo, nil
}

// RegistryLogin logs into a Docker registry.
Expand Down
41 changes: 41 additions & 0 deletions docker_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package testcontainers

import (
"context"
"sync"
"testing"

"github.com/stretchr/testify/require"
)

func TestGetDockerInfo(t *testing.T) {
t.Run("works", func(t *testing.T) {
ctx := context.Background()
c, err := NewDockerClientWithOpts(ctx)
require.NoError(t, err)

info, err := c.Info(ctx)
require.NoError(t, err)
require.NotNil(t, info)
})

t.Run("is goroutine safe", func(t *testing.T) {
ctx := context.Background()
c, err := NewDockerClientWithOpts(ctx)
require.NoError(t, err)

count := 1024
wg := sync.WaitGroup{}
wg.Add(count)

for i := 0; i < count; i++ {
go func() {
defer wg.Done()
info, err := c.Info(ctx)
require.NoError(t, err)
require.NotNil(t, info)
}()
}
wg.Wait()
})
}