From f7e898b0987f97352c7551bdd9b29b594e1236f6 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Nov 2019 02:09:02 -0800 Subject: [PATCH] #1595: use psutil_pid_is_running() instead of GetExitCodeProcess --- psutil/_psutil_windows.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index e01fa50d2..be0b2337f 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -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; @@ -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; }