[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__().
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)
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)
self.assertEqual(p1.before, p2.before)
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 SpawnPipe
from .spawn import SpawnPipe as spawn
from .spawn import SpawnSocket as spawn
from .spawn import run
__all__ = ['split_command_line', 'join_args', 'ExceptionPexpect', 'EOF', 'TIMEOUT',

View File

@ -157,14 +157,6 @@ class ConsoleReaderBase:
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):
paused = False
@ -201,14 +193,10 @@ class ConsoleReaderBase:
def terminate_child(self):
try:
win32process.TerminateProcess(self.__childProcess, 0)
except pywintypes.error as e:
""" 'Access denied' happens always? Perhaps if not running as admin (or UAC
enabled under Vista/7). Don't log. Child process will exit regardless when
calling sys.exit
"""
# if e.args[0] != winerror.ERROR_ACCESS_DENIED:
logger.info(e)
if self.child_process:
self.child_process.kill()
except psutil.NoSuchProcess:
logger.info('The process has already died.')
return
def isalive(self, process):
@ -477,12 +465,13 @@ class ConsoleReaderSocket(ConsoleReaderBase):
def send_to_host(self, msg):
# convert to bytes
msg_bytes = str.encode(msg)
if msg_bytes:
logger.debug(f'Sending msg: {msg_bytes}')
if isinstance(msg, str):
msg = str.encode(msg)
if msg:
logger.debug(f'Sending msg: {msg}')
else:
logger.spam(f'Sending msg: {msg_bytes}')
self.connection.sendall(msg_bytes)
logger.spam(f'Sending msg: {msg}')
self.connection.sendall(msg)
def get_from_host(self):
try:
@ -524,12 +513,13 @@ class ConsoleReaderPipe(ConsoleReaderBase):
def send_to_host(self, msg):
# convert to bytes
msg_bytes = str.encode(msg)
if msg_bytes:
logger.debug(f'Sending msg: {msg_bytes}')
if isinstance(msg, str):
msg = str.encode(msg)
if msg:
logger.debug(f'Sending msg: {msg}')
else:
logger.spam(f'Sending msg: {msg_bytes}')
win32file.WriteFile(self.pipe, msg_bytes)
logger.spam(f'Sending msg: {msg}')
win32file.WriteFile(self.pipe, msg)
def get_from_host(self):
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."""
try:
logger.info('Deleting...')
self.terminate()
self.disconnect_from_child()
if self.safe_exit:
self.wait()
except:
traceback.print_exc()
logger.warning(traceback.format_exc())
def __str__(self):
"""This returns a human-readable string that represents the state of