Repository navigation
fix(studio): recover from a dead Metal GPU queue instead of failing every later request - #11383
Conversation
Three ways an MLX failure reached the user as "An internal error occurred" with nothing in the log.
`_mlx_optional_fusion` guards entry so no optional fusion can fail a load or a request that worked without it. That guard is right for every entry failure but one: once Apple's watchdog has killed a command buffer, the process's GPU refuses every later submission, so there is no unfused path left to fall back to. Logging "continuing with native inference" and carrying on produced a model that answered at 0.097 tok/s and then failed every request. A dead queue is now re-raised, which fails the load and lets the worker be reaped -- the only way back to a usable queue.
"Dead queue" is deliberately narrow. `mlx/backend/metal/device.cpp` formats every `MTLCommandBufferStatusError` as `[METAL] Command buffer execution failed: {description}`, so `Insufficient Memory` shares the wrapper with a watchdog kill, and mlx clears the error when the next commands are encoded. Packing allocates memory native inference never needs, so an allocation failure there is exactly the entry failure the fallback exists for; matching the wrapper would have refused to load models that run fine unfused. Only the watchdog kill and the post-kill refusal count.
`safe_error_detail` recognised none of this. `[malloc] Unable to allocate` fell through to the generic fallback, and "GPU Timeout Error" matched the connection branch, so a dead GPU was reported to the user as an upstream network problem. The Metal and allocation wordings are now classified, ahead of that branch.
`_friendly_gen_stream_error` logs the cause it is about to replace. All four `except GenStreamErrorRaised` handlers funnel through it and none logged anything, so a local generation failure left no server-side record at all, unlike its `except Exception` neighbours.
28b450f to
7f4dabb
Compare
Apple's GPU watchdog kills a command buffer that stalls, and every later submission from that process returns SubmissionsIgnored. The process itself stays healthy, so nothing reaps it: a fault raised mid-generation left a live worker that failed every subsequent request with a generic internal error. A failed load was already reaped by load_model, which is why that path recovered on a retry. The orchestrator now inspects every worker response where it is read -- `_read_resp` for direct reads, the new `_read_mailbox` for dispatcher-routed ones -- rather than where an error is reported. Cancellation consumes a fault and discards it without reporting, and a dead GPU queue makes generation crawl, so stopping is the likeliest way to meet one. A fault may only retire the worker that produced it. A request's mailbox outlives the process it was opened for, so `_WorkerMailbox` carries that identity rather than sampling the current one; `_read_resp` takes the handle before the queue, leaving the harmless pairing when a reload lands between the two. The teardown and the model-mirror clear run under `_subprocess_shutdown_lock`, the lock load_model publishes under, and the mirrors clear only once the process is confirmed dead -- a child that survives SIGKILL keeps its handle and still holds the model.
7f4dabb to
99e6dcf
Compare
|
Ran the Codex reviewer and the full CI suite on a staging PR against a pinned base in my fork, because the Codex connector does not respond in this repository and CI here is slower to turn around. Both are green on the exact commit this PR points at, Codex converged with no findings outstanding: Lyxot#64 (comment) Four findings came out of convergence. Three were real and are fixed in this PR; all three were the same mechanism, so the third fix closes the class rather than adding a third special case.
One finding was left unfixed, with the reasoning in the thread: after any retirement, sibling streams wait up to one read timeout before CI: all checks green on both PRs — 52 passed and 4 skipped here, 51 passed and 4 skipped on the staging PR. One CI note worth recording. |
|
Confirmed this hits the path in studio/backend/utils/utils.py where a Metal watchdog kill still falls through to the connection branch, and that nothing on main retires a worker whose queue is refusing submissions. Will get this reviewed. |
|
@codex review |
|
Review record for #11383, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#98. Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found. Round 1 — reviewed mirror
|
…very later request (unslothai#11383) * fix(studio): report a dead Metal queue instead of hiding it Three ways an MLX failure reached the user as "An internal error occurred" with nothing in the log. `_mlx_optional_fusion` guards entry so no optional fusion can fail a load or a request that worked without it. That guard is right for every entry failure but one: once Apple's watchdog has killed a command buffer, the process's GPU refuses every later submission, so there is no unfused path left to fall back to. Logging "continuing with native inference" and carrying on produced a model that answered at 0.097 tok/s and then failed every request. A dead queue is now re-raised, which fails the load and lets the worker be reaped -- the only way back to a usable queue. "Dead queue" is deliberately narrow. `mlx/backend/metal/device.cpp` formats every `MTLCommandBufferStatusError` as `[METAL] Command buffer execution failed: {description}`, so `Insufficient Memory` shares the wrapper with a watchdog kill, and mlx clears the error when the next commands are encoded. Packing allocates memory native inference never needs, so an allocation failure there is exactly the entry failure the fallback exists for; matching the wrapper would have refused to load models that run fine unfused. Only the watchdog kill and the post-kill refusal count. `safe_error_detail` recognised none of this. `[malloc] Unable to allocate` fell through to the generic fallback, and "GPU Timeout Error" matched the connection branch, so a dead GPU was reported to the user as an upstream network problem. The Metal and allocation wordings are now classified, ahead of that branch. `_friendly_gen_stream_error` logs the cause it is about to replace. All four `except GenStreamErrorRaised` handlers funnel through it and none logged anything, so a local generation failure left no server-side record at all, unlike its `except Exception` neighbours. * fix(studio): retire an inference worker whose Metal queue has died Apple's GPU watchdog kills a command buffer that stalls, and every later submission from that process returns SubmissionsIgnored. The process itself stays healthy, so nothing reaps it: a fault raised mid-generation left a live worker that failed every subsequent request with a generic internal error. A failed load was already reaped by load_model, which is why that path recovered on a retry. The orchestrator now inspects every worker response where it is read -- `_read_resp` for direct reads, the new `_read_mailbox` for dispatcher-routed ones -- rather than where an error is reported. Cancellation consumes a fault and discards it without reporting, and a dead GPU queue makes generation crawl, so stopping is the likeliest way to meet one. A fault may only retire the worker that produced it. A request's mailbox outlives the process it was opened for, so `_WorkerMailbox` carries that identity rather than sampling the current one; `_read_resp` takes the handle before the queue, leaving the harmless pairing when a reload lands between the two. The teardown and the model-mirror clear run under `_subprocess_shutdown_lock`, the lock load_model publishes under, and the mirrors clear only once the process is confirmed dead -- a child that survives SIGKILL keeps its handle and still holds the model. * Trim comments in the Metal queue recovery change * Drop blank lines left by removed class docstrings * Drop four restating docstrings --------- Co-authored-by: Daniel Han <[email protected]>
What breaks today
Apple's GPU watchdog kills a Metal command buffer that stalls, and every later submission from that process returns
SubmissionsIgnored. The process itself stays healthy, so nothing reaps it: a fault raised mid-generation left a live inference worker that failed every subsequent request with "An internal error occurred" and nothing in the log. A failed load was already reaped byload_model, which is why that path recovered on a retry while generation did not.Three separate places turned an MLX failure into that generic message:
_mlx_optional_fusionguards entry so no optional fusion can fail a load or a request that worked without it. That guard is right for every entry failure but one: once the watchdog has killed a command buffer, the process's GPU refuses every later submission, so there is no unfused path left to fall back to. Logging "continuing with native inference" and carrying on produced a model that answered at 0.097 tok/s and then failed every request.safe_error_detailrecognised none of the Metal wordings.[malloc] Unable to allocatefell through to the generic fallback, and "GPU Timeout Error" matched the connection branch, so a dead GPU was reported to the user as an upstream network problem._friendly_gen_stream_errorreplaced the cause without logging it. All fourexcept GenStreamErrorRaisedhandlers funnel through it and none logged anything, so a local generation failure left no server-side record at all, unlike itsexcept Exceptionneighbours.What changed
A dead queue is re-raised rather than swallowed, which fails the load and lets the worker be reaped -- the only way back to a usable queue. The Metal and allocation wordings are classified ahead of the connection branch, and the cause being replaced is logged.
The orchestrator retires a worker whose queue is dead. Every worker response is inspected where it is read --
_read_respfor direct reads, the new_read_mailboxfor dispatcher-routed ones -- rather than where an error is reported. Cancellation consumes a fault and discards it without reporting, and a dead GPU queue makes generation crawl, so pressing Stop is the likeliest way to meet one.A fault may only retire the worker that produced it. A request's mailbox outlives the process it was opened for, so
_WorkerMailboxcarries that identity rather than sampling the current one, and_read_resptakes the process handle before the queue, so a reload landing between the two leaves the harmless pairing. Teardown and the model-mirror clear run under_subprocess_shutdown_lock, the lockload_modelpublishes under, and the mirrors clear only once the process is confirmed dead -- a child that survivesSIGKILLkeeps its handle and still holds the model."Dead queue" is deliberately narrow.
mlx/backend/metal/device.cppformats everyMTLCommandBufferStatusErroras[METAL] Command buffer execution failed: {description}, soInsufficient Memoryshares that wrapper with a watchdog kill, and mlx clears the error when the next commands are encoded. Packing allocates memory native inference never needs, so an allocation failure there is exactly the entry failure the fallback exists for, and matching the wrapper would have refused to load models that run fine unfused. Only the watchdog kill and the refusal after it count.Validation
python -m pytest tests/test_orchestrator_unload_cancel.py tests/test_audio_tts_cancellation.py \ tests/test_inference_dispatcher_resilience.py tests/test_active_generations.py -q # 1 failed, 236 passedThe single failure,
test_active_generations.py::test_a_real_reload_still_refuses_while_chats_stream, fails identically on an unmodified checkout of the target branch. Eighteen mutations of the retirement logic were each confirmed to fail a test.Risks and tradeoffs
gpu timeout,submissionsignored). If alocalizedDescriptionis ever reworded the retirement stops firing silently, and mlx exposes no typed error to key off instead.errorresponse carrying norequest_idwhile the dispatcher is running reaches no reader and is dropped. That is pre-existing and unchanged here: MLX generate faults carry an id, and init errors without one are read before a dispatcher exists.