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 { 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) + } +}