Merge pull request #1301 from Lomanic/issue1298

[process][windows] Use WaitForSingleObject with a 0 delay in PidExistsWithContext
pull/1305/head
shirou 3 years ago committed by GitHub
commit a7539106da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -285,8 +285,7 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
}
return false, err
}
const STILL_ACTIVE = 259 // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess
h, err := windows.OpenProcess(processQueryInformation, false, uint32(pid))
h, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
if err == windows.ERROR_ACCESS_DENIED {
return true, nil
}
@ -296,10 +295,9 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
if err != nil {
return false, err
}
defer syscall.CloseHandle(syscall.Handle(h))
var exitCode uint32
err = windows.GetExitCodeProcess(h, &exitCode)
return exitCode == STILL_ACTIVE, err
defer windows.CloseHandle(h)
event, err := windows.WaitForSingleObject(h, 0)
return event == uint32(windows.WAIT_TIMEOUT), err
}
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {

Loading…
Cancel
Save