Repository navigation
fix(core): initialize the lazy custom-kernel holders once across threads - #3089
Merged
Merged
Conversation
The 22 kernel holders in mlx_cxx_kernels.cpp built their CustomKernelFunction on first use behind a plain `if (!initialized)` flag. Two threads entering the same holder raced: one read an empty std::optional<std::function>, and concurrent assignment corrupted the function object. In parallel `cargo test -p mlxcel --lib granitemoehybrid` runs, the fused MoE launch threw std::bad_function_call or "__next_prime overflow", which switch_layers turned into a declined fused path, and the process sometimes died with SIGSEGV. On an M5 Max, 7 of 10 runs failed at 712f392. The race predates #3069, but #3069 changed thread timing enough to expose it (0 of 10 runs failed at bb76daa). Each holder now initializes through a std::once_flag and std::call_once, the same pattern the holders above them already use. With the change the granite module passed 12 of 12 parallel runs, the mlxcel-core lib suite passed 3 of 3 parallel runs, and the switch_layers, fused_moe, granitemoehybrid and gemma3n tests pass.
inureyes
force-pushed
the
fix/thread-safe-kernel-holders
branch
from
November 15, 2026 17:37
d79ba0e to
050b548
Compare
inureyes
added a commit
that referenced
this pull request
Nov 15, 2026
) These four PRs merged without their technical reports (the repository keeps reports via `TECHNICAL_REPORTS/.keep-reports`). This adds the English and Korean reports for each: - #3069: per-thread default device - #3082: Windows bench harness test - #3086: create_causal_mask roster count - #3089: thread-safe kernel holders Docs only.
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.
The 22 custom-kernel holders in
src/lib/mlxcel-core/cpp/mlx_cxx_kernels.cppbuilt theirCustomKernelFunctionon first use behind a plainif (!initialized)flag, so two threads entering the same holder raced. In parallelcargo test -p mlxcel --lib granitemoehybridruns:std::bad_function_callor__next_prime overflow, whichswitch_layers::forward_fused_kernelturns into a declined fused path. The tests then failed at "must dispatch the fused kernel".On an M5 Max, 7 of 10 runs failed at 712f392 (#3069). The race predates #3069, which changed thread timing enough to expose it: 0 of 10 runs failed at bb76daa. A server that dispatches the same kernel from two threads on first use would hit it too.
Each holder now initializes through a
std::once_flagandstd::call_once, the same pattern the holders above them in the file already use.Validation (M5 Max, metal,accelerate)
mlxcel-corelib suite, parallel: 3 of 3 runs pass (2292 tests).switch_layers,fused_moe,granitemoehybridandgemma3ntests pass (124).mlxcel-corelib and tests passes.