Skip to content

Commit

Permalink
fix(windows): windows leaves persistent connection open to Ruby, forc…
Browse files Browse the repository at this point in the history
…ibly close 3s after interrupt #9
  • Loading branch information
mefellows committed Feb 20, 2017
1 parent 3616976 commit e97226d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions daemon/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package daemon

import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"time"
)

// ServiceManager is the default implementation of the Service interface.
Expand Down Expand Up @@ -59,6 +59,7 @@ func (s *ServiceManager) removeServiceMonitor() {
}
}

// Stop a Service and returns the exit status.
// Stop a Service and returns the exit status.
func (s *ServiceManager) Stop(pid int) (bool, error) {
log.Println("[DEBUG] stopping service with pid", pid)
Expand All @@ -68,11 +69,26 @@ func (s *ServiceManager) Stop(pid int) (bool, error) {
go func() {
s.commandCompleteChan <- cmd
}()
var err error
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()

err := cmd.Wait()
if err != nil {
fmt.Fprintln(os.Stderr, "Error waiting for Cmd", err)
return false, err
select {
case <-time.After(3 * time.Second):
if err = cmd.Process.Kill(); err != nil {
log.Println("failed to kill: ", err)
return false, err
}
log.Println("process killed as timeout reached")
case err = <-done:
if err != nil {
log.Printf("process done with error = %v", err)

} else {
log.Print("process done gracefully without error")
}
}

return cmd.ProcessState.Success(), nil
Expand Down

0 comments on commit e97226d

Please sign in to comment.