Skip to content

Commit

Permalink
Merge pull request #11361 from laurazard/always-handle-signals
Browse files Browse the repository at this point in the history
signals/utils: always handle received signals
  • Loading branch information
glours authored Jan 18, 2024
2 parents d688d3b + 898e1b6 commit fb6d922
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ 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 @@ -74,20 +73,17 @@ type CobraCommand func(context.Context, *cobra.Command, []string) error
// AdaptCmd adapt a CobraCommand func to cobra library
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.Contains(contextString, ".WithCancel") || plugin.RunningStandalone() { // need to handle cancel
cancellableCtx, cancel := context.WithCancel(cmd.Context())
ctx = cancellableCtx
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-s
cancel()
signal.Stop(s)
close(s)
}()
}
ctx, cancel := context.WithCancel(cmd.Context())

s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-s
cancel()
signal.Stop(s)
close(s)
}()

err := fn(ctx, cmd, args)
var composeErr compose.Error
if api.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) {
Expand Down

0 comments on commit fb6d922

Please sign in to comment.