Skip to content

Commit

Permalink
fix closing some custom commands on Windows (#3981)
Browse files Browse the repository at this point in the history
* Bugfix: Rewrote addProcessToGroup. Fixed possible invalid process handle by using windows.OpenProcess instead. Fixes issue: 3980

* run gofumpt

---------

Co-authored-by: aler9 <[email protected]>
  • Loading branch information
yelodevopsi and aler9 authored Jan 11, 2025
1 parent 8f04264 commit 8c1ed72
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions internal/externalcmd/cmd_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ func closeProcessGroup(h windows.Handle) error {
}

func addProcessToGroup(h windows.Handle, p *os.Process) error {
type process struct {
Pid int
Handle uintptr
// Combine the required access rights
access := uint32(windows.PROCESS_SET_QUOTA | windows.PROCESS_TERMINATE)

processHandle, err := windows.OpenProcess(access, false, uint32(p.Pid))
if err != nil {
return fmt.Errorf("failed to open process: %v", err)
}
defer windows.CloseHandle(processHandle)

err = windows.AssignProcessToJobObject(h, processHandle)
if err != nil {
return fmt.Errorf("failed to assign process to job object: %v", err)
}

return windows.AssignProcessToJobObject(h,
windows.Handle((*process)(unsafe.Pointer(p)).Handle))
return nil
}

func (e *Cmd) runOSSpecific(env []string) error {
Expand Down

0 comments on commit 8c1ed72

Please sign in to comment.