diff --git a/salt/_logging/handlers.py b/salt/_logging/handlers.py index 5a1a161313..d8bc68a49d 100644 --- a/salt/_logging/handlers.py +++ b/salt/_logging/handlers.py @@ -95,6 +95,9 @@ def __init__(self, stream, max_queue_size=10000): super().__init__(stream) self.__messages = deque(maxlen=max_queue_size) self.__emitting = False + import traceback + + self.stack = "".join(traceback.format_stack()) def handle(self, record): self.acquire() @@ -116,6 +119,7 @@ def flush(self): super().handle(record) finally: self.__emitting = False + # This will raise a ValueError if the file handle has been closed. super().flush() def sync_with_handlers(self, handlers=()): diff --git a/salt/_logging/impl.py b/salt/_logging/impl.py index 4d1ebd2495..9d76c3174e 100644 --- a/salt/_logging/impl.py +++ b/salt/_logging/impl.py @@ -488,7 +488,15 @@ def setup_temp_handler(log_level=None): break else: handler = DeferredStreamHandler(sys.stderr) - atexit.register(handler.flush) + + def tryflush(): + try: + handler.flush() + except ValueError: + # File handle has already been closed. + pass + + atexit.register(tryflush) handler.setLevel(log_level) # Set the default temporary console formatter config