Skip to content

fix(core): initialize the lazy custom-kernel holders once across threads - #3089

Merged
inureyes merged 1 commit into
mainfrom
fix/thread-safe-kernel-holders
Nov 15, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/thread-safe-kernel-holders

Conversation

@inureyes

Copy link
Copy Markdown
Member

The 22 custom-kernel holders in src/lib/mlxcel-core/cpp/mlx_cxx_kernels.cpp built their CustomKernelFunction on first use behind a plain if (!initialized) flag, so two threads entering the same holder raced. In parallel cargo test -p mlxcel --lib granitemoehybrid runs:

  • Exceptions: the fused MoE launch threw std::bad_function_call or __next_prime overflow, which switch_layers::forward_fused_kernel turns into a declined fused path. The tests then failed at "must dispatch the fused kernel".
  • Crashes: the process sometimes died with SIGSEGV.

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_flag and std::call_once, the same pattern the holders above them in the file already use.

Validation (M5 Max, metal,accelerate)

  • Granite module, parallel: 12 of 12 runs pass (was 3 of 10 failing on main 05312c6).
  • mlxcel-core lib suite, parallel: 3 of 3 runs pass (2292 tests).
  • The switch_layers, fused_moe, granitemoehybrid and gemma3n tests pass (124).
  • Clippy for mlxcel-core lib and tests passes.

@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers labels Nov 15, 2026
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
inureyes force-pushed the fix/thread-safe-kernel-holders branch from d79ba0e to 050b548 Compare November 15, 2026 17:37
@inureyes
inureyes merged commit ffa92e0 into main Nov 15, 2026
31 checks passed
@inureyes
inureyes deleted the fix/thread-safe-kernel-holders branch November 15, 2026 17:38
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.
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers priority:high High priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant