Repository navigation
Fix mlx_lm.server model-swap leak and dead generation thread - #1837
Merged
michalk8 merged 7 commits intoSep 11, 2026
Merged
Conversation
Dropping the previous model only returns its weights to MLX's cache, so a long-lived server kept every model it had loaded. Collect and clear_cache before the next load.
An uncaught exception in _generate left HTTP workers up while every completion hung. Catch that death, fail queued work, and make /health and /v1/models return 503 so a probe can restart the process.
A request could land on the queue after the crash drain finished and then hang forever. Hold one lock around the failed flag, the drain, and generate()'s availability check plus put.
Main landed reset() on swap, health tied to the generation thread, and cache teardown in that thread. Keep those, and still call gc.collect() plus mx.clear_cache() after reset so the buffer pool does not hold the previous weights. Enqueue still fails fast if the generation thread has already died.
Contributor
Author
|
Merged current This branch still does two things
|
michalk8
self-requested a review
September 11, 2026 12:56
michalk8
approved these changes
Sep 11, 2026
michalk8
left a comment
Collaborator
There was a problem hiding this comment.
Thank for the PR, I did some changes:
- /v1/models 503 dropped, since /health already reports this
- the queue drain + lock replaced with a timeout wait, it's simpler
8 tasks
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.
Fixes #1757
Fixes #1505
ModelProvider._loaddropped the old model references and immediately loaded the next one. Those tensors go into MLX's buffer pool, not back to the OS, andserver.pynever calledmx.clear_cache()(unlikegenerate.py/evaluate.py). After dropping the old refs,gc.collect()thenmx.clear_cache().If
_generatedies, the HTTP server kept answering/healthand/v1/modelswith 200 while every completion waited forever on the dead thread. Wrap the generation thread, mark it failed, fail queued requests, reject newgenerate()calls, and return 503 from those endpoints. A request that arrives during the crash drain is rejected instead of hanging.Tests cover both: two fake model loads asserting
clear_cache, and a crashedload_defaultthat makesgeneration_available()false.