Skip to content

Commit

Permalink
#1595: use psutil_pid_is_running() instead of GetExitCodeProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Nov 20, 2019
1 parent 72c84cb commit f7e898b
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ static PyObject *
psutil_proc_kill(PyObject *self, PyObject *args) {
HANDLE hProcess;
long pid;
DWORD exitCode;

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
Expand All @@ -266,19 +265,12 @@ psutil_proc_kill(PyObject *self, PyObject *args) {
}

if (! TerminateProcess(hProcess, SIGTERM)) {
if (GetLastError() == ERROR_ACCESS_DENIED) {
// ERROR_ACCESS_DENIED (winerror 5) may happen if the
// process already died. See:
// https://github.com/giampaolo/psutil/issues/1099
// https://github.com/giampaolo/psutil/issues/1595
if (GetExitCodeProcess(hProcess, &exitCode) == 0) {
PyErr_SetFromOSErrnoWithSyscall("GetExitCodeProcess");
goto error;
}
if (exitCode == STILL_ACTIVE) {
PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
goto error;
}
// ERROR_ACCESS_DENIED may happen if the process already died. See:
// https://github.com/giampaolo/psutil/issues/1099
// https://github.com/giampaolo/psutil/issues/1595
if ((GetLastError() == ERROR_ACCESS_DENIED) && \
(psutil_pid_is_running(pid) == 0))
{
CloseHandle(hProcess);
Py_RETURN_NONE;
}
Expand Down

0 comments on commit f7e898b

Please sign in to comment.