From 722c3e8e93a1bb9e07177cce69c6d22d96fb3eac Mon Sep 17 00:00:00 2001 From: Songmu Date: Sun, 7 Jan 2018 19:16:22 +0900 Subject: [PATCH 1/2] send SIGCONT after sending termination signal just to make sure --- timeout_test.go | 22 ++++++++++++++++++++++ timeout_unix.go | 14 +++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/timeout_test.go b/timeout_test.go index 429328f..f612c95 100644 --- a/timeout_test.go +++ b/timeout_test.go @@ -127,3 +127,25 @@ func TestRunSimple(t *testing.T) { }) } } + +func TestRunSimple_withStop(t *testing.T) { + if isWin { + t.Skipf("skip on windows") + } + tio := &Timeout{ + Duration: 2 * time.Second, + KillAfter: 1 * time.Second, + Cmd: exec.Command(shellcmd, shellflag, "sleep 10"), + } + ch, err := tio.RunCommand() + if err != nil { + t.Errorf("err should be nil but: %s", err) + } + tio.Cmd.Process.Signal(syscall.SIGSTOP) + st := <-ch + + expect := 128 + 15 + if st.Code != expect { + t.Errorf("exit code invalid. out: %d, expect: %d", st.Code, expect) + } +} diff --git a/timeout_unix.go b/timeout_unix.go index 5844008..b07c51d 100644 --- a/timeout_unix.go +++ b/timeout_unix.go @@ -15,11 +15,19 @@ func (tio *Timeout) getCmd() *exec.Cmd { } func (tio *Timeout) terminate() error { - syssig, ok := tio.signal().(syscall.Signal) + sig := tio.signal() + syssig, ok := sig.(syscall.Signal) if !ok || tio.Foreground { - return tio.Cmd.Process.Signal(tio.signal()) + return tio.Cmd.Process.Signal(sig) } - return syscall.Kill(-tio.Cmd.Process.Pid, syssig) + err := syscall.Kill(-tio.Cmd.Process.Pid, syssig) + if err != nil { + return err + } + if syssig != syscall.SIGKILL && syssig != syscall.SIGCONT { + return syscall.Kill(-tio.Cmd.Process.Pid, syscall.SIGCONT) + } + return nil } func (tio *Timeout) killall() error { From 9b7bd2333c4c09f11b329680d5a985d97cca4803 Mon Sep 17 00:00:00 2001 From: Songmu Date: Sun, 7 Jan 2018 20:39:59 +0900 Subject: [PATCH 2/2] separate timeout_unix_test.go --- timeout_test.go | 22 ---------------------- timeout_unix_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 timeout_unix_test.go diff --git a/timeout_test.go b/timeout_test.go index f612c95..429328f 100644 --- a/timeout_test.go +++ b/timeout_test.go @@ -127,25 +127,3 @@ func TestRunSimple(t *testing.T) { }) } } - -func TestRunSimple_withStop(t *testing.T) { - if isWin { - t.Skipf("skip on windows") - } - tio := &Timeout{ - Duration: 2 * time.Second, - KillAfter: 1 * time.Second, - Cmd: exec.Command(shellcmd, shellflag, "sleep 10"), - } - ch, err := tio.RunCommand() - if err != nil { - t.Errorf("err should be nil but: %s", err) - } - tio.Cmd.Process.Signal(syscall.SIGSTOP) - st := <-ch - - expect := 128 + 15 - if st.Code != expect { - t.Errorf("exit code invalid. out: %d, expect: %d", st.Code, expect) - } -} diff --git a/timeout_unix_test.go b/timeout_unix_test.go new file mode 100644 index 0000000..b6470e2 --- /dev/null +++ b/timeout_unix_test.go @@ -0,0 +1,29 @@ +// +build !windows + +package timeout + +import ( + "os/exec" + "syscall" + "testing" + "time" +) + +func TestRunSimple_withStop(t *testing.T) { + tio := &Timeout{ + Duration: 2 * time.Second, + KillAfter: 1 * time.Second, + Cmd: exec.Command(shellcmd, shellflag, "sleep 10"), + } + ch, err := tio.RunCommand() + if err != nil { + t.Errorf("err should be nil but: %s", err) + } + tio.Cmd.Process.Signal(syscall.SIGSTOP) + st := <-ch + + expect := 128 + 15 + if st.Code != expect { + t.Errorf("exit code invalid. out: %d, expect: %d", st.Code, expect) + } +}