Repository navigation
fix: wrap decode loops in generation_stream context for thread safety - #1775
Merged
zcbenz merged 3 commits intoAug 26, 2026
Merged
Conversation
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
zcbenz
approved these changes
Aug 26, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The decode loops in
generate_stepandspeculative_generate_steprunmx.async_evaloutside thewith 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: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_streamis created withmx.new_thread_local_stream()at module import time (line 217). The_stepfunction correctly useswith mx.stream(generation_stream):, but the surrounding decode loop — which callsmx.async_eval(y, logprobs),mx.eval(y), andmx.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 rebindinggeneration_streamand callingmx.set_default_stream(), butmx.async_evalstill crashes because the thread-local stream created bymx.new_thread_local_streamat 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_stepandstream_generateon a worker thread viaconcurrent.futures.ThreadPoolExecutor, rebinding the generation stream the same way a real server would. Without this fix they crash; with it they complete normally.Related