Skip to content

Commit a753910

Browse files
authored
Merge pull request #1301 from Lomanic/issue1298
[process][windows] Use WaitForSingleObject with a 0 delay in PidExistsWithContext
2 parents 09fa2a9 + 7501387 commit a753910

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

process/process_windows.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
285285
}
286286
return false, err
287287
}
288-
const STILL_ACTIVE = 259 // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess
289-
h, err := windows.OpenProcess(processQueryInformation, false, uint32(pid))
288+
h, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
290289
if err == windows.ERROR_ACCESS_DENIED {
291290
return true, nil
292291
}
@@ -296,10 +295,9 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
296295
if err != nil {
297296
return false, err
298297
}
299-
defer syscall.CloseHandle(syscall.Handle(h))
300-
var exitCode uint32
301-
err = windows.GetExitCodeProcess(h, &exitCode)
302-
return exitCode == STILL_ACTIVE, err
298+
defer windows.CloseHandle(h)
299+
event, err := windows.WaitForSingleObject(h, 0)
300+
return event == uint32(windows.WAIT_TIMEOUT), err
303301
}
304302

305303
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {

0 commit comments

Comments
 (0)