From d5d41ef1a0efad1e87acd36c9b33789f127dd487 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 7 Oct 2020 10:41:36 +0200 Subject: [PATCH] pyln: Remove any logging handlers at teardown to avoid logging error Inspired by https://github.com/pytest-dev/pytest/issues/5502#issuecomment-647157873 --- contrib/pyln-testing/pyln/testing/fixtures.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/contrib/pyln-testing/pyln/testing/fixtures.py b/contrib/pyln-testing/pyln/testing/fixtures.py index f13dcfb7b..a68dd8988 100644 --- a/contrib/pyln-testing/pyln/testing/fixtures.py +++ b/contrib/pyln-testing/pyln/testing/fixtures.py @@ -40,14 +40,25 @@ def test_base_dir(): @pytest.fixture(autouse=True) def setup_logging(): - logger = logging.getLogger() - before_handlers = list(logger.handlers) + """Enable logging before a test, and remove all handlers afterwards. + This "fixes" the issue with pytest swapping out sys.stdout and sys.stderr + in order to capture the output, but then doesn't wait for the handlers to + terminate before closing the buffers. It just iterates through all + loggers, and removes any handlers that might be pointing at sys.stdout or + sys.stderr. + + """ if TEST_DEBUG: logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) yield - logger.handlers = before_handlers + + loggers = [logging.getLogger()] + list(logging.Logger.manager.loggerDict.values()) + for logger in loggers: + handlers = getattr(logger, 'handlers', []) + for handler in handlers: + logger.removeHandler(handler) @pytest.fixture