shirou / gopsutil

psutil for golang
Other
10.66k stars 1.59k forks source link

Kill process on windows maybe incorrect #1749

Closed rohan-rayaraddi closed 3 days ago

rohan-rayaraddi commented 4 days ago

Describe the bug [A clear and concise description of what the bug is.]

func (p *Process) TerminateWithContext(ctx context.Context) error {
    proc, err := windows.OpenProcess(windows.PROCESS_TERMINATE, false, uint32(p.Pid))
    if err != nil {
        return err
    }
    err = windows.TerminateProcess(proc, 0)
    windows.CloseHandle(proc)
    return err
}

func (p *Process) KillWithContext(ctx context.Context) error {
    process, err := os.FindProcess(int(p.Pid))
    if err != nil {
        return err
    }
    return process.Kill()
}

Kill method on windows calls the parent kill method. Whereas if you see an equivalent Terminate call, it calls terminate on a windows process handle.

To Reproduce

// paste example code reproducing the bug you are reporting

I was doing a comparison with python and I found that kill in python works only on UNIX systems. Hence, I believe the same issue may be applicable here as well.

Expected behavior [A clear and concise description of what you expected to happen.]

Either inform users that kill is not supported on windows or perform a sigterm if that works.

Environment (please complete the following information):

Additional context [Cross-compiling? Paste the command you are using to cross-compile and the result of the corresponding go env]