Skip to content

Commit 677c5fb

Browse files
author
Fabian Holler
committed
build: fix: compose process ignores TERM signal when resolving deps
On big docker compose projects with a lot of dependencies, the docker-compose plugin process does not terminate when the process group receives a TERM or INT signal. The docker-compose process continues to run and does not react to any TERM signals. When a TERM signal is received, compose cancels the context. The code that resolves the dependency tree is not checking if the context gets canceled and does not abort. Check if the context is canceled when receiving or sending to NodeCh and terminate in that case. With this change, docker-compose returns immediately when an INT or TERM signal is received, also on the big compose project where it got stuck before.
1 parent 7c3fe35 commit 677c5fb

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

pkg/compose/dependencies.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,29 @@ func (t *graphTraversal) visit(ctx context.Context, g *Graph) error {
142142
}
143143
nodeCh := make(chan *Vertex)
144144
eg.Go(func() error {
145-
for node := range nodeCh {
146-
expect--
147-
if expect == 0 {
148-
close(nodeCh)
149-
return nil
145+
for {
146+
select {
147+
case node, ok := <-nodeCh:
148+
if !ok {
149+
return nil
150+
}
151+
expect--
152+
if expect == 0 {
153+
return nil
154+
}
155+
t.run(ctx, g, eg, t.adjacentNodesFn(node), nodeCh)
156+
157+
case <-ctx.Done():
158+
return ctx.Err()
150159
}
151-
t.run(ctx, g, eg, t.adjacentNodesFn(node), nodeCh)
152160
}
153-
return nil
154161
})
155162

156163
nodes := t.extremityNodesFn(g)
157164
t.run(ctx, g, eg, nodes, nodeCh)
158165

159166
err := eg.Wait()
167+
close(nodeCh)
160168
return err
161169
}
162170

@@ -183,8 +191,12 @@ func (t *graphTraversal) run(ctx context.Context, graph *Graph, eg *errgroup.Gro
183191
if err == nil {
184192
graph.UpdateStatus(node.Key, t.targetServiceStatus)
185193
}
186-
nodeCh <- node
187-
return err
194+
select {
195+
case nodeCh <- node:
196+
return err
197+
case <-ctx.Done():
198+
return ctx.Err()
199+
}
188200
})
189201
}
190202
}

0 commit comments

Comments
 (0)