Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Up: teardown when command context is cancelled #11292

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/docker/cli/cli-plugins/plugin"
"github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/compose"
Expand Down Expand Up @@ -75,7 +76,7 @@ func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
contextString := fmt.Sprintf("%s", ctx)
if !strings.HasSuffix(contextString, ".WithCancel") { // need to handle cancel
if !strings.Contains(contextString, ".WithCancel") || plugin.RunningStandalone() { // need to handle cancel
cancellableCtx, cancel := context.WithCancel(cmd.Context())
ctx = cancellableCtx
s := make(chan os.Signal, 1)
Expand Down
33 changes: 20 additions & 13 deletions pkg/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"github.com/hashicorp/go-multierror"
)

func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error {
func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error { //nolint:gocyclo
err := progress.Run(ctx, tracing.SpanWrapFunc("project/up", tracing.ProjectOptions(project), func(ctx context.Context) error {
err := s.create(ctx, project, options.Create)
if err != nil {
Expand Down Expand Up @@ -69,24 +69,31 @@
doneCh := make(chan bool)
eg.Go(func() error {
first := true
gracefulTeardown := func() {
printer.Cancel()
fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
eg.Go(func() error {
err := s.Stop(context.Background(), project.Name, api.StopOptions{
Services: options.Create.Services,
Project: project,
})
isTerminated = true
close(doneCh)
return err
})
first = false
}
for {
select {
case <-doneCh:
return nil
case <-ctx.Done():
if first {
gracefulTeardown()
}

Check warning on line 93 in pkg/compose/up.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/up.go#L92-L93

Added lines #L92 - L93 were not covered by tests
case <-signalChan:
if first {
printer.Cancel()
fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
eg.Go(func() error {
err := s.Stop(context.Background(), project.Name, api.StopOptions{
Services: options.Create.Services,
Project: project,
})
isTerminated = true
close(doneCh)
return err
})
first = false
gracefulTeardown()
} else {
eg.Go(func() error {
return s.Kill(context.Background(), project.Name, api.KillOptions{
Expand Down