From a5852ba199a1dfc1c9759d3c99d5a5c98161f408 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 8 Apr 2021 21:39:18 -0700 Subject: [PATCH] Remove parent process's log handlers properly --- src/ocrmypdf/builtin_plugins/concurrency.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/ocrmypdf/builtin_plugins/concurrency.py b/src/ocrmypdf/builtin_plugins/concurrency.py index 16005984..fd26e373 100644 --- a/src/ocrmypdf/builtin_plugins/concurrency.py +++ b/src/ocrmypdf/builtin_plugins/concurrency.py @@ -72,12 +72,15 @@ def process_init(q: Queue, user_init: Callable[[], None], loglevel): # Windows and Cygwin do not have pthread_sigmask or SIGBUS signal.signal(signal.SIGBUS, process_sigbus) - # Reconfigure the root logger for this process to send all messages to a queue - h = logging.handlers.QueueHandler(q) + # Remove any log handlers that belong to the parent process root = logging.getLogger() + for handler in root.handlers[:]: + root.removeHandler(handler) + handler.close() # To ensure handlers with opened resources are released + + # Set up our single log handler to forward messages to the parent root.setLevel(loglevel) - root.handlers = [] - root.addHandler(h) + root.addHandler(logging.handlers.QueueHandler(q)) user_init() return