Skip to content

Server crashes with 'There is no Stream(gpu, 0) in current thread' on mlx 0.31.2 #1181

Closed

Description

@ashhart

Summary

mlx_lm.server crashes on the first request with RuntimeError: There is no Stream(gpu, 0) in current thread when running with mlx 0.31.2.

mlx 0.31.2 made streams thread-local (ml-explore/mlx#3281, ml-explore/mlx#3405). The module-level generation_stream in generate.py is created at import time on the main thread, but BatchGenerator.next() uses it from the server's worker thread (Thread-2 (_generate)). Streams created on one thread can no longer be used from another.

Environment

  • mlx: 0.31.2
  • mlx-lm: 0.31.2
  • Python: 3.13
  • macOS: Darwin 25.4.0 (Apple Silicon)

Reproduction

uvx --from mlx-lm mlx_lm.server \
    --model mlx-community/Qwen3.6-35B-A3B-8bit \
    --port 8005

Send any chat completion request. The server returns 200 but the generation thread crashes:

Exception in thread Thread-2 (_generate):
  File "mlx_lm/server.py", line 825, in _generate
    prompt_responses, gen_responses = batch_generator.next()
  File "mlx_lm/generate.py", line 1847, in next
    return self._next()
  File "mlx_lm/generate.py", line 1833, in _next
    self._prompt_batch.prompt(prompts)
  File "mlx_lm/generate.py", line 1161, in prompt
    mx.eval([c.state for c in self.prompt_cache])
RuntimeError: There is no Stream(gpu, 0) in current thread.

Root cause

generate.py:226 creates a module-level stream on the main thread:

generation_stream = mx.new_stream(mx.default_device())

BatchGenerator.next() uses it from the server's worker thread:

def next(self):
    with mx.stream(generation_stream):  # stream from wrong thread
        return self._next()

Fix

Have BatchGenerator create its own stream on the thread where it's instantiated. PR incoming.

Activity

  1. angeloskath commented on Apr 22, 2026

    @angeloskath
    Member

    Closing like the other 2 issues.

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