From 6f1c70cf4b1abb5735e0fa3ce714ad6ab8f68784 Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Fri, 17 Jun 2022 15:53:32 +0100 Subject: [PATCH 1/2] tests/test_wlgen_rtapp.py: Fix workload cleanup FIX Ensure the workload is cleaned during the method call, so that we don't try to interact with the target when the interpreter is shutting down. --- tests/test_wlgen_rtapp.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_wlgen_rtapp.py b/tests/test_wlgen_rtapp.py index 49d0953a4..08eecc360 100644 --- a/tests/test_wlgen_rtapp.py +++ b/tests/test_wlgen_rtapp.py @@ -313,9 +313,7 @@ class TestRTACalibrationConf(RTABase): self.target, name='test', res_dir=self.res_dir, profile=profile, calibration=calibration) - rtapp.deploy() - - with open(rtapp.local_json) as fh: + with rtapp, open(rtapp.local_json) as fh: return json.load(fh)['global']['calibration'] def test_calibration_conf_pload_nodata(self): -- GitLab From b89cd372b702f44f5c0355a09bbfed0973a8fd49 Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Fri, 17 Jun 2022 13:53:54 +0100 Subject: [PATCH 2/2] lisa.utils: Speedup Loggable.get_logger() FIX Logging directly or indirectly inside a destructor (__del__) is problematic as they are invoked when the world is breaking down around them, which can trigger some harmless exceptions. For this reason, get_logger() was returning a dummy logger if __del__ was found in the stack. inspect.stack() is unfortunately really slow (150ms to get a stack frame of depth 25), which is enough to really slowdown the import of some modules and runtime code as well. Fix that by returning a wrapped logger object that will only inpsect the stack if an exception is raised. --- lisa/utils.py | 54 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/lisa/utils.py b/lisa/utils.py index 85fa4e491..e9552dafa 100644 --- a/lisa/utils.py +++ b/lisa/utils.py @@ -235,13 +235,36 @@ class instancemethod: ) -class _DummyLogger: +class _WrappedLogger: + def __init__(self, logger): + self.logger = logger + def __getattr__(self, attr): - x = getattr(logging, attr) + x = getattr(self.logger, attr) + if callable(x): - return lambda *args, **kwargs: None + @functools.wraps(x) + def wrapper(*args, **kwargs): + try: + return x(*args, **kwargs) + except Exception as e: + # If we are invoked inside a destructor, the world may be + # broken and problems are expected, so exceptions can be + # silenced. + # + # Note: We only do the check if a problem occurs as + # inspect.stack() is very costly (150ms for 25 frames) + if any ( + frame.function == '__del__' + for frame in inspect.stack() + ): + return None + else: + raise + + return wrapper else: - return None + return x class Loggable: @@ -259,21 +282,16 @@ class Loggable: @classmethod def get_logger(cls, suffix=None): - if any ( - frame.function == '__del__' - for frame in inspect.stack() - ): - return _DummyLogger() + cls_name = cls.__name__ + module = inspect.getmodule(cls) + if module: + name = module.__name__ + '.' + cls_name else: - cls_name = cls.__name__ - module = inspect.getmodule(cls) - if module: - name = module.__name__ + '.' + cls_name - else: - name = cls_name - if suffix: - name += '.' + suffix - return logging.getLogger(name) + name = cls_name + if suffix: + name += '.' + suffix + logger = logging.getLogger(name) + return _WrappedLogger(logger) @classmethod def log_locals(cls, var_names=None, level='debug'): -- GitLab