From 6bf48e60badc52f67f16900c639a173528b68fa1 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sat, 16 Jan 2016 01:58:45 +0000 Subject: [PATCH] worker: make parent errors readable in log. --- artiq/master/worker.py | 6 ++++-- artiq/master/worker_impl.py | 30 ++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/artiq/master/worker.py b/artiq/master/worker.py index 6471ee0d8..db8a2ca64 100644 --- a/artiq/master/worker.py +++ b/artiq/master/worker.py @@ -180,9 +180,11 @@ class Worker: try: data = func(*obj["args"], **obj["kwargs"]) reply = {"status": "ok", "data": data} - except: + except Exception as e: reply = {"status": "failed", - "message": traceback.format_exc()} + "exception": traceback.format_exception_only(type(e), e)[0][:-1], + "message": str(e), + "traceback": traceback.format_tb(e.__traceback__)} await self.io_lock.acquire() try: await self._send(reply) diff --git a/artiq/master/worker_impl.py b/artiq/master/worker_impl.py index 2ff5f3f56..c132e827a 100644 --- a/artiq/master/worker_impl.py +++ b/artiq/master/worker_impl.py @@ -2,6 +2,7 @@ import sys import time import os import logging +import traceback from collections import OrderedDict import artiq @@ -30,7 +31,7 @@ class ParentActionError(Exception): pass -def make_parent_action(action, exception=ParentActionError): +def make_parent_action(action, exception=None): def parent_action(*args, **kwargs): request = {"action": action, "args": args, "kwargs": kwargs} put_object(request) @@ -43,7 +44,12 @@ def make_parent_action(action, exception=ParentActionError): if reply["status"] == "ok": return reply["data"] else: - raise exception(reply["message"]) + if exception is None: + exn = ParentActionError(reply["exception"]) + else: + exn = exception(reply["message"]) + exn.parent_traceback = reply["traceback"] + raise exn return parent_action @@ -172,11 +178,15 @@ def string_to_hdf5(f, key, value): def setup_diagnostics(experiment_file, repository_path): def render_diagnostic(self, diagnostic): - message = "Cannot compile {}\n".format(experiment_file) + \ + message = "While compiling {}\n".format(experiment_file) + \ _render_diagnostic(diagnostic, colored=False) if repository_path is not None: message = message.replace(repository_path, "") - logging.error(message) + + if diagnostic.level == 'warning': + logging.warn(message) + else: + logging.error(message) # This is kind of gross, but 1) we do not have any explicit connection # between the worker and a coredevice.core.Core instance at all, @@ -256,12 +266,12 @@ def main(): except CompileError: pass except Exception as exc: - short_exc_info = type(exc).__name__ - exc_str = str(exc) - if exc_str: - short_exc_info += ": " + exc_str - logging.error("Terminating with exception (%s)", - short_exc_info, exc_info=True) + lines = ["Terminating with exception\n"] + lines += traceback.format_exception_only(type(exc), exc) + if hasattr(exc, 'parent_traceback'): + lines += exc.parent_traceback + logging.error("".join(lines).rstrip(), + exc_info=not hasattr(exc, 'parent_traceback')) finally: device_mgr.close_devices()