diff --git a/go.mod b/go.mod index d89d03aea..4b70b5142 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68 github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d github.com/jesseduffield/gocui v0.3.1-0.20241223111608-9967d0e928a0 - github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 + github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5 github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 diff --git a/go.sum b/go.sum index 5041d10cc..69cf787f0 100644 --- a/go.sum +++ b/go.sum @@ -190,8 +190,8 @@ github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d h1:bO+Om github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d/go.mod h1:nGNEErzf+NRznT+N2SWqmHnDnF9aLgANB1CUNEan09o= github.com/jesseduffield/gocui v0.3.1-0.20241223111608-9967d0e928a0 h1:R29+E15wHqTDBfZxmzCLu0x34j5ljsXWT/DhR+2YiOU= github.com/jesseduffield/gocui v0.3.1-0.20241223111608-9967d0e928a0/go.mod h1:XtEbqCbn45keRXEu+OMZkjN5gw6AEob59afsgHjokZ8= -github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 h1:jmpr7KpX2+2GRiE91zTgfq49QvgiqB0nbmlwZ8UnOx0= -github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10/go.mod h1:aA97kHeNA+sj2Hbki0pvLslmE4CbDyhBeSSTUUnOuVo= +github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a h1:UDeJ3EBk04bXDLOPvuqM3on8HvyJfISw0+UMqW+0a4g= +github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a/go.mod h1:FSWDLKT0NQpntbDd1H3lbz51fhCVlMzy/J0S6nM727Q= github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5 h1:CDuQmfOjAtb1Gms6a1p5L2P8RhbLUq5t8aL7PiQd2uY= github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5/go.mod h1:qxN4mHOAyeIDLP7IK7defgPClM/z1Kze8VVQiaEjzsQ= github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e h1:uw/oo+kg7t/oeMs6sqlAwr85ND/9cpO3up3VxphxY0U= diff --git a/vendor/github.com/jesseduffield/kill/kill_windows.go b/vendor/github.com/jesseduffield/kill/kill_windows.go index 1ac08a125..97abfab78 100644 --- a/vendor/github.com/jesseduffield/kill/kill_windows.go +++ b/vendor/github.com/jesseduffield/kill/kill_windows.go @@ -3,12 +3,37 @@ package kill import ( + "golang.org/x/sys/windows" "os" "os/exec" "syscall" "unsafe" ) +const PROCESS_ALL_ACCESS = windows.STANDARD_RIGHTS_REQUIRED | windows.SYNCHRONIZE | 0xffff + +func GetWindowsHandle(pid int) (handle windows.Handle, err error) { + handle, err = windows.OpenProcess(PROCESS_ALL_ACCESS, false, uint32(pid)) + return +} + +func GetCreationTime(pid int) (time int64, err error) { + handle, err := GetWindowsHandle(pid) + if err != nil { + return + } + defer closeHandle(HANDLE(handle)) + + var u syscall.Rusage + err = syscall.GetProcessTimes(syscall.Handle(handle), &u.CreationTime, &u.ExitTime, &u.KernelTime, &u.UserTime) + if err != nil { + return + } + + time = u.CreationTime.Nanoseconds() + return +} + // Kill kills a process, along with any child processes it may have spawned. func Kill(cmd *exec.Cmd) error { if cmd.Process == nil { @@ -16,7 +41,12 @@ func Kill(cmd *exec.Cmd) error { return nil } - pids := Getppids(uint32(cmd.Process.Pid)) + ptime, err := GetCreationTime(cmd.Process.Pid) + if err != nil { + return err + } + + pids := Getppids(uint32(cmd.Process.Pid), ptime) for _, pid := range pids { pro, err := os.FindProcess(int(pid)) if err != nil { @@ -70,7 +100,7 @@ var ( procCloseHandle = modkernel32.NewProc("CloseHandle") ) -func Getppids(pid uint32) []uint32 { +func Getppids(pid uint32, ptime int64) []uint32 { infos, err := GetProcs() if err != nil { return []uint32{pid} @@ -83,7 +113,15 @@ func Getppids(pid uint32) []uint32 { for index < length { for _, info := range infos { if info.PPid == pids[index] { - pids = append(pids, info.Pid) + ctime, err := GetCreationTime(int(info.Pid)) + if err != nil { + continue + } + + if ctime >= ptime { + // Only appending if child is newer than parent, otherwise PPid was reused + pids = append(pids, info.Pid) + } } } index += 1 diff --git a/vendor/modules.txt b/vendor/modules.txt index 7d5bb2589..fb95e4356 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -175,7 +175,7 @@ github.com/jesseduffield/go-git/v5/utils/merkletrie/noder # github.com/jesseduffield/gocui v0.3.1-0.20241223111608-9967d0e928a0 ## explicit; go 1.12 github.com/jesseduffield/gocui -# github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 +# github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a ## explicit; go 1.18 github.com/jesseduffield/kill # github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5