Skip to content

Fixed the race on the Metal allocator's memory stats - #4885

Merged
zcbenz merged 1 commit into
ml-explore:mainfrom
ipazarbasi:allocator-active-mem-race
Nov 21, 2026
Merged

zcbenz merged 1 commit into
ml-explore:mainfrom
ipazarbasi:allocator-active-mem-race

Conversation

@ipazarbasi

@ipazarbasi ipazarbasi commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

The memory getters read the counters without the mutex while malloc and free update 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.

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: AI not used

@zcbenz
zcbenz marked this pull request as ready for review November 20, 2026 04:11

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Using both locks and atomics does not seem very optimistic, in this specific case, I think we can:

  1. Add locks in get_active_memory etc.
  2. 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

(get_active_memory() > get_memory_limit() &&

and we should probably turn get_active_memory() > get_memory_limit() into an allocator method with a single lock.

@ipazarbasi
ipazarbasi force-pushed the allocator-active-mem-race branch 2 times, most recently from 76c75b5 to 8734e81 Compare November 21, 2026 09:50
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.
@zcbenz
zcbenz merged commit 1fd565f into ml-explore:main Nov 21, 2026
24 checks passed
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}.
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