Fix SIGABRT/SIGSEGV from concurrent mx::eval dispatch - #28
Merged
Conversation
MLX is not thread-safe (ml-explore/mlx#2133). The Metal CommandEncoder is shared state — concurrent mx::eval calls from different OS threads crash with "A command encoder is already encoding to this command buffer" (SIGABRT) or SIGSEGV. The M14 soak tests were the first to exercise concurrent eval from multiple dirty-CPU scheduler threads. Fix: - Add emily::safe_eval() with a static mutex serialising all mx::eval calls (c_src/emily/tensor.hpp) - Remove set_default_stream calls from with_stream/2 — the NIF mutated MLX thread-local state which is unreliable under BEAM process migration between OS threads - Harden resolve_stream(-1) to avoid reading thread-local default Also adds M15.5 milestone for building MLX from source once upstream gains native thread-safety (0.32+).
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
CommandEncoderis shared state — concurrentmx::evalcalls from different OS threads crash with"A command encoder is already encoding to this command buffer"(SIGABRT) or SIGSEGV. M14's soak tests (8 concurrentTask.async_streamworkers) were the first to callmx::evalfrom multiple dirty-CPU scheduler threads simultaneously.emily::safe_eval(): mutex-serialisedmx::evalwrapper intensor.hpp. All eval callsites route through it; graph-building ops remain lock-free.set_default_streamfromwith_stream/2: the NIF mutated MLX thread-local state which is unreliable under BEAM process migration between OS threads. Process-dictionary routing was already correct.resolve_stream(-1): avoids reading the (potentially corrupted) thread-local default.ThreadLocalStream(ml-explore/mlx#3405).Test plan
CommandEncoderassertion🤖 Generated with Claude Code