#1119·voicebox

Refinement (Qwen3 MLX) crashes intermittently: RuntimeError: There is no Stream(gpu, 1) in current thread

Author: dxnielhrfCreated Sep 25, 2026Updated Sep 25, 2026

Bug: Refinement crashes intermittently with RuntimeError: There is no Stream(gpu, 1) in current thread.

Version: 0.5.0 (macOS) Component: backend/backends/qwen_llm_backend.py — refinement step (Qwen3 0.6B via MLX)

Description

Whisper transcription itself works reliably, but the automatic refinement step (Smart Cleanup / Self-Correction, powered by the local Qwen3 0.6B MLX model) fails intermittently with:

RuntimeError: There is no Stream(gpu, 1) in current thread.

Full traceback:

Server: 2026-09-25 10:09:56,701 - backend.routes.captures - ERROR - Refinement failed for capture e718e3de-aa22-4cd2-8ea1-01191222cae3
Traceback (most recent call last):
  File "mlx_lm/generate.py", line 255, in wired_limit
    yield
  File "mlx_lm/generate.py", line 705, in stream_generate
    for n, (token, logprobs, from_draft) in enumerate(token_generator):
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "mlx_lm/generate.py", line 695, in <genexpr>
    (token, logprobs, False) for token, logprobs in token_generator
                                                    ^^^^^^^^^^^^^^^
  File "mlx_lm/generate.py", line 438, in generate_step
    mx.eval([c.state for c in prompt_cache])
RuntimeError: There is no Stream(gpu, 1) in current thread.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "backend/routes/captures.py", line 142, in refine_capture_endpoint
  File "backend/services/captures.py", line 194, in refine_capture
  File "backend/services/refinement.py", line 287, in refine_transcript
  File "backend/backends/qwen_llm_backend.py", line 258, in generate
  File "asyncio/threads.py", line 25, in to_thread
  File "concurrent/futures/thread.py", line 59, in run
  File "backend/backends/qwen_llm_backend.py", line 282, in _generate_sync
  File "mlx_lm/generate.py", line 768, in generate
    for response in stream_generate(model, tokenizer, prompt, **kwargs):
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "mlx_lm/generate.py", line 703, in stream_generate
    with wired_limit(model, [generation_stream]):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "contextlib.py", line 158, in __exit__
  File "mlx_lm/generate.py", line 259, in wired_limit
    mx.synchronize(s)
RuntimeError: There is no Stream(gpu, 1) in current thread.

Server responds 500 Internal Server Error on POST /captures/{id}/refine. The raw Whisper transcript is still saved (so dictation itself isn't lost), but the refined/cleaned-up transcript never gets produced.

Suspected root cause

MLX binds its GPU stream to the OS thread it was first used on. qwen_llm_backend.py's generate() dispatches the actual generation call via asyncio.to_thread(self._generate_sync, ...). Python's default ThreadPoolExecutor (used by asyncio.to_thread) can spin up new worker threads on demand, and a freshly-spawned thread has no MLX GPU stream registered — hence mx.eval(...) / mx.synchronize(...) inside mlx_lm's generate_step/wired_limit fail with "There is no Stream(gpu, 1) in current thread."

This matches why the failure is intermittent: if asyncio.to_thread happens to reuse a thread that already ran an MLX call, it works; if the executor spawns a new thread, it crashes.

Suggested fix

Run MLX inference (both Whisper and Qwen) on a single, dedicated, persistent worker thread (e.g. a ThreadPoolExecutor(max_workers=1) created once at startup and reused for every MLX call) instead of relying on asyncio.to_thread's default executor, which can allocate arbitrary/ephemeral threads. Alternatively, explicitly initialize/attach the MLX GPU stream (mx.synchronize() / mx.set_default_device(mx.gpu)) at the start of every thread that will run MLX ops.

Repro

  1. Fresh install, Whisper Turbo + Qwen3 0.6B models, macOS Apple Silicon.
  2. Enable Smart Cleanup / Self-Correction (auto-refine) in settings.
  3. Dictate several times in a row (a handful of captures). Refinement intermittently fails with the above error — no fixed pattern, seems tied to thread pool scheduling.

Workaround

Disabling "Automatische Verfeinerung" / Smart Cleanup / Self-Correction in settings avoids the crash entirely, since it skips the Qwen refinement call — raw Whisper transcription is unaffected and reliable.