Skip to content

Commit

Permalink
Merge pull request #1301 from Lomanic/issue1298
Browse files Browse the repository at this point in the history
[process][windows] Use WaitForSingleObject with a 0 delay in PidExistsWithContext
  • Loading branch information
shirou committed May 21, 2022
2 parents 09fa2a9 + 7501387 commit a753910
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions process/process_windows.go
Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down

0 comments on commit a753910

Please sign in to comment.