Skip to content

fix(engine_pool): survive pending Metal command-buffer errors during unload (#3737, #4344) - #4377

Merged
jundot merged 1 commit into
jundot:mainfrom
no84by:fix/3737-unload-metal-error
Oct 9, 2026
Merged

jundot merged 1 commit into
jundot:mainfrom
no84by:fix/3737-unload-metal-error

Conversation

@no84by

@no84by no84by commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

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_engine re-raises it (RuntimeError: [METAL] Command buffer execution failed: Insufficient Memory (00000008:kIOGPUCommandBufferCallbackErrorOutOfMemory)). The exception escapes the unload (the endpoint returns 500) before _current_model_memory is 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:

  • catches only RuntimeError whose message contains Command buffer execution failed; every other exception propagates unchanged;
  • calls exit_if_gpu_submissions_ignored(e) first, so the existing fatal path for SubmissionsIgnored is kept;
  • logs a warning and retries once. The raise consumes the pending error, so the retry succeeds and the settle barrier and accounting release run as usual.

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.

before (0.7.0) with this PR
unload 500 (RuntimeError from mx.synchronize() at engine_pool.py:3052) 200, Unloaded model … (settled)
current_model_memory after unload (nothing loaded) 20.48 GB 0
next load of another model 507 "projected memory … exceeds ceiling" 200

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 single Command buffer execution failed from mx.synchronize() and releases the accounting. The other checks that a SubmissionsIgnored error still exits via fatal_exit. On this branch (rebased on current main), tests/test_engine_pool.py gives 197 passed; with the engine_pool.py change 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.

…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 jundot left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
jundot merged commit 6f938f6 into jundot:main Oct 9, 2026
1 check passed
jundot added a commit that referenced this pull request Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants