Skip to content

Commit

Permalink
fix #1099 / windows: Process.terminate() may raise AccessDenied even …
Browse files Browse the repository at this point in the history
…if the process already dided
  • Loading branch information
giampaolo committed May 30, 2017
1 parent 42bebc2 commit 1e49e9e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
on OpenProcess Windows API now check whether the PID is actually running.
- 1098_: [Windows] Process.wait() may erroneously return sooner, when the PID
is still alive.
- 1099_: [Windows] Process.terminate() may raise AccessDenied even if the
process already died.

**Porting notes**

Expand Down
12 changes: 10 additions & 2 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ psutil_pids(PyObject *self, PyObject *args) {
static PyObject *
psutil_proc_kill(PyObject *self, PyObject *args) {
HANDLE hProcess;
DWORD err;
long pid;

if (! PyArg_ParseTuple(args, "l", &pid))
Expand All @@ -342,9 +343,16 @@ psutil_proc_kill(PyObject *self, PyObject *args) {

// kill the process
if (! TerminateProcess(hProcess, 0)) {
PyErr_SetFromWindowsErr(0);
err = GetLastError();
CloseHandle(hProcess);
return NULL;
// See: https://github.com/giampaolo/psutil/issues/1099
if (psutil_pid_is_running(pid) == 0) {
Py_RETURN_NONE;
}
else {
PyErr_SetFromWindowsErr(err);
return NULL;
}
}

CloseHandle(hProcess);
Expand Down

0 comments on commit 1e49e9e

Please sign in to comment.