From a291d1fe0ba6100f717441f346a6df77df7e8fdd Mon Sep 17 00:00:00 2001 From: Siddharth Kumar Sah Date: Sun, 5 Apr 2026 22:03:57 +0800 Subject: [PATCH] fix: prevent false GPU detection when CUDA image runs without GPU The STIRLING_GPU=true env var was baked into the :cuda Dockerfile, which made gpu_available() return True without checking actual hardware. On machines without a GPU, this would crash upscale.py (torch.device("cuda") fails) and ocr.py (PaddleOCR use_gpu=True). Fix: the env var can only disable GPU (set to false/0), never force-enable it. Hardware detection always runs. Removed the baked env var from the Dockerfile since it adds no value now. --- docker/Dockerfile | 3 +-- packages/ai/python/gpu.py | 9 +++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 3125602f..84f5c91e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -190,8 +190,7 @@ ENV PORT=1349 \ CONCURRENT_JOBS=3 \ MAX_MEGAPIXELS=100 \ RATE_LIMIT_PER_MIN=100 \ - STIRLING_VARIANT=${VARIANT} \ - STIRLING_GPU=${GPU} + STIRLING_VARIANT=${VARIANT} # Create non-root user for runtime RUN groupadd -r stirling && useradd -r -g stirling -d /app -s /sbin/nologin stirling diff --git a/packages/ai/python/gpu.py b/packages/ai/python/gpu.py index 60031e88..a9dc426b 100644 --- a/packages/ai/python/gpu.py +++ b/packages/ai/python/gpu.py @@ -6,10 +6,15 @@ import os @functools.lru_cache(maxsize=1) def gpu_available(): """Return True if a usable CUDA GPU is present at runtime.""" + # Allow explicit disable via env var (set to "false" or "0") override = os.environ.get("STIRLING_GPU") - if override is not None: - return override.lower() in ("1", "true", "yes") + if override is not None and override.lower() in ("0", "false", "no"): + return False + # Always check actual hardware, even if STIRLING_GPU=true. + # The env var can disable GPU but never force-enable it, + # because the :cuda image bakes STIRLING_GPU=true and we + # still need to handle "no GPU attached" gracefully. try: import onnxruntime if "CUDAExecutionProvider" in onnxruntime.get_available_providers():