Skip to content

Commit f7e898b

Browse files
committed
#1595: use psutil_pid_is_running() instead of GetExitCodeProcess
1 parent 72c84cb commit f7e898b

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

Diff for: psutil/_psutil_windows.c

+6-14
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ static PyObject *
244244
psutil_proc_kill(PyObject *self, PyObject *args) {
245245
HANDLE hProcess;
246246
long pid;
247-
DWORD exitCode;
248247

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

268267
if (! TerminateProcess(hProcess, SIGTERM)) {
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-
}
268+
// ERROR_ACCESS_DENIED may happen if the process already died. See:
269+
// https://github.com/giampaolo/psutil/issues/1099
270+
// https://github.com/giampaolo/psutil/issues/1595
271+
if ((GetLastError() == ERROR_ACCESS_DENIED) && \
272+
(psutil_pid_is_running(pid) == 0))
273+
{
282274
CloseHandle(hProcess);
283275
Py_RETURN_NONE;
284276
}

0 commit comments

Comments
 (0)