Skip to content

Commit 8c1ed72

Browse files
yelodevopsialer9
andauthored
fix closing some custom commands on Windows (#3981)
* Bugfix: Rewrote addProcessToGroup. Fixed possible invalid process handle by using windows.OpenProcess instead. Fixes issue: 3980 * run gofumpt --------- Co-authored-by: aler9 <[email protected]>
1 parent 8f04264 commit 8c1ed72

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

internal/externalcmd/cmd_win.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ func closeProcessGroup(h windows.Handle) error {
4444
}
4545

4646
func addProcessToGroup(h windows.Handle, p *os.Process) error {
47-
type process struct {
48-
Pid int
49-
Handle uintptr
47+
// Combine the required access rights
48+
access := uint32(windows.PROCESS_SET_QUOTA | windows.PROCESS_TERMINATE)
49+
50+
processHandle, err := windows.OpenProcess(access, false, uint32(p.Pid))
51+
if err != nil {
52+
return fmt.Errorf("failed to open process: %v", err)
53+
}
54+
defer windows.CloseHandle(processHandle)
55+
56+
err = windows.AssignProcessToJobObject(h, processHandle)
57+
if err != nil {
58+
return fmt.Errorf("failed to assign process to job object: %v", err)
5059
}
5160

52-
return windows.AssignProcessToJobObject(h,
53-
windows.Handle((*process)(unsafe.Pointer(p)).Handle))
61+
return nil
5462
}
5563

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

0 commit comments

Comments
 (0)