From 5589ecbac42bb89b3c1c53fa4754744c9292b7d0 Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Fri, 29 Jun 2018 23:59:43 +0300 Subject: improved streamer killing --- kvmd/kvmd/streamer.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'kvmd') diff --git a/kvmd/kvmd/streamer.py b/kvmd/kvmd/streamer.py index 5ee16871..6e3ccffc 100644 --- a/kvmd/kvmd/streamer.py +++ b/kvmd/kvmd/streamer.py @@ -57,8 +57,8 @@ class Streamer: # pylint: disable=too-many-instance-attributes async def __process(self) -> None: logger = get_logger(0) - proc: Optional[asyncio.subprocess.Process] = None # pylint: disable=no-member - while True: + while True: # pylint: disable=too-many-nested-blocks + proc: Optional[asyncio.subprocess.Process] = None # pylint: disable=no-member try: proc = await asyncio.create_subprocess_shell( self.__cmd, @@ -78,27 +78,36 @@ class Streamer: # pylint: disable=too-many-instance-attributes if empty == 100: # asyncio bug break - await self.__kill(proc) raise RuntimeError("WTF") except asyncio.CancelledError: break + except Exception as err: if proc: - logger.exception("Unexpected finished streamer pid=%d with retcode=%d", proc.pid, proc.returncode) + logger.exception("Unexpected streamer error: pid=%d", proc.pid) else: logger.exception("Can't start streamer: %s", err) await asyncio.sleep(1) - if proc: - await self.__kill(proc) + finally: + if proc and proc.returncode is None: + await self.__kill(proc) async def __kill(self, proc: asyncio.subprocess.Process) -> None: # pylint: disable=no-member try: proc.terminate() await asyncio.sleep(1) if proc.returncode is None: - proc.kill() + try: + proc.kill() + except Exception: + if proc.returncode is not None: + raise await proc.wait() + get_logger().info("Streamer killed: pid=%d; retcode=%d") except Exception: - pass + if proc.returncode is None: + get_logger().exception("Can't kill streamer pid=%d", proc.pid) + else: + get_logger().info("Streamer killed: pid=%d; retcode=%d", proc.pid, proc.returncode) -- cgit v1.2.3