Skip to content

Regression (0.31.1→0.31.2): stream created in main thread is unusable from a worker thread — There is no Stream(gpu, 0) in current thread (breaks mlx_lm threaded server) #3727

Closed

Description

@Ogilthorp3

Summary

A stream created in the main thread via mx.new_stream(mx.default_device()) can no longer be used (mx.eval / mx.synchronize under with mx.stream(...)) from a different thread as of mlx 0.31.2. It worked in 0.31.1. This breaks mlx_lm's server, whose BatchGenerator runs generation in a worker thread — every chat/completions request raises:

RuntimeError: There is no Stream(gpu, 0) in current thread.
  File ".../mlx_lm/generate.py", line 1161, in prompt
    mx.eval([c.state for c in self.prompt_cache])
  ...
  File ".../mlx_lm/generate.py", line 1549, in close
    mx.synchronize(generation_stream)

(generation_stream = mx.new_stream(mx.default_device()) is created at import time in the main thread, mlx_lm/generate.py:226.)

Bisect (same venv, Python 3.12, mlx_lm 0.31.2 fixed — only mlx changed)

mlx result
0.31.1 ✅ works
0.31.2 ❌ regression (no-Stream)
0.32.0.dev20260427 ❌ regression (no-Stream)

Minimal reproducer

python3 -m venv /tmp/v && /tmp/v/bin/pip install "mlx==0.31.2" "mlx-lm==0.31.2"
/tmp/v/bin/python -m mlx_lm server \
  --model mlx-community/Qwen2.5-Coder-0.5B-Instruct-4bit --port 6672 &
curl -s localhost:6672/v1/chat/completions -H 'Content-Type: application/json' \
  -d '{"messages":[{"role":"user","content":"hi"}],"max_tokens":6}'
# -> server thread crashes: "There is no Stream(gpu, 0) in current thread"
# Downgrade to fix:
/tmp/v/bin/pip install "mlx==0.31.1"   # same curl now returns a completion

Environment

  • macOS, Apple Silicon (M4 Max)
  • Python 3.12
  • mlx 0.31.2 (broken) / 0.31.1 (works); mlx_lm 0.31.2 in both

Likely cause

A change in 0.31.2 to GPU Stream thread-affinity / thread-local default-stream registration: a Stream object created in one thread is no longer resolvable when ops referencing it execute in another thread. Either restore cross-thread stream usability (revert behavior) or, if intentional, document it so consumers (mlx_lm) register the stream per worker thread.

Workaround for downstreams

Pin mlx==0.31.1, or run generation in the thread that created the stream.



Found while serving a Qwen3.6-35B-A3B model via mlx_lm.server. Repro + bisect done in a clean throwaway venv.

Activity

  1. zcbenz commented on Jun 20, 2026

    @zcbenz
    Member

    This had been fixed by ml-explore/mlx-lm#1090.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions