Repository navigation
fix(engine_pool): survive pending Metal command-buffer errors during unload (#3737, #4344) - #4377
Merged
Merged
Conversation
…unload (jundot#3737, jundot#4344) A prefill that OOMs on Metal leaves a pending command-buffer error; the first mx.synchronize() in _stop_and_unload_engine re-raises it, the exception escapes the unload (HTTP 500) and the memory accounting is never released, so every later load is refused with 507 until restart. Route every synchronize/clear_cache in the teardown through _metal_sync_clear_cache(), which catches only RuntimeError('Command buffer execution failed ...'), honours exit_if_gpu_submissions_ignored(), logs, and retries once (the raise consumes the pending error). Reproduced on an M5 Pro 48 GB: Metal OOM during prefill with another process holding GPU memory, then unload. Before: unload 500, current_model_memory stuck at the model size, next load 507. After: the retry path fires, unload 200, memory settled, accounting 0, next load 200. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
jundot
approved these changes
Oct 9, 2026
jundot
left a comment
Owner
There was a problem hiding this comment.
The retry is sound: MLX clears the encoder error when it throws, so the second synchronize goes through and the settle barrier and the accounting release still run. I'm not treating this as the fix for #3737 or #4344 though, since those show a settle timeout and a pool that never gets reclaimed rather than an unload that raises. LGTM.
jundot
added a commit
that referenced
this pull request
Oct 9, 2026
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
Fixes the "507 until restart" state reported in #3737 and #4344.
A prefill that hits a Metal OOM leaves a pending command-buffer error. When that model is later unloaded, the first
mx.synchronize()in_stop_and_unload_enginere-raises it (RuntimeError: [METAL] Command buffer execution failed: Insufficient Memory (00000008:kIOGPUCommandBufferCallbackErrorOutOfMemory)). The exception escapes the unload (the endpoint returns 500) before_current_model_memoryis decremented. Nothing is loaded, but admission still counts the old model's size, so every later load is refused with 507 until the server restarts.This PR routes the three
synchronize()+clear_cache()calls in the teardown (the initial clear, the settle-barrier rounds, and the emergency-reclaim rounds) through a small helper,_metal_sync_clear_cache(). The helper:RuntimeErrorwhose message containsCommand buffer execution failed; every other exception propagates unchanged;exit_if_gpu_submissions_ignored(e)first, so the existing fatal path forSubmissionsIgnoredis kept;There are no new settings, and behaviour on the normal path is unchanged.
Validation
Setup: MacBook Pro M5 Pro (48 GB), macOS 27.0, oMLX 0.7.0 (official wheel), MLX 0.32.3. Model
ornith-1.5-35b-4bit(Qwen3.5-35B-A3B architecture). To force the Metal OOM, a second process (Ollama) held a 25 GB model resident, and the wired limit was set to 40 GB.Repro: load model → send a 12K-word prompt (the prefill fails with the Metal OOM above) →
POST /v1/models/<id>/unload→POST /v1/models/<other>/load.mx.synchronize()atengine_pool.py:3052)Unloaded model … (settled)current_model_memoryafter unload (nothing loaded)The pending error does not resurface on every run. In 5 repro runs with the patch, 2 hit the new path (the warning was logged) and both recovered as above, 2 did not re-raise at all, and 1 unloaded during an in-progress prefill and took the existing 60 s teardown guard (a clean exit and restart, unrelated to this change).
Tests: two regression tests were added to
tests/test_engine_pool.py. One checks that the unload survives a singleCommand buffer execution failedfrommx.synchronize()and releases the accounting. The other checks that aSubmissionsIgnorederror still exits viafatal_exit. On this branch (rebased on currentmain),tests/test_engine_pool.pygives 197 passed; with theengine_pool.pychange reverted, exactly the 2 new tests fail.Not covered: this does not address #2855 (pools of dead processes stranded in wired memory) or #3338 (eviction without draining the buffer pool). It only stops the unload from aborting on a pending command-buffer error.