Skip to content

Commit 06af729

Browse files
authored
Merge pull request #11292 from laurazard/update-cli-signal-handling
Up: teardown when command context is cancelled
2 parents e105f16 + dcbf005 commit 06af729

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

cmd/compose/compose.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/spf13/cobra"
4343
"github.com/spf13/pflag"
4444

45+
"github.com/docker/cli/cli-plugins/plugin"
4546
"github.com/docker/compose/v2/cmd/formatter"
4647
"github.com/docker/compose/v2/pkg/api"
4748
"github.com/docker/compose/v2/pkg/compose"
@@ -75,7 +76,7 @@ func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error {
7576
return func(cmd *cobra.Command, args []string) error {
7677
ctx := cmd.Context()
7778
contextString := fmt.Sprintf("%s", ctx)
78-
if !strings.HasSuffix(contextString, ".WithCancel") { // need to handle cancel
79+
if !strings.Contains(contextString, ".WithCancel") || plugin.RunningStandalone() { // need to handle cancel
7980
cancellableCtx, cancel := context.WithCancel(cmd.Context())
8081
ctx = cancellableCtx
8182
s := make(chan os.Signal, 1)

pkg/compose/up.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"github.com/hashicorp/go-multierror"
3232
)
3333

34-
func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error {
34+
func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error { //nolint:gocyclo
3535
err := progress.Run(ctx, tracing.SpanWrapFunc("project/up", tracing.ProjectOptions(project), func(ctx context.Context) error {
3636
err := s.create(ctx, project, options.Create)
3737
if err != nil {
@@ -69,24 +69,31 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
6969
doneCh := make(chan bool)
7070
eg.Go(func() error {
7171
first := true
72+
gracefulTeardown := func() {
73+
printer.Cancel()
74+
fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
75+
eg.Go(func() error {
76+
err := s.Stop(context.Background(), project.Name, api.StopOptions{
77+
Services: options.Create.Services,
78+
Project: project,
79+
})
80+
isTerminated = true
81+
close(doneCh)
82+
return err
83+
})
84+
first = false
85+
}
7286
for {
7387
select {
7488
case <-doneCh:
7589
return nil
90+
case <-ctx.Done():
91+
if first {
92+
gracefulTeardown()
93+
}
7694
case <-signalChan:
7795
if first {
78-
printer.Cancel()
79-
fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
80-
eg.Go(func() error {
81-
err := s.Stop(context.Background(), project.Name, api.StopOptions{
82-
Services: options.Create.Services,
83-
Project: project,
84-
})
85-
isTerminated = true
86-
close(doneCh)
87-
return err
88-
})
89-
first = false
96+
gracefulTeardown()
9097
} else {
9198
eg.Go(func() error {
9299
return s.Kill(context.Background(), project.Name, api.KillOptions{

0 commit comments

Comments
 (0)