Skip to content

Commit

Permalink
Terminate Windows processes with SIGTERM code rather than 0 (#1150)
Browse files Browse the repository at this point in the history
If the TerminateProcess WinAPI function is called with 0, then the
exit code of the terminated process (e.g., observed by its parent)
will be 0. However, this is usually associated with successful
execution. Any other exit code could be used to signal forced
termination, but perhaps the value of SIGTERM is the most
meaningful.
  • Loading branch information
akosthekiss authored and giampaolo committed Oct 20, 2017
1 parent 3e5e9b7 commit 548656a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <wtsapi32.h>
#include <Winsvc.h>
#include <PowrProf.h>
#include <signal.h>

// Link with Iphlpapi.lib
#pragma comment(lib, "IPHLPAPI.lib")
Expand Down Expand Up @@ -349,7 +350,7 @@ psutil_proc_kill(PyObject *self, PyObject *args) {
}

// kill the process
if (! TerminateProcess(hProcess, 0)) {
if (! TerminateProcess(hProcess, SIGTERM)) {
err = GetLastError();
// See: https://github.com/giampaolo/psutil/issues/1099
if (err != ERROR_ACCESS_DENIED) {
Expand Down
10 changes: 5 additions & 5 deletions psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def test_wait(self):
if POSIX:
self.assertEqual(code, -signal.SIGKILL)
else:
self.assertEqual(code, 0)
self.assertEqual(code, signal.SIGTERM)
self.assertFalse(p.is_running())

sproc = get_test_subprocess()
Expand All @@ -161,7 +161,7 @@ def test_wait(self):
if POSIX:
self.assertEqual(code, -signal.SIGTERM)
else:
self.assertEqual(code, 0)
self.assertEqual(code, signal.SIGTERM)
self.assertFalse(p.is_running())

# check sys.exit() code
Expand Down Expand Up @@ -207,8 +207,8 @@ def test_wait_non_children(self):
# to get None.
self.assertEqual(ret2, None)
else:
self.assertEqual(ret1, 0)
self.assertEqual(ret1, 0)
self.assertEqual(ret1, signal.SIGTERM)
self.assertEqual(ret1, signal.SIGTERM)

def test_wait_timeout_0(self):
sproc = get_test_subprocess()
Expand All @@ -227,7 +227,7 @@ def test_wait_timeout_0(self):
if POSIX:
self.assertEqual(code, -signal.SIGKILL)
else:
self.assertEqual(code, 0)
self.assertEqual(code, signal.SIGTERM)
self.assertFalse(p.is_running())

def test_cpu_percent(self):
Expand Down

0 comments on commit 548656a

Please sign in to comment.