Repository navigation
Fixed the race on the Metal allocator's memory stats - #4885
Merged
Merged
Conversation
zcbenz
marked this pull request as ready for review
November 20, 2026 04:11
zcbenz
requested changes
Nov 20, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
Using both locks and atomics does not seem very optimistic, in this specific case, I think we can:
- Add locks in
get_active_memoryetc. - In the implementation of allocator classes, access the data members directly instead of using getters like
get_active_memory.
so there would be no need to mix locks and atomics.
Also race condition could still happen in theory for code like
Line 324 in 8f0f391
and we should probably turn get_active_memory() > get_memory_limit() into an allocator method with a single lock.
ipazarbasi
force-pushed
the
allocator-active-mem-race
branch
2 times, most recently
from
November 21, 2026 09:50
76c75b5 to
8734e81
Compare
The memory getters read the counters without the mutex while `malloc` and `free` update them under it. This patch ensures counters are locked by a single mutex.
ipazarbasi
force-pushed
the
allocator-active-mem-race
branch
from
November 21, 2026 09:51
8734e81 to
4c6f0ef
Compare
zcbenz
approved these changes
Nov 21, 2026
Open
2 of 4 tasks
11 tasks
rcfa
pushed a commit
to cubiculum-ai/mlx
that referenced
this pull request
Nov 26, 2026
Ported: the fork's memory stat getters are relaxed atomic loads (osaurus-ai/mlx PR 16), and they stay so. In the Metal and no-GPU allocators, over_memory_limit() does not take mutex_, which malloc holds during garbage collection. Apple's overflow asserts are ported onto the atomics. The CUDA hunks and the tests are Apple's. (cherry picked from commit 1fd565f)
Brooooooklyn
added a commit
to mlx-node/mlx
that referenced
this pull request
Nov 29, 2026
…aph limits, and Win32 NVRTC/PTX fixes Adapted for upstream: - ml-explore#4885: use private cache_memory_locked() and memory_limit_locked() while holding mutex_ in malloc_async()/free() to avoid recursive mutex deadlock, preserving overflow asserts. - ml-explore#4868: preserve move_to_device_memory() / cuda_malloc_async() / cuda_free_async(). - ml-explore#4711: preserve debug track_allocation/untrack_allocation and registered_data_ptrs_. - ml-explore#4706: preserve BIN_DIR macro and try/catch in delayload.cpp alongside resolve_bin_dir(), std::error_code guards, and unconditional nvrtc-builtins preload. - ml-explore#4745: preserve force_single_root_/roots_/get_noop_kernel_ptr() in cuda/device.{h,cpp}.
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.
The memory getters read the counters without the mutex while
mallocandfreeupdate them under it. This patch ensures the counters are guarded with a single mutex.Note: The build must be instrumented with thread sanitizer to validate the fix.