Skip to content

Commit 898e1b6

Browse files
committed
signals/utils: always handle received signals
The changes in docker@dcbf005 fixed the "cancellable context" detection, and made it so that Compose would conditionally set up signal handling when the context was already not cancellable/when the plugin was running through the CLI, as we'd introduced a mechanism into the CLI to signal plugins to exit through a socket instead of handling signals themselves. This had some (not noticed at the time) issues when running through the CLI as, due to sharing a process group id with the parent CLI process, when a user CTRL-Cs the CLI will notify the plugin via the socket but the plugin process itself will also be signalled if attached to the TTY. This impacted some Compose commands that don't set up signal handling - so not `compose up`, but other commands would immediately quit instead of getting some "graceful" cancelled output. We initially attempted to address this "double notification" issue in the CLI by executing plugins under a new pgid so that they wouldn't be signalled, but that posed an issue with Buildx reading from the TTY, (see: moby/moby#47073) so we reverted the process group id changes and ended at a temporary solution in docker/cli#4792 where the CLI will only notify plugins via the socket when they are not already going to be signalled (when attached to a TTY). Due to this, plugins should always set up some signal handling, which this commit implements. Signed-off-by: Laura Brehm <[email protected]>
1 parent f414bf7 commit 898e1b6

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

cmd/compose/compose.go

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

45-
"github.com/docker/cli/cli-plugins/plugin"
4645
"github.com/docker/compose/v2/cmd/formatter"
4746
"github.com/docker/compose/v2/pkg/api"
4847
"github.com/docker/compose/v2/pkg/compose"
@@ -74,20 +73,17 @@ type CobraCommand func(context.Context, *cobra.Command, []string) error
7473
// AdaptCmd adapt a CobraCommand func to cobra library
7574
func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error {
7675
return func(cmd *cobra.Command, args []string) error {
77-
ctx := cmd.Context()
78-
contextString := fmt.Sprintf("%s", ctx)
79-
if !strings.Contains(contextString, ".WithCancel") || plugin.RunningStandalone() { // need to handle cancel
80-
cancellableCtx, cancel := context.WithCancel(cmd.Context())
81-
ctx = cancellableCtx
82-
s := make(chan os.Signal, 1)
83-
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
84-
go func() {
85-
<-s
86-
cancel()
87-
signal.Stop(s)
88-
close(s)
89-
}()
90-
}
76+
ctx, cancel := context.WithCancel(cmd.Context())
77+
78+
s := make(chan os.Signal, 1)
79+
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
80+
go func() {
81+
<-s
82+
cancel()
83+
signal.Stop(s)
84+
close(s)
85+
}()
86+
9187
err := fn(ctx, cmd, args)
9288
var composeErr compose.Error
9389
if api.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) {

0 commit comments

Comments
 (0)