Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 8f27de6

Browse files
author
Ulysses Souza
authored
Merge pull request #2098 from docker/revert-1879-signals
Revert "signals"
2 parents 461f022 + 48ad148 commit 8f27de6

File tree

12 files changed

+97
-19
lines changed

12 files changed

+97
-19
lines changed

Diff for: cli/cmd/context/context.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func exportCommand() *cobra.Command {
4848
Use: "export",
4949
Short: "Export a context to a tar or kubeconfig file",
5050
Run: func(cmd *cobra.Command, args []string) {
51-
mobycli.Exec()
51+
mobycli.Exec(cmd.Root())
5252
},
5353
}
5454
cmd.Flags().Bool("kubeconfig", false, "Export as a kubeconfig file")
@@ -60,7 +60,7 @@ func importCommand() *cobra.Command {
6060
Use: "import",
6161
Short: "Import a context from a tar or zip file",
6262
Run: func(cmd *cobra.Command, args []string) {
63-
mobycli.Exec()
63+
mobycli.Exec(cmd.Root())
6464
},
6565
}
6666
return cmd

Diff for: cli/cmd/context/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ $ docker context create my-context --description "some description" --docker "ho
6969
Use: "create CONTEXT",
7070
Short: "Create new context",
7171
RunE: func(cmd *cobra.Command, args []string) error {
72-
mobycli.Exec()
72+
mobycli.Exec(cmd.Root())
7373
return nil
7474
},
7575
Long: longHelp,

Diff for: cli/cmd/context/inspect.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func inspectCommand() *cobra.Command {
2727
Use: "inspect",
2828
Short: "Display detailed information on one or more contexts",
2929
RunE: func(cmd *cobra.Command, args []string) error {
30-
mobycli.Exec()
30+
mobycli.Exec(cmd.Root())
3131
return nil
3232
},
3333
}

Diff for: cli/cmd/context/ls.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func listCommand() *cobra.Command {
5353
Aliases: []string{"ls"},
5454
Args: cobra.NoArgs,
5555
RunE: func(cmd *cobra.Command, args []string) error {
56-
return runList(opts)
56+
return runList(cmd, opts)
5757
},
5858
}
5959
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names")
@@ -62,14 +62,14 @@ func listCommand() *cobra.Command {
6262
return cmd
6363
}
6464

65-
func runList(opts lsOpts) error {
65+
func runList(cmd *cobra.Command, opts lsOpts) error {
6666
err := opts.validate()
6767
if err != nil {
6868
return err
6969
}
7070
format := strings.ToLower(strings.ReplaceAll(opts.format, " ", ""))
7171
if format != "" && format != formatter.JSON && format != formatter.PRETTY && format != formatter.TemplateLegacyJSON {
72-
mobycli.Exec()
72+
mobycli.Exec(cmd.Root())
7373
return nil
7474
}
7575

Diff for: cli/cmd/context/update.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ $ docker context update my-context --description "some description" --docker "ho
5656
Short: "Update a context",
5757
Args: cobra.ExactArgs(1),
5858
RunE: func(cmd *cobra.Command, args []string) error {
59-
return runUpdate(args[0])
59+
return runUpdate(cmd, args[0])
6060
},
6161
Long: longHelp,
6262
}
@@ -71,7 +71,7 @@ $ docker context update my-context --description "some description" --docker "ho
7171
return cmd
7272
}
7373

74-
func runUpdate(name string) error {
74+
func runUpdate(cmd *cobra.Command, name string) error {
7575
s := store.Instance()
7676
dockerContext, err := s.Get(name)
7777
if err == nil && dockerContext != nil {
@@ -80,6 +80,6 @@ func runUpdate(name string) error {
8080
}
8181
}
8282

83-
mobycli.Exec()
83+
mobycli.Exec(cmd.Root())
8484
return nil
8585
}

Diff for: cli/cmd/login/login.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func Command() *cobra.Command {
4949
}
5050

5151
func runLogin(cmd *cobra.Command, args []string) error {
52-
mobycli.Exec()
52+
mobycli.Exec(cmd.Root())
5353
return nil
5454
}
5555

Diff for: cli/cmd/logout/logout.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ func Command() *cobra.Command {
3737
}
3838

3939
func runLogout(cmd *cobra.Command, args []string) error {
40-
mobycli.Exec()
40+
mobycli.Exec(cmd.Root())
4141
return nil
4242
}

Diff for: cli/cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func getOutFromMoby(cmd *cobra.Command, args ...string) (string, error) {
8080
// we don't want to fail on error, there is an error if the engine is not available but it displays client version info
8181
// Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
8282
if versionResult == nil {
83-
mobycli.Exec()
83+
mobycli.Exec(cmd.Root())
8484
return "", nil
8585
}
8686
return string(versionResult), err

Diff for: cli/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func main() {
195195

196196
// --version should immediately be forwarded to the original cli
197197
if opts.Version {
198-
mobycli.Exec()
198+
mobycli.Exec(root)
199199
}
200200

201201
if opts.Config == "" {
@@ -209,7 +209,7 @@ func main() {
209209

210210
s, err := store.New(configDir)
211211
if err != nil {
212-
mobycli.Exec()
212+
mobycli.Exec(root)
213213
}
214214
store.WithContextStore(s)
215215

Diff for: cli/mobycli/exec.go

+30-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323
"os/exec"
24+
"os/signal"
2425
"path/filepath"
2526
"regexp"
2627

@@ -48,7 +49,7 @@ func ExecIfDefaultCtxType(ctx context.Context, root *cobra.Command) {
4849
currentCtx, err := s.Get(currentContext)
4950
// Only run original docker command if the current context is not ours.
5051
if err != nil || mustDelegateToMoby(currentCtx.Type()) {
51-
Exec()
52+
Exec(root)
5253
}
5354
}
5455

@@ -62,8 +63,10 @@ func mustDelegateToMoby(ctxType string) bool {
6263
}
6364

6465
// Exec delegates to com.docker.cli if on moby context
65-
func Exec() {
66-
err := RunDocker(os.Args[1:]...)
66+
func Exec(root *cobra.Command) {
67+
childExit := make(chan bool)
68+
err := RunDocker(childExit, os.Args[1:]...)
69+
childExit <- true
6770
if err != nil {
6871
if exiterr, ok := err.(*exec.ExitError); ok {
6972
exitCode := exiterr.ExitCode()
@@ -88,7 +91,7 @@ func Exec() {
8891
}
8992

9093
// RunDocker runs a docker command, and forward signals to the shellout command (stops listening to signals when an event is sent to childExit)
91-
func RunDocker(args ...string) error {
94+
func RunDocker(childExit chan bool, args ...string) error {
9295
execBinary, err := resolvepath.LookPath(ComDockerCli)
9396
if err != nil {
9497
execBinary = findBinary(ComDockerCli)
@@ -102,6 +105,29 @@ func RunDocker(args ...string) error {
102105
cmd.Stdin = os.Stdin
103106
cmd.Stdout = os.Stdout
104107
cmd.Stderr = os.Stderr
108+
109+
signals := make(chan os.Signal, 1)
110+
signal.Notify(signals) // catch all signals
111+
go func() {
112+
for {
113+
select {
114+
case sig := <-signals:
115+
if cmd.Process == nil {
116+
continue // can happen if receiving signal before the process is actually started
117+
}
118+
// In go1.14+, the go runtime issues SIGURG as an interrupt to
119+
// support preemptable system calls on Linux. Since we can't
120+
// forward that along we'll check that here.
121+
if isRuntimeSig(sig) {
122+
continue
123+
}
124+
_ = cmd.Process.Signal(sig)
125+
case <-childExit:
126+
return
127+
}
128+
}
129+
}()
130+
105131
return cmd.Run()
106132
}
107133

Diff for: cli/mobycli/exec_unix.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// +build !windows
2+
3+
/*
4+
Copyright 2020 Docker Compose CLI authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package mobycli
20+
21+
import (
22+
"os"
23+
24+
"golang.org/x/sys/unix"
25+
)
26+
27+
func isRuntimeSig(s os.Signal) bool {
28+
return s == unix.SIGURG
29+
}

Diff for: cli/mobycli/exec_windows.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mobycli
18+
19+
import "os"
20+
21+
func isRuntimeSig(s os.Signal) bool {
22+
return false
23+
}

0 commit comments

Comments
 (0)