Skip to content

Commit

Permalink
fix(compose): container locking
Browse files Browse the repository at this point in the history
Fix container locking which was duplicated and held for the duration of
the concurrent operation so preventing any concurrency.
  • Loading branch information
stevenh committed Jul 30, 2024
1 parent 1734ffc commit dc283ad
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions modules/compose/compose_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ type dockerCompose struct {
// only one strategy can be added to a service, to use multiple use wait.ForAll(...)
waitStrategies map[string]wait.Strategy

// used to synchronise writes to the containers map
containersLock sync.RWMutex
// Used to synchronise writes to the containers.
containersLock sync.Mutex

// cache for containers that are part of the stack
// used in ServiceContainer(...) function to avoid calls to the Docker API
Expand Down Expand Up @@ -391,10 +391,6 @@ func (d *dockerCompose) Up(ctx context.Context, opts ...StackUpOption) (err erro
termSignals = append(termSignals, termSignal)
}

d.containersLock.Lock()
defer d.containersLock.Unlock()
d.containers[srv.Name] = dc

return nil
})
}
Expand All @@ -420,11 +416,6 @@ func (d *dockerCompose) Up(ctx context.Context, opts ...StackUpOption) (err erro
return err
}

// cache all the containers on compose.up
d.containersLock.Lock()
defer d.containersLock.Unlock()
d.containers[svc] = target

return strategy.WaitUntilReady(errGrpCtx, target)
})
}
Expand Down Expand Up @@ -511,6 +502,8 @@ func (d *dockerCompose) lookupContainer(ctx context.Context, svcName string) (*t

ctr.SetProvider(dockerProvider)

d.containersLock.Lock()
defer d.containersLock.Unlock()
d.containers[svcName] = ctr

return ctr, nil
Expand All @@ -520,9 +513,6 @@ func (d *dockerCompose) lookupContainer(ctx context.Context, svcName string) (*t
//
// Safe for concurrent calls.
func (d *dockerCompose) lookupNetworks(ctx context.Context) error {
d.containersLock.Lock()
defer d.containersLock.Unlock()

networks, err := d.dockerClient.NetworkList(ctx, dockernetwork.ListOptions{
Filters: filters.NewArgs(
filters.Arg("label", fmt.Sprintf("%s=%s", api.ProjectLabel, d.name)),
Expand Down

0 comments on commit dc283ad

Please sign in to comment.