Repository navigation
Description
Summary
When MLX's C++ side throws (for example the graph-cache "Cache thrashing" runtime_error in #818, but also allocation failures and other internal errors), the exception propagates through an infallible cxx boundary and the whole mlxcel-server process dies with terminate called after throwing an instance of 'std::runtime_error'. Every in-flight request is dropped, with no request-level error and no operator-actionable log beyond the raw C++ what(). #818 removed the one specific trigger that we hit in practice (the graph-cache default), but the crash class remains: any MLX throw reached from the batch decode loop still aborts the process. This issue is the defense-in-depth follow-up (#818 proposal 2): route the decode-loop evaluation through the fallible boundary so an MLX failure fails the affected request/batch instead of the process.
Root cause
The batch scheduler decode path evaluates on-device tensors through the infallible mlxcel_core::eval / mlxcel_core::async_eval wrappers, declared in src/lib/mlxcel-core/src/lib.rs as plain cxx functions (fn eval(arr: &MlxArray); at line 202, fn async_eval(arr: &MlxArray); at 1782). cxx generates a noexcept shim for a function that does not return Result, so a C++ exception crossing that boundary calls std::terminate. The scheduler uses the infallible eval at 10 sites in src/server/batch/scheduler.rs (4777, 4923, 5067, 5083, 5254, 5327, 6059, 6238, 6577) plus async_eval at 5959.
The fallible boundary already exists and is already used elsewhere: fn try_eval(arr: &MlxArray) -> Result<()>; (lib.rs:212) makes cxx catch the C++ exception and return it as a Rust Err. The audio and whisper workers deliberately go through it precisely so an MLX failure surfaces as an error rather than a process abort (src/server/audio_worker.rs:399,493, src/models/whisper/mod.rs:149, src/models/whisper/decoding.rs:88). The batch decode loop, which is the highest-throughput and longest-lived MLX consumer, is the one place that does not.
Proposed change
- Add a fallible
try_async_eval(arr: &MlxArray) -> Result<()>(and, if the lookahead pipeline needs it,try_async_eval_pair) to the cxx bridge, mirroringtry_eval.async_evalhas no fallible variant today. - Route the scheduler decode-loop evaluations (the 10
evalsites and theasync_evalsite above) through the fallible variants. OnErr, translate it into a request-level failure for the affected sequence(s)/batch: return a 500internal_errorto those requests with the MLXwhat()string in the operator log, and continue serving other sequences. This mirrors the audio/whisper pattern already in the tree. - Add a bounded-failure policy so a genuinely unrecoverable backend does not spin: after N consecutive decode-eval failures (or a failure whose message indicates allocator/device corruption), drain in-flight requests with an error and shut the worker down cleanly rather than looping.
Risks and scope notes
- Not every MLX throw is safely recoverable. The graph-cache thrashing throw happens at graph capture, before any state mutation, so failing just that batch is clean. But an out-of-memory or an internal-invariant throw may leave the allocator or device in an undefined state; for those, continuing to serve other requests is unsafe. The bounded-failure policy (point 3) is why this is not simply "catch and keep going": the goal is a graceful, logged, request-scoped failure or clean shutdown, not pretend-recovery.
- Performance:
try_evalreturnsResult<()>but is otherwise the same MLX call; the added cost is a branch on the hot path, negligible against a decode step. Confirm no measurable decode-throughput regression on a GB10 sweep before/after. - This is defense in depth, not a fix for a currently-reproducing crash (that was fix(server): speculative serving aborts the whole process via MLX CUDA graph-cache "Cache thrashing" throw (lifetime miss counter vs default capacity 400) #818). Priority is medium: it hardens the process against the next MLX throw we have not yet hit.
Refs
Follow-up to #818 (proposal 2). Sibling follow-ups: the upstream MLX lifetime-miss report (proposal 3) and the paged-state sync warning spam.
Activity
fix(server): unwind speculative KV append on lookahead eval throw
docs: correct MLX FFI-exception notes for the #822 eval guard
fix(server): survive MLX decode-eval throws instead of aborting the w…
refactor(engine): sample and finish decode rows inside Engine::step
fix(engine): keep the per-row chain for a batch of one
refactor: batch-native engine step API with BatchScheduler on top (#2217
fix: guard the per-row pipeline's host read behind try_eval
update: pipeline penalty and DRY requests in the engine's B=1 client (#…
fix(server): survive MLX decode-eval throws instead of aborting the w…
fix(speculative): keep a full-model drafter failure inside its row
fix(speculative): keep a full-model drafter failure inside its row