Skip to content

Commit

Permalink
#3483 try harder to catch all paths that import numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
totaam committed Dec 3, 2023
1 parent 9d9d2fa commit ee37522
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
5 changes: 3 additions & 2 deletions xpra/client/gl/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from xpra.util.str_fn import csv, print_nested_dict
from xpra.util.env import envint, envbool
from xpra.os_util import bytestostr
from xpra.os_util import bytestostr, NumpyImportContext
from xpra.log import Logger, CaptureHandler
from xpra.client.gl.drivers import (
GL_MATCH_LIST, WHITELIST, GREYLIST, BLACKLIST,
Expand Down Expand Up @@ -322,7 +322,8 @@ def check_PyOpenGL_support(force_enable) -> dict[str, Any]:
logger.handlers = [CaptureHandler()]
logger.propagate = False

return do_check_PyOpenGL_support(force_enable)
with NumpyImportContext():
return do_check_PyOpenGL_support(force_enable)

finally:
def recs(rname) -> list[str]:
Expand Down
5 changes: 3 additions & 2 deletions xpra/codecs/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from xpra.util.str_fn import csv, print_nested_dict, pver
from xpra.util.env import envbool
from xpra.os_util import OSX, WIN32
from xpra.os_util import OSX, WIN32, NumpyImportContext
from xpra.util.version import parse_version
from xpra.codecs.constants import HELP_ORDER
from xpra.log import Logger
Expand Down Expand Up @@ -416,7 +416,8 @@ def main(args) -> int:
return 1
list_codecs = ALL_CODECS
#not really a codec, but gets used by codecs, so include version info:
add_codec_version("numpy", "numpy")
with NumpyImportContext:
add_codec_version("numpy", "numpy")

#use another logger for printing the results,
#and use debug level by default, which shows up as green
Expand Down
7 changes: 3 additions & 4 deletions xpra/codecs/nvidia/cuda/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from threading import RLock
from typing import Any

from xpra.codecs.nvidia.util import numpy_import_lock
from xpra.codecs.constants import TransientCodecException
from xpra.util.types import typedict
from xpra.util.str_fn import csv, print_nested_dict
Expand All @@ -22,13 +21,13 @@
get_default_conf_dirs, get_system_conf_dirs, get_user_conf_dirs,
get_resources_dir, get_app_dir,
)
from xpra.os_util import load_binary_file, is_WSL, WIN32, first_time
from xpra.os_util import load_binary_file, is_WSL, WIN32, first_time, NumpyImportContext
from xpra.log import Logger

if WIN32 and not os.environ.get("CUDA_PATH") and getattr(sys, "frozen", None) in ("windows_exe", "console_exe", True):
os.environ["CUDA_PATH"] = get_app_dir()

with numpy_import_lock:
with NumpyImportContext:
if is_WSL() and not envbool("XPRA_PYCUDA_WSL", False):
raise ImportError("refusing to import pycuda on WSL, use XPRA_PYCUDA_WSL=1 to override")
import pycuda
Expand Down Expand Up @@ -494,7 +493,7 @@ def __enter__(self):
def make_context(self) -> None:
start = monotonic()
if self.opengl:
with numpy_import_lock:
with NumpyImportContext:
from pycuda import gl # @UnresolvedImport pylint: disable=import-outside-toplevel
self.context = gl.make_context(self.device)
else:
Expand Down
4 changes: 2 additions & 2 deletions xpra/codecs/nvidia/cuda/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

from time import monotonic

from xpra.codecs.nvidia.util import numpy_import_lock
from xpra.os_util import NumpyImportContext
from xpra.codecs.image import ImageWrapper
from xpra.log import Logger

log = Logger("cuda")

with numpy_import_lock:
with NumpyImportContext:
from numpy import byte # @UnresolvedImport
from pycuda.driver import (
pagelocked_empty,
Expand Down
4 changes: 0 additions & 4 deletions xpra/codecs/nvidia/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import sys
import os
from threading import RLock

from xpra.util.str_fn import csv, print_nested_dict, pver
from xpra.util.env import envbool
Expand All @@ -18,9 +17,6 @@

MIN_VERSION = 466


numpy_import_lock = RLock()

NVIDIA_PROC_FILE = "/proc/driver/nvidia/version"
NVIDIA_HARDWARE = envbool("XPRA_NVIDIA_HARDWARE", False)

Expand Down
20 changes: 19 additions & 1 deletion xpra/os_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from subprocess import PIPE, Popen
from typing import Any
from collections.abc import Callable
from threading import Thread
from threading import Thread, RLock

from xpra.common import noerr

Expand Down Expand Up @@ -1110,3 +1110,21 @@ def ignorewarnings(fn, *args) -> Any:
return fn(*args)
finally:
warnings.filterwarnings("default")


numpy_import_lock = RLock()


class NumpyImportContext(AbstractContextManager):

def __enter__(self):
if not numpy_import_lock.acquire(blocking=False):
raise RuntimeError("the numpy import lock is already held!")
os.environ["XPRA_NUMPY_IMPORT"] = "1"

def __exit__(self, exc_type, exc_val, exc_tb):
os.environ.pop("XPRA_NUMPY_IMPORT", None)
numpy_import_lock.release()

def __repr__(self):
return "numpy_import_context"

0 comments on commit ee37522

Please sign in to comment.