From 23b2a545cfddb7e3816cbcc77be6a81dc8a5e0b9 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sat, 9 Jun 2018 17:04:10 +0100 Subject: [PATCH] fork: avoid another logging deadlock at startup. The very first task /must/ be clearing out logging locks, since _at_fork() functions call LOG.debug() via Side.close(). Additionally, the root logger is not included in loggerDict, so we must specify it explicitly. --- mitogen/fork.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/mitogen/fork.py b/mitogen/fork.py index 8b29ad0c..dd6008db 100644 --- a/mitogen/fork.py +++ b/mitogen/fork.py @@ -51,7 +51,7 @@ def fixup_prngs(): sys.modules['ssl'].RAND_add(s, 75.0) -def break_logging_locks(): +def reset_logging_framework(): """ After fork, ensure any logging.Handler locks are recreated, as a variety of threads in the parent may have been using the logging package at the moment @@ -61,10 +61,19 @@ def break_logging_locks(): https://github.com/dw/mitogen/issues/150 for a full discussion. """ logging._lock = threading.RLock() - for name in logging.Logger.manager.loggerDict: + + # The root logger does not appear in the loggerDict. + for name in [None] + list(logging.Logger.manager.loggerDict): for handler in logging.getLogger(name).handlers: handler.createLock() + root = logging.getLogger() + root.handlers = [ + handler + for handler in root.handlers + if not isinstance(handler, mitogen.core.LogHandler) + ] + def handle_child_crash(): """ @@ -125,10 +134,10 @@ class Stream(mitogen.parent.Stream): handle_child_crash() def _child_main(self, childfp): + reset_logging_framework() # Must be first! + fixup_prngs() mitogen.core.Latch._on_fork() mitogen.core.Side._on_fork() - break_logging_locks() - fixup_prngs() if self.on_fork: self.on_fork() mitogen.core.set_block(childfp.fileno())