@@ -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
0 commit comments