[bitbake-devel,1/4] bitbake: knotty: Add logging cleanup

Submitted by Joshua Watt on March 11, 2020, 11:28 p.m. | Patch ID: 170981

Details

Message ID 20200311232847.2874-2-JPEWhacker@gmail.com
State New
Headers show

Commit Message

Joshua Watt March 11, 2020, 11:28 p.m.
Adds code to close all loggers when bitbake exits. This prevents
unclosed file ResourceWarnings. A form of this closing existed
previously, but was removed in the new logging code.

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
---
 bitbake/lib/bb/msg.py       | 11 +++++++++++
 bitbake/lib/bb/ui/knotty.py |  2 ++
 2 files changed, 13 insertions(+)

Patch hide | download patch | download mbox

diff --git a/bitbake/lib/bb/msg.py b/bitbake/lib/bb/msg.py
index ced605d96d..ee3c38b5a4 100644
--- a/bitbake/lib/bb/msg.py
+++ b/bitbake/lib/bb/msg.py
@@ -284,3 +284,14 @@  def setLoggingConfig(defaultconfig, userconfigfile=None):
         # TODO: I don't think that setting the global log level should be necessary
         #if newlevel < bb.msg.loggerDefaultLogLevel:
         #    bb.msg.loggerDefaultLogLevel = newlevel
+
+def cleanupLogging():
+    # Iterate through all the handlers and close them if possible. Fixes
+    # 'Unclosed resource' warnings when bitbake exits, see
+    # https://bugs.python.org/issue23010
+    handlers = set()
+    for logger_iter in logging.Logger.manager.loggerDict.keys():
+        handlers.update(logging.getLogger(logger_iter).handlers)
+
+    for h in handlers:
+        h.close()
diff --git a/bitbake/lib/bb/ui/knotty.py b/bitbake/lib/bb/ui/knotty.py
index b229687ef8..2dfce90a4f 100644
--- a/bitbake/lib/bb/ui/knotty.py
+++ b/bitbake/lib/bb/ui/knotty.py
@@ -823,4 +823,6 @@  def main(server, eventHandler, params, tf = TerminalFilter):
         if e.errno == errno.EPIPE:
             pass
 
+    bb.msg.cleanupLogging()
+
     return return_value