Skip to content

Commit 72c84cb

Browse files
committed
#fix #1595 / windows: kill() may not raise AccessDenied
1 parent 1f8d432 commit 72c84cb

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

HISTORY.rst

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ XXXX-XX-XX
77

88
**Bug fixes**
99

10+
- 1595_: [Windows] Process.kill() may not throw AccessDenied.
1011
- 1616_: use of Py_DECREF instead of Py_CLEAR will result in double free and
1112
segfault (CVE). (patch by Riccardo Schirone)
1213
- 1619_: [OpenBSD] compilation fails due to C syntax error. (patch by Nathan

psutil/_psutil_windows.c

+23-7
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ psutil_pids(PyObject *self, PyObject *args) {
243243
static PyObject *
244244
psutil_proc_kill(PyObject *self, PyObject *args) {
245245
HANDLE hProcess;
246-
DWORD err;
247246
long pid;
247+
DWORD exitCode;
248248

249249
if (! PyArg_ParseTuple(args, "l", &pid))
250250
return NULL;
@@ -265,19 +265,35 @@ psutil_proc_kill(PyObject *self, PyObject *args) {
265265
return NULL;
266266
}
267267

268-
// kill the process
269268
if (! TerminateProcess(hProcess, SIGTERM)) {
270-
err = GetLastError();
271-
// See: https://github.com/giampaolo/psutil/issues/1099
272-
if (err != ERROR_ACCESS_DENIED) {
273-
PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
269+
if (GetLastError() == ERROR_ACCESS_DENIED) {
270+
// ERROR_ACCESS_DENIED (winerror 5) may happen if the
271+
// process already died. See:
272+
// https://github.com/giampaolo/psutil/issues/1099
273+
// https://github.com/giampaolo/psutil/issues/1595
274+
if (GetExitCodeProcess(hProcess, &exitCode) == 0) {
275+
PyErr_SetFromOSErrnoWithSyscall("GetExitCodeProcess");
276+
goto error;
277+
}
278+
if (exitCode == STILL_ACTIVE) {
279+
PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
280+
goto error;
281+
}
274282
CloseHandle(hProcess);
275-
return NULL;
283+
Py_RETURN_NONE;
284+
}
285+
else {
286+
PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
287+
goto error;
276288
}
277289
}
278290

279291
CloseHandle(hProcess);
280292
Py_RETURN_NONE;
293+
294+
error:
295+
CloseHandle(hProcess);
296+
return NULL;
281297
}
282298

283299

scripts/internal/download_exes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
BASE_URL = 'https://ci.appveyor.com/api'
29-
PY_VERSIONS = ['2.7', '3.5', '3.6', '3.7']
29+
PY_VERSIONS = ['2.7', '3.5', '3.6', '3.7', '3.8']
3030
TIMEOUT = 30
3131
COLORS = True
3232

0 commit comments

Comments
 (0)