All of lore.kernel.org
 help / color / mirror / Atom feed
From: AdrianF <adrian.freihofer@siemens.com>
To: openembedded-core@lists.openembedded.org
Cc: Adrian Freihofer <adrian.freihofer@siemens.com>
Subject: [PATCH 15/15] buildstats: close proc/disk log files via weakref.finalize safety net
Date: Wed,  9 Sep 2026 23:53:15 +0200	[thread overview]
Message-ID: <20260909215337.89106-16-adrian.freihofer@siemens.com> (raw)
In-Reply-To: <20260909215337.89106-1-adrian.freihofer@siemens.com>

From: Adrian Freihofer <adrian.freihofer@siemens.com>

SystemStats.close() is normally invoked by the buildstats.bbclass handler
on bb.event.BuildCompleted, but that event is skipped for internal,
event-less task runs (e.g. tinfoil's run_prepared_task(), used by
devtool ide-sdk). Without a BuildCompleted event, the open proc/disk log
files were only closed implicitly at GC/interpreter-exit time, causing a
ResourceWarning for leaked file handles.

Register a weakref.finalize() callback at construction time as a safety
net. close() now just triggers the finalizer, which is idempotent and
keeps no reference back to SystemStats itself.

AI-Generated: Uses GitHub Copilot

Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
---
 meta/lib/oe/buildstats.py | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/meta/lib/oe/buildstats.py b/meta/lib/oe/buildstats.py
index db3d26afb8..56228246e4 100644
--- a/meta/lib/oe/buildstats.py
+++ b/meta/lib/oe/buildstats.py
@@ -10,6 +10,7 @@
 import os
 import time
 import re
+import weakref
 import bb.event
 from collections import deque
 
@@ -56,6 +57,12 @@ class SystemStats:
                     destfile = os.path.join(bsdir, '%sproc_%s.log' % ('reduced_' if handler else '', filename))
                 self.proc_files.append((filename, open(destfile, 'ab'), handler))
         self.monitor_disk = open(os.path.join(bsdir, 'monitor_disk.log'), 'ab')
+        # Safety net: normally close() is called when bb.event.BuildCompleted
+        # fires, but that event is skipped for internal, event-less task runs
+        # (e.g. tinfoil's run_prepared_task()). Without this, the open files
+        # would only get closed implicitly at GC/interpreter-exit time,
+        # triggering a ResourceWarning.
+        self._finalizer = weakref.finalize(self, self._close_files, self.proc_files, self.monitor_disk)
         # Last time that we sampled /proc data resp. recorded disk monitoring data.
         self.last_proc = 0
         self.last_disk_monitor = 0
@@ -81,8 +88,12 @@ class SystemStats:
         self.net_stats = {}
 
     def close(self):
-        self.monitor_disk.close()
-        for _, output, _ in self.proc_files:
+        self._finalizer()
+
+    @staticmethod
+    def _close_files(proc_files, monitor_disk):
+        monitor_disk.close()
+        for _, output, _ in proc_files:
             output.close()
 
     def _reduce_meminfo(self, time, data, filename):
-- 
2.55.0



      parent reply	other threads:[~2026-09-09 21:53 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-09 21:53 [PATCH 00/15] devtool ide-sdk: support booting from NFS, VSCode clangd AdrianF
2026-09-09 21:53 ` [PATCH 01/15] devtool: deploy: split ssh deployment into a separate function AdrianF
2026-09-09 21:53 ` [PATCH 02/15] devtool: deploy: allow deploying directly into a local rootfs directory AdrianF
2026-09-09 21:53 ` [PATCH 03/15] oe-selftest: devtool deploy-target: test deploying into a local rootfs path AdrianF
2026-09-09 21:53 ` [PATCH 04/15] devtool: ide-sdk: support NFS rootfs AdrianF
2026-09-09 21:53 ` [PATCH 05/15] oe-selftest: devtool ide-sdk: test NFS debug rootfs AdrianF
2026-09-09 21:53 ` [PATCH 06/15] runqemu-extract-sdk: refactor in Python AdrianF
2026-09-10 17:45   ` [OE-core] " Richard Purdie
2026-09-09 21:53 ` [PATCH 07/15] devtool: ide-sdk: reuse pseudo_rootfs_utils for SDK rootfs extraction AdrianF
2026-09-09 21:53 ` [PATCH 08/15] cpp-example: update formatting and code style AdrianF
2026-09-09 21:53 ` [PATCH 09/15] cpp-example: fix clang-tidy warnings AdrianF
2026-09-09 21:53 ` [PATCH 10/15] devtool: ide-sdk: add clangd support for VSCode IntelliSense AdrianF
2026-09-09 21:53 ` [PATCH 11/15] devtool: ide-sdk: support clangd for non-clang toolchain recipes AdrianF
2026-09-09 21:53 ` [PATCH 12/15] devtool: ide-sdk: format C/C++ with clangd when .clang-format is present AdrianF
2026-09-09 21:53 ` [PATCH 13/15] devtool: ide-sdk: support LLDB ATTACH mode AdrianF
2026-09-09 21:53 ` [PATCH 14/15] devtool: ide-sdk: pin BBPATH in generated do_install script AdrianF
2026-09-09 21:53 ` AdrianF [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260909215337.89106-16-adrian.freihofer@siemens.com \
    --to=adrian.freihofer@siemens.com \
    --cc=openembedded-core@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.