Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: stop the backend crashing on startup when memory is tight (#12374)

Nilay1c4b92c81425
diff --git a/studio/backend/tests/test_cpu_threads.py b/studio/backend/tests/test_cpu_threads.py
index eb3c021ad51..78cf126bf5b 100644
--- a/studio/backend/tests/test_cpu_threads.py
+++ b/studio/backend/tests/test_cpu_threads.py
@@ -50,16 +50,31 @@ def test_cpu_thread_cap_normalises_valid_inputs(raw):
assert env["OMP_NUM_THREADS"] == str(int(raw.strip()))
-# Unset / empty / whitespace -> no env mutation (pure opt-in).
@pytest.mark.parametrize("raw", [None, "", " ", "\t"])
-def test_cpu_thread_cap_is_opt_in(raw):
+def test_cpu_thread_cap_unset_limits_only_openblas(raw):
env = {} if raw is None else {"UNSLOTH_CPU_THREADS": raw}
snapshot = dict(env)
configure_cpu_threads(env)
- assert env == snapshot
- assert all(variable not in env for variable in _THREAD_POOL_ENV_VARS)
+ assert env == {**snapshot, "OPENBLAS_NUM_THREADS": "1"}
+
+
+def test_openblas_default_keeps_user_value():
+ env = {"OPENBLAS_NUM_THREADS": "8"}
+
+ configure_cpu_threads(env)
+
+ assert env == {"OPENBLAS_NUM_THREADS": "8"}
+
+
[email protected]("raw", ["", " "])
+def test_openblas_default_replaces_blank_value(raw):
+ env = {"OPENBLAS_NUM_THREADS": raw}
+
+ configure_cpu_threads(env)
+
+ assert env == {"OPENBLAS_NUM_THREADS": "1"}
# Anything that is not a positive integer raises a clear ValueError.
diff --git a/studio/backend/utils/cpu_threads.py b/studio/backend/utils/cpu_threads.py
index 91d577408da..c1998dbce4f 100644
--- a/studio/backend/utils/cpu_threads.py
+++ b/studio/backend/utils/cpu_threads.py
@@ -16,7 +16,7 @@ _THREAD_POOL_ENV_VARS = (
def configure_cpu_threads(env: Optional[MutableMapping[str, str]] = None) -> None:
- """Apply ``UNSLOTH_CPU_THREADS`` to native CPU pools when configured.
+ """Apply ``UNSLOTH_CPU_THREADS`` to native CPU pools when configured, else cap OpenBLAS at one thread.
Must run before importing libraries that initialize an OpenMP or BLAS
pool. Library-specific vars are left untouched so users can override a
@@ -25,6 +25,9 @@ def configure_cpu_threads(env: Optional[MutableMapping[str, str]] = None) -> Non
environ = os.environ if env is None else env
configured = environ.get("UNSLOTH_CPU_THREADS", "").strip()
if not configured:
+ # OpenBLAS reads a blank value as 0 (one thread per core), so blank counts as unset.
+ if not environ.get("OPENBLAS_NUM_THREADS", "").strip():
+ environ["OPENBLAS_NUM_THREADS"] = "1"
return
try: