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
20 changes: 13 additions & 7 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
defer p.Close()

var defaultNetwork string
defaultNetwork, err = p.ensureDefaultNetwork(ctx)
defaultNetwork, err = p.ensureDefaultNetwork(ctx, false)
if err != nil {
return nil, fmt.Errorf("ensure default network: %w", err)
}
Expand Down Expand Up @@ -1498,7 +1498,7 @@ func (p *DockerProvider) daemonHostLocked(ctx context.Context) (string, error) {
p.hostCache = daemonURL.Hostname()
case "unix", "npipe":
if core.InAContainer() {
ip, err := p.GetGatewayIP(ctx)
ip, err := p.getGatewayIPMaybeLocked(ctx, true)
if err != nil {
ip, err = core.DefaultGatewayIP()
if err != nil {
Expand All @@ -1522,7 +1522,7 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
// defer the close of the Docker client connection the soonest
defer p.Close()

if _, err = p.ensureDefaultNetwork(ctx); err != nil {
if _, err = p.ensureDefaultNetwork(ctx, false); err != nil {
return nil, fmt.Errorf("ensure default network: %w", err)
}

Expand Down Expand Up @@ -1593,8 +1593,12 @@ func (p *DockerProvider) GetNetwork(ctx context.Context, req NetworkRequest) (ne
}

func (p *DockerProvider) GetGatewayIP(ctx context.Context) (string, error) {
return p.getGatewayIPMaybeLocked(ctx, false)
}

func (p *DockerProvider) getGatewayIPMaybeLocked(ctx context.Context, locked bool) (string, error) {
// Use a default network as defined in the DockerProvider
defaultNetwork, err := p.ensureDefaultNetwork(ctx)
defaultNetwork, err := p.ensureDefaultNetwork(ctx, locked)
if err != nil {
return "", fmt.Errorf("ensure default network: %w", err)
}
Expand All @@ -1621,9 +1625,11 @@ func (p *DockerProvider) GetGatewayIP(ctx context.Context) (string, error) {
// ensureDefaultNetwork ensures that defaultNetwork is set and creates
// it if it does not exist, returning its value.
// It is safe to call this method concurrently.
func (p *DockerProvider) ensureDefaultNetwork(ctx context.Context) (string, error) {
p.mtx.Lock()
defer p.mtx.Unlock()
func (p *DockerProvider) ensureDefaultNetwork(ctx context.Context, locked bool) (string, error) {
if !locked {
p.mtx.Lock()
defer p.mtx.Unlock()
}

if p.defaultNetwork != "" {
// Already set.
Expand Down
2 changes: 1 addition & 1 deletion reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (r *reaperSpawner) newReaper(ctx context.Context, sessionID string, provide

// Attach reaper container to a requested network if it is specified
if p, ok := provider.(*DockerProvider); ok {
defaultNetwork, err := p.ensureDefaultNetwork(ctx)
defaultNetwork, err := p.ensureDefaultNetwork(ctx, false)
if err != nil {
return nil, fmt.Errorf("ensure default network: %w", err)
}
Expand Down