[CLN] clean process die

This commit is contained in:
Benedek Racz 2020-01-23 13:31:18 +01:00
parent 6a9b086d0b
commit 46519bb4cf
4 changed files with 21 additions and 29 deletions

View File

@ -28,9 +28,9 @@ class TestCaseConstructor(PexpectTestCase.PexpectTestCase):
the same results for different styles of invoking __init__(). the same results for different styles of invoking __init__().
This assumes that the root directory / is static during the test. This assumes that the root directory / is static during the test.
''' '''
p1 = wexpect.spawn('uname -m -n -p -r -s -v') p1 = wexpect.spawn('uname -m -n -p -r -s -v', timeout=5, port=4321)
p1.expect(wexpect.EOF) p1.expect(wexpect.EOF)
p2 = wexpect.spawn('uname', ['-m', '-n', '-p', '-r', '-s', '-v']) p2 = wexpect.spawn('uname', ['-m', '-n', '-p', '-r', '-s', '-v'], timeout=5, port=4322)
p2.expect(wexpect.EOF) p2.expect(wexpect.EOF)
self.assertEqual(p1.before, p2.before) self.assertEqual(p1.before, p2.before)
self.assertEqual(str(p1).splitlines()[1:9], str(p2).splitlines()[1:9]) self.assertEqual(str(p1).splitlines()[1:9], str(p2).splitlines()[1:9])

View File

@ -11,7 +11,7 @@ from .console_reader import ConsoleReaderPipe
from .spawn import SpawnSocket from .spawn import SpawnSocket
from .spawn import SpawnPipe from .spawn import SpawnPipe
from .spawn import SpawnPipe as spawn from .spawn import SpawnSocket as spawn
from .spawn import run from .spawn import run
__all__ = ['split_command_line', 'join_args', 'ExceptionPexpect', 'EOF', 'TIMEOUT', __all__ = ['split_command_line', 'join_args', 'ExceptionPexpect', 'EOF', 'TIMEOUT',

View File

@ -157,14 +157,6 @@ class ConsoleReaderBase:
time.sleep(.1) time.sleep(.1)
self.terminate_child()
time.sleep(.1)
self.send_to_host(self.readConsoleToCursor())
self.sendeof()
time.sleep(.1)
self.close_connection()
logger.info('Console finished.')
def read_loop(self): def read_loop(self):
paused = False paused = False
@ -201,14 +193,10 @@ class ConsoleReaderBase:
def terminate_child(self): def terminate_child(self):
try: try:
win32process.TerminateProcess(self.__childProcess, 0) if self.child_process:
except pywintypes.error as e: self.child_process.kill()
""" 'Access denied' happens always? Perhaps if not running as admin (or UAC except psutil.NoSuchProcess:
enabled under Vista/7). Don't log. Child process will exit regardless when logger.info('The process has already died.')
calling sys.exit
"""
# if e.args[0] != winerror.ERROR_ACCESS_DENIED:
logger.info(e)
return return
def isalive(self, process): def isalive(self, process):
@ -477,12 +465,13 @@ class ConsoleReaderSocket(ConsoleReaderBase):
def send_to_host(self, msg): def send_to_host(self, msg):
# convert to bytes # convert to bytes
msg_bytes = str.encode(msg) if isinstance(msg, str):
if msg_bytes: msg = str.encode(msg)
logger.debug(f'Sending msg: {msg_bytes}') if msg:
logger.debug(f'Sending msg: {msg}')
else: else:
logger.spam(f'Sending msg: {msg_bytes}') logger.spam(f'Sending msg: {msg}')
self.connection.sendall(msg_bytes) self.connection.sendall(msg)
def get_from_host(self): def get_from_host(self):
try: try:
@ -524,12 +513,13 @@ class ConsoleReaderPipe(ConsoleReaderBase):
def send_to_host(self, msg): def send_to_host(self, msg):
# convert to bytes # convert to bytes
msg_bytes = str.encode(msg) if isinstance(msg, str):
if msg_bytes: msg = str.encode(msg)
logger.debug(f'Sending msg: {msg_bytes}') if msg:
logger.debug(f'Sending msg: {msg}')
else: else:
logger.spam(f'Sending msg: {msg_bytes}') logger.spam(f'Sending msg: {msg}')
win32file.WriteFile(self.pipe, msg_bytes) win32file.WriteFile(self.pipe, msg)
def get_from_host(self): def get_from_host(self):
data, avail, bytes_left = win32pipe.PeekNamedPipe(self.pipe, 4096) data, avail, bytes_left = win32pipe.PeekNamedPipe(self.pipe, 4096)

View File

@ -291,12 +291,14 @@ class SpawnBase:
garbage collects Python objects, not the child console.""" garbage collects Python objects, not the child console."""
try: try:
logger.info('Deleting...')
self.terminate() self.terminate()
self.disconnect_from_child() self.disconnect_from_child()
if self.safe_exit: if self.safe_exit:
self.wait() self.wait()
except: except:
traceback.print_exc() traceback.print_exc()
logger.warning(traceback.format_exc())
def __str__(self): def __str__(self):
"""This returns a human-readable string that represents the state of """This returns a human-readable string that represents the state of