Skip to content

Commit e1eca52

Browse files
authored
Handle logger exception when flushing already closed file
1 parent 264aade commit e1eca52

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
From e23dce108588a9c52d3f7542636892750d6efcbd Mon Sep 17 00:00:00 2001
2+
From: Marek Czernek <[email protected]>
3+
Date: Thu, 17 Oct 2024 15:52:00 +0200
4+
Subject: [PATCH] Handle logger flushing already closed file (#686)
5+
6+
This is a partial cherry-pick of
7+
https://github.com/saltstack/salt/commit/9683260d61668da8559ecde6caf63a52fedd8790
8+
---
9+
salt/_logging/handlers.py | 4 ++++
10+
salt/_logging/impl.py | 10 +++++++++-
11+
2 files changed, 13 insertions(+), 1 deletion(-)
12+
13+
diff --git a/salt/_logging/handlers.py b/salt/_logging/handlers.py
14+
index 5a1a1613137..d8bc68a49db 100644
15+
--- a/salt/_logging/handlers.py
16+
+++ b/salt/_logging/handlers.py
17+
@@ -95,6 +95,9 @@ class DeferredStreamHandler(StreamHandler):
18+
super().__init__(stream)
19+
self.__messages = deque(maxlen=max_queue_size)
20+
self.__emitting = False
21+
+ import traceback
22+
+
23+
+ self.stack = "".join(traceback.format_stack())
24+
25+
def handle(self, record):
26+
self.acquire()
27+
@@ -116,6 +119,7 @@ class DeferredStreamHandler(StreamHandler):
28+
super().handle(record)
29+
finally:
30+
self.__emitting = False
31+
+ # This will raise a ValueError if the file handle has been closed.
32+
super().flush()
33+
34+
def sync_with_handlers(self, handlers=()):
35+
diff --git a/salt/_logging/impl.py b/salt/_logging/impl.py
36+
index 4d1ebd2495f..9d76c3174e2 100644
37+
--- a/salt/_logging/impl.py
38+
+++ b/salt/_logging/impl.py
39+
@@ -488,7 +488,15 @@ def setup_temp_handler(log_level=None):
40+
break
41+
else:
42+
handler = DeferredStreamHandler(sys.stderr)
43+
- atexit.register(handler.flush)
44+
+
45+
+ def tryflush():
46+
+ try:
47+
+ handler.flush()
48+
+ except ValueError:
49+
+ # File handle has already been closed.
50+
+ pass
51+
+
52+
+ atexit.register(tryflush)
53+
handler.setLevel(log_level)
54+
55+
# Set the default temporary console formatter config
56+
--
57+
2.47.0
58+

salt/salt.spec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ Patch143: fix-x509-test-fails-on-old-openssl-systems-682.patch
452452
Patch144: fix-x509-private-key-tests-and-test_suse-on-sle12-68.patch
453453
# PATCH-FIX_OPENSUSE: https://github.com/openSUSE/salt/pull/685
454454
Patch145: enhance-cleanup-mechanism-after-salt-bundle-upgrade-.patch
455+
# PATCH-FIX_UPSTREAM: https://github.com/saltstack/salt/commit/9683260d61668da8559ecde6caf63a52fedd8790
456+
Patch146: handle-logger-flushing-already-closed-file-686.patch
455457

456458
### IMPORTANT: The line below is used as a snippet marker. Do not touch it.
457459
### SALT PATCHES LIST END

0 commit comments

Comments
 (0)