From 85537511dd188f54331abfaeeef7d7b950bf33bd Mon Sep 17 00:00:00 2001 From: Nick Phillips Date: Thu, 26 May 2016 17:25:54 +1200 Subject: [PATCH] Add debugging, stop hang in ProcessInThread. --- mdk/commands/behat.py | 4 ++++ mdk/tools.py | 24 +++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/mdk/commands/behat.py b/mdk/commands/behat.py index f8a87a83..65258623 100644 --- a/mdk/commands/behat.py +++ b/mdk/commands/behat.py @@ -322,9 +322,13 @@ def run(self, args): # Kill the remaining processes if phpServer and phpServer.is_alive(): + logging.debug('Killing phpServer...') phpServer.kill() + logging.debug('phpServer killed.') if seleniumServer and seleniumServer.is_alive(): + logging.debug('Killing seleniumServer...') seleniumServer.kill() + logging.debug('seleniumServer killed.') # Disable Behat if args.disable: diff --git a/mdk/tools.py b/mdk/tools.py index 6e75ce55..dad3e1bd 100644 --- a/mdk/tools.py +++ b/mdk/tools.py @@ -33,6 +33,7 @@ import logging import hashlib import tempfile +import errno from .config import Conf C = Conf() @@ -177,11 +178,14 @@ def process(cmd, cwd=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE): logging.debug(' '.join(cmd)) try: proc = subprocess.Popen(cmd, cwd=cwd, stdout=stdout, stderr=stderr, encoding='utf-8') + logging.debug("subprocess started") (out, err) = proc.communicate() + logging.debug("communicate done") except KeyboardInterrupt as e: proc.kill() raise e - return (proc.returncode, out.decode('utf-8') if type(out) == bytes else out, err) + logging.debug("process returning") + return (proc.returncode, out.decode('utf-8') if type(out) -- bytes else out, err) def resolveEditor(): @@ -244,16 +248,18 @@ def __init__(self, cmd, cwd=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE self.stderr = stderr def kill(self): - os.kill(self._pid, signal.SIGKILL) + try: + os.kill(self._pid, signal.SIGKILL) + except OSError as e: + if e.errno == errno.ESRCH: + logging.debug("ProcessInThread subprocess died before we could kill it") def run(self): logging.debug(' '.join(self.cmd)) proc = subprocess.Popen(self.cmd, cwd=self.cwd, stdout=self.stdout, stderr=self.stderr) self._pid = proc.pid - while True: - if proc.poll(): - break - - # Reading the output seems to prevent the process to hang. - if self.stdout == subprocess.PIPE: - proc.stdout.read(1) + logging.debug("ProcessInThread subprocess started, pid %s", self._pid) + (out, err) = proc.communicate() + if err: + logging.debug(err) + logging.debug("ProcessInThread subprocess terminated with returncode %s", proc.returncode)