Skip to content

Commit b1a5e9d

Browse files
committed
Replace subprocess.run with run_python_code_safely
1 parent 0d23bb6 commit b1a5e9d

File tree

2 files changed

+3
-11
lines changed

2 files changed

+3
-11
lines changed

cuda_bindings/cuda/bindings/_path_finder/load_dl_common.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# Copyright 2025 NVIDIA Corporation. All rights reserved.
22
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
33

4-
import os
5-
import subprocess # nosec B404
6-
import sys
74
from dataclasses import dataclass
85
from typing import Callable, Optional
96

7+
from cuda.bindings._path_finder.run_python_code_safely import run_python_code_safely
108
from cuda.bindings._path_finder.supported_libs import DIRECT_DEPENDENCIES
119

1210

@@ -45,13 +43,7 @@ def load_dependencies(libname: str, load_func: Callable[[str], LoadedDL]) -> Non
4543

4644
def load_in_subprocess(python_code, timeout=30):
4745
# This is to avoid loading libraries into the parent process.
48-
return subprocess.run( # nosec B603
49-
[sys.executable, "-c", python_code],
50-
capture_output=True,
51-
encoding="utf-8",
52-
timeout=timeout, # Ensure this does not hang for an excessive amount of time.
53-
env=os.environ, # Pass current environment
54-
)
46+
return run_python_code_safely(python_code, timeout=timeout)
5547

5648

5749
def build_subprocess_failed_for_libname_message(libname, result):

cuda_bindings/cuda/bindings/_path_finder/run_python_code_safely.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from io import StringIO
55

66

7-
def run_python_code_safely(python_code, timeout=None):
7+
def run_python_code_safely(python_code, *, timeout=None):
88
"""Replacement for subprocess.run that forces 'spawn' context"""
99
ctx = multiprocessing.get_context("spawn")
1010
result_queue = ctx.Queue()

0 commit comments

Comments
 (0)