Skip to content

Commit 7cf6d5e

Browse files
authored
Merge pull request docker#10104 from ndeloof/logs_race_condition
fix race condition on compose logs
2 parents 89ef819 + 0ab5079 commit 7cf6d5e

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

pkg/compose/logs.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ func (s *composeService) Logs(
6565

6666
if options.Follow {
6767
printer := newLogPrinter(consumer)
68+
eg.Go(func() error {
69+
_, err := printer.Run(false, "", nil)
70+
return err
71+
})
72+
6873
for _, c := range containers {
6974
printer.HandleEvent(api.ContainerEvent{
7075
Type: api.ContainerEventAttach,
@@ -74,18 +79,15 @@ func (s *composeService) Logs(
7479
}
7580

7681
eg.Go(func() error {
77-
return s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container) error {
82+
err := s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container) error {
7883
printer.HandleEvent(api.ContainerEvent{
7984
Type: api.ContainerEventAttach,
8085
Container: getContainerNameWithoutProject(c),
8186
Service: c.Labels[api.ServiceLabel],
8287
})
8388
return s.logContainers(ctx, consumer, c, options)
8489
})
85-
})
86-
87-
eg.Go(func() error {
88-
_, err := printer.Run(ctx, false, "", nil)
90+
printer.Stop()
8991
return err
9092
})
9193
}

pkg/compose/printer.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package compose
1818

1919
import (
20-
"context"
2120
"fmt"
2221

2322
"github.com/docker/compose/v2/pkg/api"
@@ -26,7 +25,7 @@ import (
2625
// logPrinter watch application containers an collect their logs
2726
type logPrinter interface {
2827
HandleEvent(event api.ContainerEvent)
29-
Run(ctx context.Context, cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error)
28+
Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error)
3029
Cancel()
3130
Stop()
3231
}
@@ -64,7 +63,7 @@ func (p *printer) HandleEvent(event api.ContainerEvent) {
6463
}
6564

6665
//nolint:gocyclo
67-
func (p *printer) Run(ctx context.Context, cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
66+
func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
6867
var (
6968
aborting bool
7069
exitCode int
@@ -74,8 +73,6 @@ func (p *printer) Run(ctx context.Context, cascadeStop bool, exitCodeFrom string
7473
select {
7574
case <-p.stopCh:
7675
return exitCode, nil
77-
case <-ctx.Done():
78-
return exitCode, ctx.Err()
7976
case event := <-p.queue:
8077
container := event.Container
8178
switch event.Type {

pkg/compose/up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
8181
var exitCode int
8282
eg, ctx := errgroup.WithContext(ctx)
8383
eg.Go(func() error {
84-
code, err := printer.Run(context.Background(), options.Start.CascadeStop, options.Start.ExitCodeFrom, stopFunc)
84+
code, err := printer.Run(options.Start.CascadeStop, options.Start.ExitCodeFrom, stopFunc)
8585
exitCode = code
8686
return err
8787
})

0 commit comments

Comments
 (0)