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.

Fix a 500 on dictation Unload before any backend is resident (#9013)

Daniel Hanb191e7c33d71
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 4989a2995aa..a338161499d 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -11447,7 +11447,12 @@ async def stt_unload(
# Every engine is attempted even if one raises, so failing to free one never
# skips the other (both can be resident after a switch).
_, unload_stt = _stt_lifecycle()
- failed: list[str] = await asyncio.to_thread(unload_stt, engines, model)
+ # expected_model by keyword: _stt_lifecycle returns the orchestrator's
+ # unload_stt_model when a backend is resident and stt_registry.unload when one
+ # is not, and only the former takes it positionally. Registry-side it sits
+ # behind a `*`, so passing it positionally raised TypeError, which is the
+ # state a fresh process is in before anything has loaded.
+ failed: list[str] = await asyncio.to_thread(unload_stt, engines, expected_model = model)
if failed:
raise HTTPException(
status_code = 500,
diff --git a/studio/backend/tests/test_stt_ggml_sidecar.py b/studio/backend/tests/test_stt_ggml_sidecar.py
index b37d58533b4..01ea4524310 100644
--- a/studio/backend/tests/test_stt_ggml_sidecar.py
+++ b/studio/backend/tests/test_stt_ggml_sidecar.py
@@ -1158,3 +1158,75 @@ def test_free_stt_frees_gguf_even_when_transformers_unload_raises(monkeypatch):
# The Transformers failure must not skip GGUF eviction.
assert ggml.unloaded is True
assert any("small" in entry for entry in freed)
+
+
+def test_unload_without_a_resident_backend_does_not_crash(monkeypatch):
+ """Unload before anything has loaded used to raise TypeError, so the route 500'd.
+
+ _stt_lifecycle() hands back the orchestrator's unload_stt_model when a backend is
+ resident and stt_registry.unload when one is not. Only the first takes
+ expected_model positionally; on the registry it is keyword-only, and the route
+ passed it positionally. A fresh process is in exactly that state, so the two
+ unload tests above only passed because an earlier test in the full suite had left
+ a backend resident. Pin the no-backend path directly so neither the signature nor
+ the call site can drift back.
+ """
+ import routes.inference as ri
+ from core.inference import orchestrator, stt_registry
+
+ seen: dict = {}
+
+ def _unload(
+ engines = None,
+ *,
+ wait = True,
+ expected_model = None,
+ ):
+ seen["engines"] = engines
+ seen["expected_model"] = expected_model
+ return []
+
+ monkeypatch.setattr(orchestrator, "peek_inference_backend", lambda: None)
+ monkeypatch.setattr(stt_registry, "unload", _unload)
+
+ asyncio.run(ri.stt_unload(engine = None, model = "whisper-small", current_subject = "tester"))
+
+ assert seen["engines"] is None
+ assert seen["expected_model"] == "whisper-small"
+
+
+def test_both_unload_callables_accept_the_arguments_the_route_passes(monkeypatch):
+ """_stt_lifecycle returns two DIFFERENT callables, and the route has one call site.
+
+ That is the shape of the bug the test above covers: the orchestrator's
+ unload_stt_model takes expected_model positionally, the registry's unload keeps it
+ behind a `*`, and the route gets whichever branch is live. A call that suits one is a
+ TypeError on the other, and which one runs depends on whether a backend happens to be
+ resident, so the broken half only shows on a fresh process.
+
+ Binding both real signatures against the route's actual call keeps them compatible
+ without demanding they be identical. If either grows a parameter the other cannot
+ accept in the same position, this fails here rather than as a 500 in dictation.
+ """
+ import inspect
+ import routes.inference as ri
+ from core.inference import orchestrator, stt_registry
+ from core.inference.orchestrator import InferenceOrchestrator
+
+ class _Resident:
+ load_stt_model = InferenceOrchestrator.load_stt_model
+ unload_stt_model = InferenceOrchestrator.unload_stt_model
+
+ monkeypatch.setattr(orchestrator, "peek_inference_backend", lambda: None)
+ registry_unload = ri._stt_lifecycle()[1]
+ monkeypatch.setattr(orchestrator, "peek_inference_backend", lambda: _Resident())
+ orchestrator_unload = ri._stt_lifecycle()[1]
+
+ assert registry_unload is stt_registry.unload
+ assert (
+ registry_unload is not orchestrator_unload
+ ), "both branches resolved to the same callable, so this proves nothing"
+
+ for unload in (registry_unload, orchestrator_unload):
+ # Exactly how routes/inference.py::stt_unload calls it.
+ inspect.signature(unload).bind(["whisper"], expected_model = "whisper-small")