Skip to content

Commit ba2acd7

Browse files
Merge pull request #980 from yasinBursali/fix/bsd-sed-and-models-exception
fix: portable ANSI stripping and narrowed GPU exception
2 parents 51ed7b7 + 7b533ee commit ba2acd7

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

dream-server/dream-preflight.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ WARN=0
8080

8181
log() {
8282
echo -e "$1"
83-
echo -e "$1" | sed 's/\x1b\[[0-9;]*m//g' >> "$LOG_FILE"
83+
echo -e "$1" | sed $'s/\033\\[[0-9;]*m//g' >> "$LOG_FILE"
8484
}
8585
pass() { log "${GREEN}${NC} $1"; PASS=$((PASS+1)); }
8686
fail() { log "${RED}${NC} $1"; FAIL=$((FAIL+1)); }

dream-server/extensions/services/dashboard-api/routers/models.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
_LIBRARY_PATH = Path(INSTALL_DIR) / "config" / "model-library.json"
2121
_MODELS_DIR = Path(DATA_DIR) / "models"
2222
_ENV_PATH = Path(INSTALL_DIR) / ".env"
23+
_GPU_VRAM_EXCEPTIONS = (
24+
ImportError,
25+
FileNotFoundError,
26+
OSError,
27+
KeyError,
28+
AttributeError,
29+
)
30+
31+
try:
32+
import pynvml
33+
except ImportError:
34+
pynvml = None
35+
else:
36+
_GPU_VRAM_EXCEPTIONS = _GPU_VRAM_EXCEPTIONS + (pynvml.NVMLError,)
2337

2438

2539
def _load_library() -> list[dict]:
@@ -79,7 +93,8 @@ def _get_gpu_vram() -> Optional[ModelLibraryGpu]:
7993
vramUsed=round(used_gb, 1),
8094
vramFree=round(total_gb - used_gb, 1),
8195
)
82-
except Exception:
96+
except _GPU_VRAM_EXCEPTIONS as exc:
97+
logger.warning("GPU VRAM detection failed: %s", exc)
8398
return None
8499

85100

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Focused tests for the models router helpers."""
2+
3+
from __future__ import annotations
4+
5+
import importlib
6+
import sys
7+
import types
8+
9+
10+
def test_get_gpu_vram_returns_none_on_nvml_error(monkeypatch):
11+
"""Operational NVML failures should degrade to unknown GPU rather than 500."""
12+
13+
class FakeNVMLError(Exception):
14+
pass
15+
16+
def _raise_nvml_error():
17+
raise FakeNVMLError("driver not loaded")
18+
19+
real_gpu = sys.modules.get("gpu")
20+
real_pynvml = sys.modules.get("pynvml")
21+
22+
monkeypatch.setitem(sys.modules, "gpu", types.SimpleNamespace(get_gpu_info=_raise_nvml_error))
23+
monkeypatch.setitem(sys.modules, "pynvml", types.SimpleNamespace(NVMLError=FakeNVMLError))
24+
25+
import routers.models as models_router
26+
27+
importlib.reload(models_router)
28+
assert models_router._get_gpu_vram() is None
29+
30+
if real_gpu is None:
31+
monkeypatch.delitem(sys.modules, "gpu", raising=False)
32+
else:
33+
monkeypatch.setitem(sys.modules, "gpu", real_gpu)
34+
35+
if real_pynvml is None:
36+
monkeypatch.delitem(sys.modules, "pynvml", raising=False)
37+
else:
38+
monkeypatch.setitem(sys.modules, "pynvml", real_pynvml)
39+
40+
importlib.reload(models_router)

0 commit comments

Comments
 (0)