From bdb7b93f25823c4a76d03592ab4c2baa7c30a1b9 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 18 Mar 2019 10:52:18 +0100 Subject: pkg/osutil: kill subprocesses more reliably In some cases we start scp, which starts ssh, then kill scp but the ssh subprocess is not killed. As the result cmd.Wait hangs waiting for EOF on the stdout/stderr, which are still kept alive by ssh subprocess. But ssh just hangs forever. Create a process group for each command and kill whole process group. Hopefully this will help. --- pkg/osutil/osutil.go | 2 ++ pkg/osutil/osutil_akaros.go | 3 +++ pkg/osutil/osutil_appengine.go | 3 +++ pkg/osutil/osutil_bsd.go | 3 +++ pkg/osutil/osutil_darwin.go | 3 +++ pkg/osutil/osutil_fuchsia.go | 3 +++ pkg/osutil/osutil_linux.go | 6 ++++++ pkg/osutil/osutil_windows.go | 3 +++ 8 files changed, 26 insertions(+) (limited to 'pkg/osutil') diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go index 7061b02d5..32a8054e7 100644 --- a/pkg/osutil/osutil.go +++ b/pkg/osutil/osutil.go @@ -36,6 +36,7 @@ func Run(timeout time.Duration, cmd *exec.Cmd) ([]byte, error) { if cmd.Stderr == nil { cmd.Stderr = output } + setPdeathsig(cmd) if err := cmd.Start(); err != nil { return nil, fmt.Errorf("failed to start %v %+v: %v", cmd.Path, cmd.Args, err) } @@ -46,6 +47,7 @@ func Run(timeout time.Duration, cmd *exec.Cmd) ([]byte, error) { select { case <-timer.C: timedout <- true + killPgroup(cmd) cmd.Process.Kill() case <-done: timedout <- false diff --git a/pkg/osutil/osutil_akaros.go b/pkg/osutil/osutil_akaros.go index 83a4bca47..bcc285748 100644 --- a/pkg/osutil/osutil_akaros.go +++ b/pkg/osutil/osutil_akaros.go @@ -30,3 +30,6 @@ func SandboxChown(file string) error { func setPdeathsig(cmd *exec.Cmd) { } + +func killPgroup(cmd *exec.Cmd) { +} diff --git a/pkg/osutil/osutil_appengine.go b/pkg/osutil/osutil_appengine.go index ce2938a24..6c3888dbf 100644 --- a/pkg/osutil/osutil_appengine.go +++ b/pkg/osutil/osutil_appengine.go @@ -19,3 +19,6 @@ func SandboxChown(file string) error { func setPdeathsig(cmd *exec.Cmd) { } + +func killPgroup(cmd *exec.Cmd) { +} diff --git a/pkg/osutil/osutil_bsd.go b/pkg/osutil/osutil_bsd.go index 976fd34fa..1f5cf6be8 100644 --- a/pkg/osutil/osutil_bsd.go +++ b/pkg/osutil/osutil_bsd.go @@ -27,3 +27,6 @@ func SandboxChown(file string) error { func setPdeathsig(cmd *exec.Cmd) { } + +func killPgroup(cmd *exec.Cmd) { +} diff --git a/pkg/osutil/osutil_darwin.go b/pkg/osutil/osutil_darwin.go index 5163a6133..bf6ba9810 100644 --- a/pkg/osutil/osutil_darwin.go +++ b/pkg/osutil/osutil_darwin.go @@ -27,3 +27,6 @@ func SandboxChown(file string) error { func setPdeathsig(cmd *exec.Cmd) { } + +func killPgroup(cmd *exec.Cmd) { +} diff --git a/pkg/osutil/osutil_fuchsia.go b/pkg/osutil/osutil_fuchsia.go index 0cd4c4a27..dc0ad5ddc 100644 --- a/pkg/osutil/osutil_fuchsia.go +++ b/pkg/osutil/osutil_fuchsia.go @@ -44,3 +44,6 @@ func SandboxChown(file string) error { func setPdeathsig(cmd *exec.Cmd) { } + +func killPgroup(cmd *exec.Cmd) { +} diff --git a/pkg/osutil/osutil_linux.go b/pkg/osutil/osutil_linux.go index 35e5646fd..732d8e4c2 100644 --- a/pkg/osutil/osutil_linux.go +++ b/pkg/osutil/osutil_linux.go @@ -139,6 +139,12 @@ func setPdeathsig(cmd *exec.Cmd) { cmd.SysProcAttr = new(syscall.SysProcAttr) } cmd.SysProcAttr.Pdeathsig = syscall.SIGKILL + // We will kill the whole process group. + cmd.SysProcAttr.Setpgid = true +} + +func killPgroup(cmd *exec.Cmd) { + syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) } func prolongPipe(r, w *os.File) { diff --git a/pkg/osutil/osutil_windows.go b/pkg/osutil/osutil_windows.go index 3305352b4..a7cdb1c4f 100644 --- a/pkg/osutil/osutil_windows.go +++ b/pkg/osutil/osutil_windows.go @@ -44,3 +44,6 @@ func SandboxChown(file string) error { func setPdeathsig(cmd *exec.Cmd) { } + +func killPgroup(cmd *exec.Cmd) { +} -- cgit mrf-deployment