Skip to content

Commit

Permalink
[process][windows] Use WaitForSingleObject with a 0 delay in PidExist…
Browse files Browse the repository at this point in the history
…sWithContext

Reference https://stackoverflow.com/a/6493793

Fixes #1298
  • Loading branch information
Lomanic committed May 16, 2022
1 parent 19771aa commit 7501387
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions process/process_windows.go
Original file line number Diff line number Diff line change
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 7501387

Please sign in to comment.