Skip to content

Commit

Permalink
Work around bug in PythonPsUtil so we get negative except codes when
Browse files Browse the repository at this point in the history
the program being executed is killed by a signal.

See giampaolo/psutil#960
  • Loading branch information
Dan Liew committed Jan 31, 2017
1 parent 0621eaf commit 8885ca4
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions KleeRunner/Backends/PythonPsUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
import os
import pprint
import psutil
import subprocess
import threading
import time
import sys

_logger = logging.getLogger(__name__)

if sys.version_info < (3, 3):
# HACK: We need newer python so subprocess.Popen.wait()
# supports a timeout.
raise Exception('Need Python >= 3.3')


class PythonPsUtilBackendException(BackendException):
pass
Expand Down Expand Up @@ -79,20 +86,28 @@ def run(self, cmdLine, logFilePath, envVars):
preExecFn = self._setStacksize
_logger.info('Using stacksize limit: {} KiB'.format(
'unlimited' if self.stackLimit == 0 else self.stackLimit))
self._process = psutil.Popen(cmdLine,
# HACK: Use subprocess.Popen and then create the psutil wrapper
# around it because it returns the wrong exit code.
# This is a workaround for https://github.com/giampaolo/psutil/issues/960
self._subprocess_process = subprocess.Popen(cmdLine,
cwd=self.workingDirectory,
stdout=f,
stderr=f,
env=envVars,
preexec_fn=preExecFn)
try:
self._process = psutil.Process(pid=self._subprocess_process.pid)
except psutil.NoSuchProcess as e:
# HACK: Catch case where process has already died
pass

if self.memoryLimit > 0:
pollThread = self._memoryLimitPolling(self._process)

_logger.info(
'Running with timeout of {} seconds'.format(self.timeLimit))
exitCode = self._process.wait(timeout=self.timeLimit)
except (psutil.TimeoutExpired) as e:
exitCode = self._subprocess_process.wait(timeout=self.timeLimit)
except subprocess.TimeoutExpired as e:
outOfTime = True
# Note the code in the finally block will sort out clean up
finally:
Expand Down

0 comments on commit 8885ca4

Please sign in to comment.