Skip to content

Commit

Permalink
Move Kill and PrepareForChildren to platform specific implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
reicheltp committed Nov 11, 2019
1 parent b8804d9 commit bfd3df5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
22 changes: 0 additions & 22 deletions pkg/commands/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"strings"
"sync"
"syscall"
"time"

"github.com/go-errors/errors"
Expand Down Expand Up @@ -330,24 +329,3 @@ func (c *OSCommand) PipeCommands(commandStrings ...string) error {
}
return nil
}

// Kill kills a process. If the process has Setpgid == true, then we have anticipated that it might spawn its own child processes, so we've given it a process group ID (PGID) equal to its process id (PID) and given its child processes will inherit the PGID, we can kill that group, rather than killing the process itself.
func (c *OSCommand) Kill(cmd *exec.Cmd) error {
if cmd.Process == nil {
// somebody got to it before we were able to, poor bastard
return nil
}
if cmd.SysProcAttr != nil && cmd.SysProcAttr.Setpgid {
// minus sign means we're talking about a PGID as opposed to a PID
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}

return cmd.Process.Kill()
}

// PrepareForChildren sets Setpgid to true on the cmd, so that when we run it as a sideproject, we can kill its group rather than the process itself. This is because some commands, like `docker-compose logs` spawn multiple children processes, and killing the parent process isn't sufficient for killing those child processes. We set the group id here, and then in subprocess.go we check if the group id is set and if so, we kill the whole group rather than just the one process.
func (c *OSCommand) PrepareForChildren(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
}
21 changes: 21 additions & 0 deletions pkg/commands/os_default_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@ func getPlatform() *Platform {
fallbackEscapedQuote: "\"",
}
}

// Kill kills a process. If the process has Setpgid == true, then we have anticipated that it might spawn its own child processes, so we've given it a process group ID (PGID) equal to its process id (PID) and given its child processes will inherit the PGID, we can kill that group, rather than killing the process itself.
func (c *OSCommand) Kill(cmd *exec.Cmd) error {
if cmd.Process == nil {
// somebody got to it before we were able to, poor bastard
return nil
}
if cmd.SysProcAttr != nil && cmd.SysProcAttr.Setpgid {
// minus sign means we're talking about a PGID as opposed to a PID
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}

return cmd.Process.Kill()
}

// PrepareForChildren sets Setpgid to true on the cmd, so that when we run it as a sideproject, we can kill its group rather than the process itself. This is because some commands, like `docker-compose logs` spawn multiple children processes, and killing the parent process isn't sufficient for killing those child processes. We set the group id here, and then in subprocess.go we check if the group id is set and if so, we kill the whole group rather than just the one process.
func (c *OSCommand) PrepareForChildren(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
}
17 changes: 17 additions & 0 deletions pkg/commands/os_windows.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package commands

import (
"os/exec"
)

func getPlatform() *Platform {
return &Platform{
os: "windows",
Expand All @@ -9,3 +13,16 @@ func getPlatform() *Platform {
fallbackEscapedQuote: "\\'",
}
}

// Kill kills a process. If the process has Setpgid == true, then we have anticipated that it might spawn its own child processes, so we've given it a process group ID (PGID) equal to its process id (PID) and given its child processes will inherit the PGID, we can kill that group, rather than killing the process itself.
func (c *OSCommand) Kill(cmd *exec.Cmd) error {
if cmd.Process == nil {
// somebody got to it before we were able to, poor bastard
return nil
}

return cmd.Process.Kill()
}

// PrepareForChildren sets Setpgid to true on the cmd, so that when we run it as a sideproject, we can kill its group rather than the process itself. This is because some commands, like `docker-compose logs` spawn multiple children processes, and killing the parent process isn't sufficient for killing those child processes. We set the group id here, and then in subprocess.go we check if the group id is set and if so, we kill the whole group rather than just the one process.
func (c *OSCommand) PrepareForChildren(cmd *exec.Cmd) {}

0 comments on commit bfd3df5

Please sign in to comment.