Skip to content

fix: wrap decode loops in generation_stream context for thread safety - #1775

Merged
zcbenz merged 3 commits into
ml-explore:mainfrom
aberhamm:fix/generate-step-stream-context
Aug 26, 2026
Merged

zcbenz merged 3 commits into
ml-explore:mainfrom
aberhamm:fix/generate-step-stream-context

Conversation

@aberhamm

Copy link
Copy Markdown
Contributor

Summary

The decode loops in generate_step and speculative_generate_step run mx.async_eval outside the with mx.stream(generation_stream): context manager. On the main thread this works because the default stream happens to be valid, but on worker threads (as used by inference servers like vllm-mlx) the thread-local default stream does not exist, causing:

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

This makes tool/function calling impossible on any server that dispatches generation to worker threads, since the request crashes as soon as decoding begins. Non-tool chat works because it takes a different code path.

Root cause

generation_stream is created with mx.new_thread_local_stream() at module import time (line 217). The _step function correctly uses with mx.stream(generation_stream):, but the surrounding decode loop — which calls mx.async_eval(y, logprobs), mx.eval(y), and mx.clear_cache() — runs outside that context. These calls fall back to the implicit default stream, which is stale/absent on worker threads.

vllm-mlx's bind_generation_streams() (PR #411) attempted to fix this by rebinding generation_stream and calling mx.set_default_stream(), but mx.async_eval still crashes because the thread-local stream created by mx.new_thread_local_stream at import time doesn't exist in the worker thread.

Fix

Wrap both decode loops in with mx.stream(generation_stream): so all MLX operations use the explicitly rebound stream:

  • generate_step: lines 446–461 (the token-by-token decode loop)
  • speculative_generate_step: lines 598–645 (the speculative decode loop)

Tests

Two new tests run generate_step and stream_generate on a worker thread via concurrent.futures.ThreadPoolExecutor, rebinding the generation stream the same way a real server would. Without this fix they crash; with it they complete normally.

Related

  • vllm-project/vllm-mlx#407 — original report of the stream crash
  • vllm-project/vllm-mlx#411 — partial fix (stream rebinding) that doesn't cover the decode loop

The decode loops in `generate_step` and `speculative_generate_step` run
`mx.async_eval` outside the `with mx.stream(generation_stream):` context
manager. On the main thread this works because the default stream happens
to be valid, but on worker threads (as used by inference servers like
vllm-mlx) the thread-local default stream does not exist, causing:

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

This makes tool/function calling impossible on any server that runs
generation on worker threads, since the request crashes as soon as
decoding begins.

The fix wraps both decode loops in `with mx.stream(generation_stream):`
so that `mx.async_eval`, `mx.eval`, and `mx.clear_cache` all execute on
the explicitly rebound generation stream rather than the implicit
thread-local default.

Adds two tests that run `generate_step` and `stream_generate` on a
worker thread to verify the fix.

Related: vllm-project/vllm-mlx#407, vllm-project/vllm-mlx#411
@nastya236 nastya236 added the bug label Aug 24, 2026
@zcbenz
zcbenz merged commit 74e7cf9 into ml-explore:main Aug 26, 2026
2 checks passed
azamamirza added a commit to azamamirza/mlx-lm that referenced this pull request Aug 27, 2026
Rebased onto upstream/main 74e7cf9 (adds ml-explore#1673 ChunkedKVCache valid-length
trim — which supersedes and retires our own fix — plus ml-explore#1772 GenerationBatch
filter, ml-explore#1775 decode-loop thread-safety, and new model support). Our two
qwen3_moe patches (tensor-parallel ml-explore#1774 submitted, pipeline staged) ride on
top unchanged and rebased clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants