[PATCH] run-clang-tools: run multiprocessing.Pool as context manager

Philipp Hahn posted 1 patch 3 weeks, 6 days ago
scripts/clang-tools/run-clang-tools.py | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
[PATCH] run-clang-tools: run multiprocessing.Pool as context manager
Posted by Philipp Hahn 3 weeks, 6 days ago
From: Philipp Hahn <phahn-oss@avm.de>

`multiprocessing.pool.Pool()` should be used as a context manager so
Python can free its internal resources and do a proper cleanup.[1]

While at it move the code to read the `compiler_commands.json` so the
opened file can be closed before the sub-processes are fork()ed.

Link: https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool [1]
Signed-off-by: Philipp Hahn <phahn-oss@avm.de>
---
 scripts/clang-tools/run-clang-tools.py | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/scripts/clang-tools/run-clang-tools.py b/scripts/clang-tools/run-clang-tools.py
index f31ffd09e1ea..e78be82aa693 100755
--- a/scripts/clang-tools/run-clang-tools.py
+++ b/scripts/clang-tools/run-clang-tools.py
@@ -79,14 +79,15 @@ def run_analysis(entry):
 
 
 def main():
-    try:
-        args = parse_arguments()
+    args = parse_arguments()
+
+    # Read JSON data into the datastore variable
+    with open(args.path) as f:
+        datastore = json.load(f)
 
-        lock = multiprocessing.Lock()
-        pool = multiprocessing.Pool(initializer=init, initargs=(lock, args))
-        # Read JSON data into the datastore variable
-        with open(args.path, "r") as f:
-            datastore = json.load(f)
+    lock = multiprocessing.Lock()
+    try:
+        with multiprocessing.Pool(initializer=init, initargs=(lock, args)) as pool:
             pool.map(run_analysis, datastore)
     except BrokenPipeError:
         # Python flushes standard streams on exit; redirect remaining output
-- 
2.43.0
Re: [PATCH] run-clang-tools: run multiprocessing.Pool as context manager
Posted by Nathan Chancellor 2 days, 10 hours ago
On Fri, 15 May 2026 14:47:50 +0200, Philipp Hahn wrote:
> run-clang-tools: run multiprocessing.Pool as context manager

Applied to

  https://git.kernel.org/pub/scm/linux/kernel/git/kbuild/linux.git kbuild-next

Thanks!

[1/1] run-clang-tools: run multiprocessing.Pool as context manager
      https://git.kernel.org/kbuild/c/c10ba5c9c62e2

Please look out for regression or issue reports or other follow up
comments, as they may result in the patch/series getting dropped or
reverted. Patches applied to an "unstable" branch are accepted pending
wider testing in -next and any post-commit review; they will generally
be moved to the main branch in a week if no issues are found.

Best regards,
-- 
Cheers,
Nathan