You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you remove the computation from the loop and just have:
s = mx.new_stream(mx.gpu)
You get:
1609
1610
Traceback (most recent call last):
File "/tmp/t.py", line 7, in <module>
s = mx.new_stream(mx.gpu)
RuntimeError: [metal::Device] Failed to make new command queue.
I wonder if there is a limit to how many command queues you can make 🤔
I think you're right :-) in #2846 we found a limit on the number of residency sets that can be added to a single encoder. There might be a limit on the number of command queues, and perhaps new_stream could use a RAII wrapper in the C/C++ side, and with new_stream(...) on the python side so the lifetime of the stream can be scoped?
Hi, I’d like to work on this issue. Before implementing anything, I traced the current stream lifetime and would like to confirm the intended design.
My current understanding is:
all_streams() in mlx/stream.cpp is append-only.
gpu::new_stream() creates a per-stream CommandEncoder; on Metal its constructor allocates an MTL::CommandQueue.
The scheduler also creates a StreamThread per stream index and currently never removes it.
Stream is a small value type that is copied into primitives/graphs, so destroying the Python Stream object alone is not sufficient evidence that the underlying stream is unused.
clear_streams() clears the current thread’s backend encoder map, but it does not remove entries from all_streams() or the scheduler.
I’m considering an RAII-style shared lifetime token (or equivalent registry lease) carried by every copied Stream. The last live owner would request deferred cleanup rather than directly destroying resources:
Keep the stream alive while arrays/primitives or queued work still reference it.
After outstanding scheduler tasks and GPU command buffers complete, remove the backend encoder/command queue and scheduler worker.
Route cleanup through the stream’s owning thread, since the regular encoder maps are thread-local.
Avoid reusing a stream index until teardown is complete.
Preserve the required lifetime of default, thread-local, and thread-unsafe streams.
I would add regression coverage for:
repeatedly creating, evaluating on, and dropping more streams than Metal’s command-queue limit;
dropping the original handle while a copied handle or graph still uses the stream;
releasing a stream with pending asynchronous work;
ensuring the stream/encoder/scheduler registries remain bounded;
CPU behavior as well as Metal, with CUDA coverage where applicable.
Would changing Stream to carry shared lifetime state be acceptable, including the resulting ABI considerations? Or would you prefer a registry/pooling design or an explicit close/release API? I’d like to align on the ownership model before preparing the patch.
Activity
awni commentedon Apr 24, 2025
That's interesting. It seems like an issue on the Metal side.. I wonder if there is a limit to how many command queues you can make 🤔
davidkoski commentedon Apr 24, 2025
If you remove the computation from the loop and just have:
You get:
so your idea seems plausible.
awni commentedon Apr 25, 2025
I'm going to leave this open for now. There is definitely a couple metal issues at play here:
Context leak detected, msgtracer returned -1is a metal or lower-level issueHowever, the internal error is a bit weird... that one might be something going wrong in MLX and merits some further investigation.
robertmsale commentedon Feb 13, 2026
I think you're right :-) in #2846 we found a limit on the number of residency sets that can be added to a single encoder. There might be a limit on the number of command queues, and perhaps new_stream could use a RAII wrapper in the C/C++ side, and
with new_stream(...)on the python side so the lifetime of the stream can be scoped?Daijunfan commentedon Jul 25, 2026
Hi, I’d like to work on this issue. Before implementing anything, I traced the current stream lifetime and would like to confirm the intended design.
My current understanding is:
all_streams()inmlx/stream.cppis append-only.gpu::new_stream()creates a per-streamCommandEncoder; on Metal its constructor allocates anMTL::CommandQueue.StreamThreadper stream index and currently never removes it.Streamis a small value type that is copied into primitives/graphs, so destroying the PythonStreamobject alone is not sufficient evidence that the underlying stream is unused.clear_streams()clears the current thread’s backend encoder map, but it does not remove entries fromall_streams()or the scheduler.I’m considering an RAII-style shared lifetime token (or equivalent registry lease) carried by every copied
Stream. The last live owner would request deferred cleanup rather than directly destroying resources:I would add regression coverage for:
Would changing
Streamto carry shared lifetime state be acceptable, including the resulting ABI considerations? Or would you prefer a registry/pooling design or an explicit close/release API? I’d like to align on the ownership model before preparing the patch.Drop the mlx patch and the local stream shim
Drop the mlx patch and the local stream shim
pool Streams, fix Device inheritance
pool Streams, fix Device inheritance (#472)
Sync upstream and integrate signed Hadamard layers (#8)