From 00da25cdde16912a3445181fda7a4518050c485a Mon Sep 17 00:00:00 2001 From: Theofilos Manitaras Date: Thu, 9 Apr 2020 17:06:38 +0200 Subject: [PATCH] Ignore error when piping output to inexistent program * Ignore the `BrokenPipeError` which is caused when the `logging.Handler` tries to `emit` to a closed stream. --- reframe/core/logging.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/reframe/core/logging.py b/reframe/core/logging.py index eb6ba5d8e8..102549b52e 100644 --- a/reframe/core/logging.py +++ b/reframe/core/logging.py @@ -101,6 +101,24 @@ def set_handler_level(hdlr, level): logging.Handler.setLevel = set_handler_level +# Here we monkeypatch the `handleError` method of `logging.Handler` in +# order to ignore `BrokenPipeError` exceptions while keeping the default +# behavior for all the other types of exceptions. + +def handleError(func): + def ignore_brokenpipe(hdlr, l): + exc_type, *_ = sys.exc_info() + if exc_type == BrokenPipeError: + pass + else: + func(hdlr, l) + + return ignore_brokenpipe + + +logging.Handler.handleError = handleError(logging.Handler.handleError) + + class MultiFileHandler(logging.FileHandler): '''A file handler that allows writing on different log files based on information from the log record.