Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit d720eb6

Browse files
committed
Fix race (parallel update of collection) to remove orphan containers
Signed-off-by: Guillaume Tardif <[email protected]>
1 parent fbe1ebc commit d720eb6

File tree

3 files changed

+5
-21
lines changed

3 files changed

+5
-21
lines changed

local/compose/containers.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,6 @@ func (containers Containers) filter(predicate containerPredicate) Containers {
7070
return filtered
7171
}
7272

73-
// split return Containers with elements to match and those not to match predicate
74-
func (containers Containers) split(predicate containerPredicate) (Containers, Containers) {
75-
var right Containers
76-
var left Containers
77-
for _, c := range containers {
78-
if predicate(c) {
79-
right = append(right, c)
80-
} else {
81-
left = append(left, c)
82-
}
83-
}
84-
return right, left
85-
}
86-
8773
func (containers Containers) names() []string {
8874
var names []string
8975
for _, c := range containers {

local/compose/down.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
5757
}
5858

5959
err = InReverseDependencyOrder(ctx, options.Project, func(c context.Context, service types.ServiceConfig) error {
60-
serviceContainers, others := containers.split(isService(service.Name))
60+
serviceContainers := containers.filter(isService(service.Name))
6161
err := s.removeContainers(ctx, w, serviceContainers)
62-
containers = others
6362
return err
6463
})
6564
if err != nil {
6665
return err
6766
}
6867

69-
if options.RemoveOrphans && len(containers) > 0 {
70-
err := s.removeContainers(ctx, w, containers)
68+
orphans := containers.filter(isNotService(options.Project.ServiceNames()...))
69+
if options.RemoveOrphans && len(orphans) > 0 {
70+
err := s.removeContainers(ctx, w, orphans)
7171
if err != nil {
7272
return err
7373
}

local/compose/down_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ func TestDownRemoveOrphans(t *testing.T) {
6464

6565
ctx := context.Background()
6666
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
67-
[]apitypes.Container{testContainer("service1", "123"),
68-
testContainer("service2", "789"),
69-
testContainer("service_orphan", "321")}, nil).Times(2)
67+
[]apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
7068

7169
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
7270
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)

0 commit comments

Comments
 (0)