From 791d6e63be09b361dd3397707a0507399b9a9ce7 Mon Sep 17 00:00:00 2001 From: Richard Purdie Date: Tue, 2 Nov 2021 11:06:55 +0000 Subject: bitbake: daemonize: Avoid unclosed file warning In theory we can leak the so file descriptor so refactor the code to avoid that. (Bitbake rev: dfad69d4d8c894a5e1e2686023e41552de09bf3b) Signed-off-by: Richard Purdie --- bitbake/lib/bb/daemonize.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'bitbake') diff --git a/bitbake/lib/bb/daemonize.py b/bitbake/lib/bb/daemonize.py index c187fcfc6c..40fabd0c0a 100644 --- a/bitbake/lib/bb/daemonize.py +++ b/bitbake/lib/bb/daemonize.py @@ -74,26 +74,26 @@ def createDaemon(function, logfile): with open('/dev/null', 'r') as si: os.dup2(si.fileno(), sys.stdin.fileno()) - try: - so = open(logfile, 'a+') - os.dup2(so.fileno(), sys.stdout.fileno()) - os.dup2(so.fileno(), sys.stderr.fileno()) - except io.UnsupportedOperation: - sys.stdout = open(logfile, 'a+') + with open(logfile, 'a+') as so: + try: + os.dup2(so.fileno(), sys.stdout.fileno()) + os.dup2(so.fileno(), sys.stderr.fileno()) + except io.UnsupportedOperation: + sys.stdout = so - # Have stdout and stderr be the same so log output matches chronologically - # and there aren't two seperate buffers - sys.stderr = sys.stdout + # Have stdout and stderr be the same so log output matches chronologically + # and there aren't two seperate buffers + sys.stderr = sys.stdout - try: - function() - except Exception as e: - traceback.print_exc() - finally: - bb.event.print_ui_queue() - # os._exit() doesn't flush open files like os.exit() does. Manually flush - # stdout and stderr so that any logging output will be seen, particularly - # exception tracebacks. - sys.stdout.flush() - sys.stderr.flush() - os._exit(0) + try: + function() + except Exception as e: + traceback.print_exc() + finally: + bb.event.print_ui_queue() + # os._exit() doesn't flush open files like os.exit() does. Manually flush + # stdout and stderr so that any logging output will be seen, particularly + # exception tracebacks. + sys.stdout.flush() + sys.stderr.flush() + os._exit(0) -- cgit v1.2.3-54-g00ecf