diff --git a/HISTORY.rst b/HISTORY.rst index 5aaf9da12..045bb5d5f 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -7,6 +7,7 @@ XXXX-XX-XX **Bug fixes** +- 1595_: [Windows] Process.kill() may not throw AccessDenied. - 1616_: use of Py_DECREF instead of Py_CLEAR will result in double free and segfault (CVE). (patch by Riccardo Schirone) - 1619_: [OpenBSD] compilation fails due to C syntax error. (patch by Nathan diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 08b208dc0..e01fa50d2 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -243,8 +243,8 @@ psutil_pids(PyObject *self, PyObject *args) { static PyObject * psutil_proc_kill(PyObject *self, PyObject *args) { HANDLE hProcess; - DWORD err; long pid; + DWORD exitCode; if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; @@ -265,19 +265,35 @@ psutil_proc_kill(PyObject *self, PyObject *args) { return NULL; } - // kill the process if (! TerminateProcess(hProcess, SIGTERM)) { - err = GetLastError(); - // See: https://github.com/giampaolo/psutil/issues/1099 - if (err != ERROR_ACCESS_DENIED) { - PyErr_SetFromOSErrnoWithSyscall("TerminateProcess"); + 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; + } CloseHandle(hProcess); - return NULL; + Py_RETURN_NONE; + } + else { + PyErr_SetFromOSErrnoWithSyscall("TerminateProcess"); + goto error; } } CloseHandle(hProcess); Py_RETURN_NONE; + +error: + CloseHandle(hProcess); + return NULL; } diff --git a/scripts/internal/download_exes.py b/scripts/internal/download_exes.py index 1b72a1777..4a559bb0b 100755 --- a/scripts/internal/download_exes.py +++ b/scripts/internal/download_exes.py @@ -26,7 +26,7 @@ BASE_URL = 'https://ci.appveyor.com/api' -PY_VERSIONS = ['2.7', '3.5', '3.6', '3.7'] +PY_VERSIONS = ['2.7', '3.5', '3.6', '3.7', '3.8'] TIMEOUT = 30 COLORS = True