Skip to content

Commit

Permalink
Merge pull request #259 from onsi/issue-258
Browse files Browse the repository at this point in the history
Avoid sending a signal or killing the session if the process is not alive
  • Loading branch information
William Martin committed Jan 30, 2018
2 parents 003f63b + b8043e5 commit 4fc1762
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
15 changes: 7 additions & 8 deletions gexec/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,7 @@ If the command has already exited, Kill returns silently.
The session is returned to enable chaining.
*/
func (s *Session) Kill() *Session {
if s.ExitCode() != -1 {
return s
}
s.Command.Process.Kill()
return s
return s.Signal(syscall.SIGKILL)
}

/*
Expand Down Expand Up @@ -188,10 +184,9 @@ If the command has already exited, Signal returns silently.
The session is returned to enable chaining.
*/
func (s *Session) Signal(signal os.Signal) *Session {
if s.ExitCode() != -1 {
return s
if s.processIsAlive() {
s.Command.Process.Signal(signal)
}
s.Command.Process.Signal(signal)
return s
}

Expand All @@ -215,6 +210,10 @@ func (s *Session) monitorForExit(exited chan<- struct{}) {
close(exited)
}

func (s *Session) processIsAlive() bool {
return s.ExitCode() == -1 && s.Command.Process != nil
}

var trackedSessions = []*Session{}
var trackedSessionsMutex = &sync.Mutex{}

Expand Down
7 changes: 7 additions & 0 deletions gexec/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ var _ = Describe("Session", func() {
Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
Eventually(session).Should(Exit(128 + 6))
})

It("should ignore sending a signal if the command did not start", func() {
session, err := Start(exec.Command("notexisting"), GinkgoWriter, GinkgoWriter)
Expect(err).To(HaveOccurred())

Expect(func() { session.Signal(syscall.SIGUSR1) }).NotTo(Panic())
})
})

Context("tracking sessions", func() {
Expand Down

0 comments on commit 4fc1762

Please sign in to comment.