Преглед изворни кода

Merge pull request #10104 from ndeloof/logs_race_condition

fix race condition on compose logs
Guillaume Lours пре 3 година
родитељ
комит
7cf6d5ec4e
3 измењених фајлова са 10 додато и 11 уклоњено
  1. 7 5
      pkg/compose/logs.go
  2. 2 5
      pkg/compose/printer.go
  3. 1 1
      pkg/compose/up.go

+ 7 - 5
pkg/compose/logs.go

@@ -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,
@@ -74,7 +79,7 @@ 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),
@@ -82,10 +87,7 @@ func (s *composeService) Logs(
 				})
 				return s.logContainers(ctx, consumer, c, options)
 			})
-		})
-
-		eg.Go(func() error {
-			_, err := printer.Run(ctx, false, "", nil)
+			printer.Stop()
 			return err
 		})
 	}

+ 2 - 5
pkg/compose/printer.go

@@ -17,7 +17,6 @@
 package compose
 
 import (
-	"context"
 	"fmt"
 
 	"github.com/docker/compose/v2/pkg/api"
@@ -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()
 }
@@ -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
@@ -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 {

+ 1 - 1
pkg/compose/up.go

@@ -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
 	})