diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index 32e99b6fe..ad41cac21 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -20,6 +20,7 @@ import ( "context" "errors" "fmt" + "github.com/sirupsen/logrus" "os" "path/filepath" @@ -152,14 +153,15 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro ctx, cancel := context.WithCancel(ctx) listener := make(chan compose.ContainerExited) + exitCode := make(chan int) go func() { var aborting bool for { - <-listener + exit := <-listener if opts.cascadeStop && !aborting { aborting = true - fmt.Println("Aborting on container exit...") cancel() + exitCode <- exit.Status } } }() @@ -170,15 +172,29 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro }) if errors.Is(ctx.Err(), context.Canceled) { - fmt.Println("Gracefully stopping...") - ctx = context.Background() - _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", c.ComposeService().Stop(ctx, project) - }) + select { + case exit := <-exitCode: + fmt.Println("Aborting on container exit...") + err = stop(c, project) + logrus.Error(exit) + // os.Exit(exit) + default: + // cancelled by user + fmt.Println("Gracefully stopping...") + err = stop(c, project) + } } return err } +func stop(c *client.Client, project *types.Project) error { + ctx := context.Background() + _, err := progress.Run(ctx, func(ctx context.Context) (string, error) { + return "", c.ComposeService().Stop(ctx, project) + }) + return err +} + func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) { c, err := client.NewWithDefaultLocalBackend(ctx) if err != nil {