Skip to content

Commit

Permalink
Merge pull request #10104 from ndeloof/logs_race_condition
Browse files Browse the repository at this point in the history
fix race condition on compose logs
  • Loading branch information
glours committed Dec 20, 2022
2 parents 89ef819 + 0ab5079 commit 7cf6d5e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
12 changes: 7 additions & 5 deletions pkg/compose/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (s *composeService) Logs(

if options.Follow {
printer := newLogPrinter(consumer)
eg.Go(func() error {
_, err := printer.Run(false, "", nil)
return err
})

for _, c := range containers {
printer.HandleEvent(api.ContainerEvent{
Type: api.ContainerEventAttach,
Expand All @@ -74,18 +79,15 @@ func (s *composeService) Logs(
}

eg.Go(func() error {
return s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container) error {
err := s.watchContainers(ctx, projectName, options.Services, nil, printer.HandleEvent, containers, func(c types.Container) error {
printer.HandleEvent(api.ContainerEvent{
Type: api.ContainerEventAttach,
Container: getContainerNameWithoutProject(c),
Service: c.Labels[api.ServiceLabel],
})
return s.logContainers(ctx, consumer, c, options)
})
})

eg.Go(func() error {
_, err := printer.Run(ctx, false, "", nil)
printer.Stop()
return err
})
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/compose/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package compose

import (
"context"
"fmt"

"github.com/docker/compose/v2/pkg/api"
Expand All @@ -26,7 +25,7 @@ import (
// logPrinter watch application containers an collect their logs
type logPrinter interface {
HandleEvent(event api.ContainerEvent)
Run(ctx context.Context, cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error)
Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error)
Cancel()
Stop()
}
Expand Down Expand Up @@ -64,7 +63,7 @@ func (p *printer) HandleEvent(event api.ContainerEvent) {
}

//nolint:gocyclo
func (p *printer) Run(ctx context.Context, cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
var (
aborting bool
exitCode int
Expand All @@ -74,8 +73,6 @@ func (p *printer) Run(ctx context.Context, cascadeStop bool, exitCodeFrom string
select {
case <-p.stopCh:
return exitCode, nil
case <-ctx.Done():
return exitCode, ctx.Err()
case event := <-p.queue:
container := event.Container
switch event.Type {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
var exitCode int
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
code, err := printer.Run(context.Background(), options.Start.CascadeStop, options.Start.ExitCodeFrom, stopFunc)
code, err := printer.Run(options.Start.CascadeStop, options.Start.ExitCodeFrom, stopFunc)
exitCode = code
return err
})
Expand Down

0 comments on commit 7cf6d5e

Please sign in to comment.