From 388b81af19287bad8e187fb880d179b56492a820 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 2 Jul 2022 17:48:18 +0800 Subject: [PATCH] moninj,corelog: fix/cleanup exception handling (#1897) --- artiq/coredevice/comm_moninj.py | 4 ++ artiq/frontend/aqctl_corelog.py | 82 +++++++++++++++++---------------- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/artiq/coredevice/comm_moninj.py b/artiq/coredevice/comm_moninj.py index 26505a08d..9d0713e0e 100644 --- a/artiq/coredevice/comm_moninj.py +++ b/artiq/coredevice/comm_moninj.py @@ -91,6 +91,10 @@ class CommMonInj: self.injection_status_cb(channel, override, value) else: raise ValueError("Unknown packet type", ty) + except asyncio.CancelledError: + raise + except: + logger.error("Moninj connection terminating with exception", exc_info=True) finally: if self.disconnect_cb is not None: self.disconnect_cb() diff --git a/artiq/frontend/aqctl_corelog.py b/artiq/frontend/aqctl_corelog.py index 8dcf69abc..8c766ecf5 100755 --- a/artiq/frontend/aqctl_corelog.py +++ b/artiq/frontend/aqctl_corelog.py @@ -40,44 +40,49 @@ async def get_logs_sim(host): async def get_logs(host): - reader, writer = await async_open_connection( - host, - 1380, - after_idle=1, - interval=1, - max_fails=3, - ) - writer.write(b"ARTIQ management\n") - endian = await reader.readexactly(1) - if endian == b"e": - endian = "<" - elif endian == b"E": - endian = ">" - else: - raise IOError("Incorrect reply from device: expected e/E.") - writer.write(struct.pack("B", Request.PullLog.value)) - await writer.drain() + try: + reader, writer = await async_open_connection( + host, + 1380, + after_idle=1, + interval=1, + max_fails=3, + ) + writer.write(b"ARTIQ management\n") + endian = await reader.readexactly(1) + if endian == b"e": + endian = "<" + elif endian == b"E": + endian = ">" + else: + raise IOError("Incorrect reply from device: expected e/E.") + writer.write(struct.pack("B", Request.PullLog.value)) + await writer.drain() - while True: - length, = struct.unpack(endian + "l", await reader.readexactly(4)) - log = await reader.readexactly(length) + while True: + length, = struct.unpack(endian + "l", await reader.readexactly(4)) + log = await reader.readexactly(length) - for line in log.decode("utf-8").splitlines(): - m = re.match(r"^\[.+?\] (TRACE|DEBUG| INFO| WARN|ERROR)\((.+?)\): (.+)$", line) - levelname = m.group(1) - if levelname == 'TRACE': - level = logging.TRACE - elif levelname == 'DEBUG': - level = logging.DEBUG - elif levelname == ' INFO': - level = logging.INFO - elif levelname == ' WARN': - level = logging.WARN - elif levelname == 'ERROR': - level = logging.ERROR - name = 'firmware.' + m.group(2).replace('::', '.') - text = m.group(3) - log_with_name(name, level, text) + for line in log.decode("utf-8").splitlines(): + m = re.match(r"^\[.+?\] (TRACE|DEBUG| INFO| WARN|ERROR)\((.+?)\): (.+)$", line) + levelname = m.group(1) + if levelname == 'TRACE': + level = logging.TRACE + elif levelname == 'DEBUG': + level = logging.DEBUG + elif levelname == ' INFO': + level = logging.INFO + elif levelname == ' WARN': + level = logging.WARN + elif levelname == 'ERROR': + level = logging.ERROR + name = 'firmware.' + m.group(2).replace('::', '.') + text = m.group(3) + log_with_name(name, level, text) + except asyncio.CancelledError: + raise + except: + logger.error("Logging connection terminating with exception", exc_info=True) def main(): @@ -99,8 +104,7 @@ def main(): _, pending = loop.run_until_complete(asyncio.wait( [signal_handler.wait_terminate(), server.wait_terminate(), - get_logs_task - ], + get_logs_task], return_when=asyncio.FIRST_COMPLETED)) for task in pending: task.cancel() @@ -108,8 +112,6 @@ def main(): loop.run_until_complete(server.stop()) finally: pass - except Exception: - logger.error("Termination due to exception", exc_info=True) finally: signal_handler.teardown() finally: