From 29fe26620ab2ee8fca74a5b5ee9a1c4db4bdd52e Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Wed, 5 Aug 2020 14:42:21 +0200 Subject: [PATCH] Stop forwarding signals to child process when the child terminates --- cli/mobycli/exec.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/cli/mobycli/exec.go b/cli/mobycli/exec.go index 14a5f8b99..c9cdfb154 100644 --- a/cli/mobycli/exec.go +++ b/cli/mobycli/exec.go @@ -62,18 +62,26 @@ func Exec() { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - s := make(chan os.Signal) - signal.Notify(s) // catch all signals + signals := make(chan os.Signal) + childExit := make(chan bool) + signal.Notify(signals) // catch all signals go func() { - for sig := range s { - err := cmd.Process.Signal(sig) - if err != nil { - fmt.Printf("WARNING could not forward signal %s to %s : %s\n", sig.String(), ComDockerCli, err.Error()) + for { + select { + case sig := <-signals: + err := cmd.Process.Signal(sig) + if err != nil { + fmt.Printf("WARNING could not forward signal %s to %s : %s\n", sig.String(), ComDockerCli, err.Error()) + } + case <-childExit: + return } } }() - if err := cmd.Run(); err != nil { + err := cmd.Run() + childExit <- true + if err != nil { if exiterr, ok := err.(*exec.ExitError); ok { os.Exit(exiterr.ExitCode()) }