]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
run-clang-tools: run multiprocessing.Pool as context manager
authorPhilipp Hahn <phahn-oss@avm.de>
Fri, 15 May 2026 12:47:50 +0000 (14:47 +0200)
committerNathan Chancellor <nathan@kernel.org>
Wed, 27 May 2026 23:47:51 +0000 (16:47 -0700)
`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
Signed-off-by: Philipp Hahn <phahn-oss@avm.de>
Link: https://patch.msgid.link/40180613bef84946c45d6fbeb4bb274573cd0beb.1778849135.git.phahn-oss@avm.de
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
scripts/clang-tools/run-clang-tools.py

index f31ffd09e1eab1047cd67b8793e2d33fa00e26ca..e78be82aa6931b0d2b185a06cf7390d582fbe8db 100755 (executable)
@@ -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