fix race condition on compose logs

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2022-12-20 09:21:31 +01:00
parent 89ef8198f3
commit 0ab5079c1a
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
3 changed files with 10 additions and 11 deletions

View File

@ -65,6 +65,11 @@ func (s *composeService) Logs(
if options.Follow { if options.Follow {
printer := newLogPrinter(consumer) printer := newLogPrinter(consumer)
eg.Go(func() error {
_, err := printer.Run(false, "", nil)
return err
})
for _, c := range containers { for _, c := range containers {
printer.HandleEvent(api.ContainerEvent{ printer.HandleEvent(api.ContainerEvent{
Type: api.ContainerEventAttach, Type: api.ContainerEventAttach,
@ -74,7 +79,7 @@ func (s *composeService) Logs(
} }
eg.Go(func() error { 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{ printer.HandleEvent(api.ContainerEvent{
Type: api.ContainerEventAttach, Type: api.ContainerEventAttach,
Container: getContainerNameWithoutProject(c), Container: getContainerNameWithoutProject(c),
@ -82,10 +87,7 @@ func (s *composeService) Logs(
}) })
return s.logContainers(ctx, consumer, c, options) return s.logContainers(ctx, consumer, c, options)
}) })
}) printer.Stop()
eg.Go(func() error {
_, err := printer.Run(ctx, false, "", nil)
return err return err
}) })
} }

View File

@ -17,7 +17,6 @@
package compose package compose
import ( import (
"context"
"fmt" "fmt"
"github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/api"
@ -26,7 +25,7 @@ import (
// logPrinter watch application containers an collect their logs // logPrinter watch application containers an collect their logs
type logPrinter interface { type logPrinter interface {
HandleEvent(event api.ContainerEvent) 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() Cancel()
Stop() Stop()
} }
@ -64,7 +63,7 @@ func (p *printer) HandleEvent(event api.ContainerEvent) {
} }
//nolint:gocyclo //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 ( var (
aborting bool aborting bool
exitCode int exitCode int
@ -74,8 +73,6 @@ func (p *printer) Run(ctx context.Context, cascadeStop bool, exitCodeFrom string
select { select {
case <-p.stopCh: case <-p.stopCh:
return exitCode, nil return exitCode, nil
case <-ctx.Done():
return exitCode, ctx.Err()
case event := <-p.queue: case event := <-p.queue:
container := event.Container container := event.Container
switch event.Type { switch event.Type {

View File

@ -81,7 +81,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
var exitCode int var exitCode int
eg, ctx := errgroup.WithContext(ctx) eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error { 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 exitCode = code
return err return err
}) })