Skip to content

Commit

Permalink
Handle logger flushing already closed file
Browse files Browse the repository at this point in the history
This is a partial cherry-pick of
saltstack/salt@9683260
  • Loading branch information
m-czernek committed Oct 16, 2024
1 parent 994ebca commit ac729ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions salt/_logging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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=()):
Expand Down
10 changes: 9 additions & 1 deletion salt/_logging/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ac729ef

Please sign in to comment.