Skip to content

[BUG] crash while using new_stream #2118

Open

Description

@davidkoski

Describe the bug

crash while using new_stream -- I was trying to replicate something from the swift side and I can see the same error there.

To Reproduce

Include code snippet

import mlx.core as mx

i = 0
while True:
	print(i)

	s = mx.new_stream(mx.gpu)
	x = mx.random.uniform(shape=[10, 10]).sum(stream=s)
	_ = x.item()

	i += 1
...
607
608
609
libc++abi: terminating due to uncaught exception of type std::runtime_error: [METAL] Command buffer execution failed: Internal Error (0000010a:Internal Error)
[1]    10855 abort      python /tmp/t.py

Expected behavior

No crash

Desktop (please complete the following information):

  • OS Version: macOS 15.4
  • Version 0.25.0

Activity

  1. awni commented on Apr 24, 2025

    @awni
    Member

    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 🤔

  2. davidkoski commented on Apr 24, 2025

    @davidkoski
    MemberAuthor

    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.
    

    so your idea seems plausible.

  3. awni commented on Apr 25, 2025

    @awni
    Member

    I'm going to leave this open for now. There is definitely a couple metal issues at play here:

    • The strange log: Context leak detected, msgtracer returned -1 is a metal or lower-level issue
    • There is a system-wide limit on the number of command queues

    However, the internal error is a bit weird... that one might be something going wrong in MLX and merits some further investigation.

  4. robertmsale commented on Feb 13, 2026

    @robertmsale

    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?

  5. Daijunfan commented on Jul 25, 2026

    @Daijunfan

    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:

    1. Keep the stream alive while arrays/primitives or queued work still reference it.
    2. After outstanding scheduler tasks and GPU command buffers complete, remove the backend encoder/command queue and scheduler worker.
    3. Route cleanup through the stream’s owning thread, since the regular encoder maps are thread-local.
    4. Avoid reusing a stream index until teardown is complete.
    5. 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.

  6. added a commit that references this issue on Sep 6, 2026
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions