Merge pull request #11361 from laurazard/always-handle-signals

signals/utils: always handle received signals
This commit is contained in:
Guillaume Lours 2024-01-18 10:14:27 +01:00 committed by GitHub
commit fb6d92250c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -42,7 +42,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/docker/cli/cli-plugins/plugin"
"github.com/docker/compose/v2/cmd/formatter" "github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/compose" "github.com/docker/compose/v2/pkg/compose"
@ -74,11 +73,8 @@ type CobraCommand func(context.Context, *cobra.Command, []string) error
// AdaptCmd adapt a CobraCommand func to cobra library // AdaptCmd adapt a CobraCommand func to cobra library
func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error { func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context() ctx, cancel := context.WithCancel(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) s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT) signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
go func() { go func() {
@ -87,7 +83,7 @@ func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error {
signal.Stop(s) signal.Stop(s)
close(s) close(s)
}() }()
}
err := fn(ctx, cmd, args) err := fn(ctx, cmd, args)
var composeErr compose.Error var composeErr compose.Error
if api.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) { if api.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) {