From e97226d44327d55d827f3585b59f6bc9f0ce3b0c Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Tue, 21 Feb 2017 00:17:46 +1100 Subject: [PATCH] fix(windows): windows leaves persistent connection open to Ruby, forcibly close 3s after interrupt #9 --- daemon/service_manager.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/daemon/service_manager.go b/daemon/service_manager.go index 6fb259e96..f778d5cef 100644 --- a/daemon/service_manager.go +++ b/daemon/service_manager.go @@ -2,11 +2,11 @@ package daemon import ( "bufio" - "fmt" "io" "log" "os" "os/exec" + "time" ) // ServiceManager is the default implementation of the Service interface. @@ -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) @@ -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